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();

快速上手

packagist

github

这是一个基于 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 协议》,转载必须注明作者和本文链接
Make everything simple instead of making difficulties as simple as possible
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!