apisc/phroper

此包最新版本(dev-master)没有可用的许可证信息。

Phroper api 引擎

dev-master 2021-11-16 14:36 UTC

This package is auto-updated.

Last update: 2024-09-16 20:54:48 UTC


README

无头CMS引擎,用php编写,为您量身定制 ❤.

启动配置

为了处理Phroper的入站请求,您需要将请求重定向到php文件(例如:index.php)。如果您使用的是apache服务器,请编写一个 .htaccess 文件并启用url重写模块。

示例 .htaccess 内容

RewriteEngine On
RewriteRule ^([^?]*) index.php?__url__=$1 [L,QSA]

<Limit GET POST PUT OPTIONS DELETE>
    Require all granted
</Limit>
<LimitExcept GET POST PUT OPTIONS DELETE>
    Require all denied
</LimitExcept>

在php文件中,您必须提供ROOT常量作为Phroper服务器的根目录,以正确处理动态导入。创建一个Phroper实例,注册处理程序,并调用run方法。

示例 index.php

<?php

define('ROOT', dirname(__FILE__));
define('DS', DIRECTORY_SEPARATOR);

require_once("phroper/index.php");

Phroper::setMysqli(new mysqli(
    "localhost",
    "user",
    "password",
    "database"
));

Phroper::serveApi("api/");
Phroper::serveFolder(ROOT . DS . "public");
Phroper::serveFallbackFile(ROOT . DS . "public" . DS . "index.html");

Phroper::run();

模型

您可以在 Models/ 下创建新的数据模式,Phroper可以处理模型中的php类和json文件。类必须在 Models 命名空间中声明,并继承自 Phroper\Model。

模型参数

这些参数可以被配置来指定模型的行为。在声明php类时,这些参数可以作为关联数组传递给Model类的构造函数的第一个参数,在JSON模型中,这些参数被设置在JSON对象的根目录。

模型字段

字段决定了实体的结构。模型有4个预定义字段,但任何一个都可以被重写或通过将其设置为null来删除。在php类模型中,字段可以通过类似关联数组的 $fields 成员进行访问和修改,并必须提供一个Field类型的实例,在JSON模型中,字段是通过fields对象的成员设置的,可以指定字段的类名为字符串,或指定一个数组(类名后跟构造函数参数)。

示例

以下示例显示了使用php和json结构等效的模型声明;

// Models/Logs.php
namespace Models;


use Phroper\Model;
use Phroper\Fields\Email;
use Phroper\Fields\Password;

class Log extends Model {
  public function __construct() {
    parent::__construct([
      "sql_table" => "log",
      "editable" => false,
      "default_service" => false
    ]);

    $this->fields["updated_at"] = null;
    $this->fields["type"] = new Phroper\Fields\Enum(["debug", "info", "warn", "error"]);
    $this->fields["message"] = new Phroper\Fields\Text();
  }
}
// Models/Log.json
{
  "sql_table": "log",
  "editable": false,
  "default_service": false,
  "fields": {
    "updated_at": null,
    "type": ["Enum", ["debug", "info", "warn", "error"]],
    "message": "Text"
  }
}

字段

字段参数可以作为字段构造函数的最后一个参数传递。所有字段类型都是继承自Phroper\Fields\Field。基本字段参数可以在以下表格中找到。

可用的字段类型