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
代码install
git clone https://github.com/jc91715/file-orm-example.git project
composer
cd project
composer install
start php webserver
php -S localhost:8888
localhost:8888/index.php
本作品采用《CC 协议》,转载必须注明作者和本文链接