File ORM Example 
                                                    
                        
                    
                    
  
                    
                    安装
composer require october/halcyon注册数据源
use October\Rain\Halcyon\Model;
use October\Rain\Filesystem\Filesystem;
use October\Rain\Halcyon\Datasource\FileDatasource;
use October\Rain\Halcyon\Datasource\Resolver;
$datasource = new FileDatasource('/path/to/theme', new Filesystem);
$resolver = new Resolver(['theme1' => $datasource]);
$resolver->setDefaultDatasource('theme1');
Model::setDatasourceResolver($resolver);模型示例
use October\Rain\Halcyon\Model;
class MyPage extends Model
{
    /**
     * @var array The attributes that are mass assignable.
     */
    protected $fillable = [
        'markup',
        'title',
        'code'
    ];
    /**
     * @var string The container name associated with the model, eg: pages.
     */
    protected $dirName = 'pages';
}可用的属性
- fileName 文件名字
- content 文件的完整内容
- markup html标记
- code php 标记
创建一个页面
MyPage::create([
    'fileName' => 'my-file',
    'title' => 'Test page',
    'markup' => '<p>Hello world!</p>'
    'code'   => "echo 'hello world';" 
]);将创建/path/to/theme/pages/my-file.htm 的文件。内容为
title = "Test page"
==
<?php
echo 'hello word';
?>
==
<p>Hello world!</p>获得文件模型
$page = MyPage::find('my-file');
echo '<h1>'.$page->title.'</h1>';
echo $page->markup;更新文件模型
$page->fileName = 'renamed-file';
$page->save();快速上手
这是一个基于 ORM 文件模型的例子,是 Eloquent 的表弟.在这个例子中,
- 可以学到像创建Model一样,创建文件,删除文件,更新文件。
- 以及如何以字符串的形式,执行php代码installgit clone https://github.com/jc91715/file-orm-example.git project
composer
cd project 
composer installstart php webserver
php -S localhost:8888localhost:8888/index.php
本作品采用《CC 协议》,转载必须注明作者和本文链接
 
           jcc123 的个人博客
 jcc123 的个人博客
         
                     
                     
           
           关于 LearnKu
                关于 LearnKu
               
                     
                     
                     粤公网安备 44030502004330号
 粤公网安备 44030502004330号 
 
推荐文章: