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
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

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