mezon / application
小型应用程序脚本
Requires
- php: >=7.2.0
- mezon/conf: 1.2.*
- mezon/html-template: >=1.1.4 <1.2.0
- mezon/http-request-params: 1.0.*
- mezon/infrastructure-layer: 1.2.*
- mezon/presenter: 1.3.*
- mezon/rest-exception: 1.0.*
- mezon/router: 1.5.*
- mezon/template-engine: 1.0.*
- mezon/utils: >=1.0.9
Requires (Dev)
- infection/infection: ^0.21.5
- phpunit/phpunit: ^8.5
- vimeo/psalm: ^4.2
- dev-master
- 3.0.15
- 3.0.14
- 3.0.13
- 3.0.12
- 3.0.11
- 3.0.10
- 3.0.9
- 3.0.8
- 3.0.7
- 3.0.6
- 3.0.5
- 3.0.4
- 3.0.3.x-dev
- 3.0.3
- 3.0.2.x-dev
- 3.0.2
- 3.0.1.x-dev
- 3.0.1
- 3.0.0.x-dev
- 3.0.0
- 2.1.4.x-dev
- 2.1.4
- 2.1.3
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.11
- 2.0.10
- 2.0.9
- 2.0.8
- 2.0.7
- 2.0.6
- 2.0.5
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.1.30
- 1.1.29
- 1.1.28
- 1.1.27
- 1.1.26
- 1.1.25
- 1.1.24
- 1.1.22
- 1.1.21
- 1.1.20
- 1.1.19
- 1.1.18
- 1.1.16
- 1.1.15
- 1.1.14
- 1.1.13
- 1.1.12
- 1.1.11
- 1.1.10
- 1.1.9
- 1.1.8
- 1.1.7
- 1.1.6
- 1.1.5
- 1.1.4
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.9
- 1.0.8
- 1.0.7
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
This package is auto-updated.
Last update: 2024-09-17 20:27:59 UTC
README
简介
您所有的应用程序都将从这个类派生,或者将使用与 Application 类同级的类。
功能
此类提供
- 路由
- 将 actionActionName 方法转换为静态路由 /action-name/,该路由处理 GET 和 POST 方法
- 从配置文件(PHP 或 JSON)加载路由
从配置文件加载路由
随着时间的推移,您的应用程序将增长,路由数量也会增加。因此,我们提供了一个方便的方法将所有路由存储在一个独立的配置文件中。因此,没有必要在 Application(或任何派生类)对象的构造函数中初始化所有路由。
让我们看看如何使用它。
首先,在您的项目目录中创建配置文件 ./conf/routes.php。它应该看起来像这样
$callbackProvider = new SomeCallbackClass(); return [ [ 'route' => '/news/' , // your route 'callback' => 'displayNewsLine' // this must be the method name of your // Application derived class ] , [ 'route' => '/news/[i:news_id]/' , // your route 'callback' => 'displayExactNews' , // this must be the method name of your 'method' => 'POST' // Application derived class ] , [ 'route' => '/some-route/' , 'method' => 'GET' , 'callback' => [ // here we specify callback as pair [object, method] callbackProvider , 'someMethod' ] ] ];
请注意,如果 'method' 字段未设置,则默认为 GET。
您还可以指定自己的配置文件。
然后只需调用 Application::loadRoutesFromConfig()。
$app->loadRoutesFromConfig( './conf/my-config.php' );
通用应用程序类
简介
此类提供简单的应用程序流程,具有更复杂的渲染和错误处理。
扩展路由处理
在 Application 类中,路由只能返回字符串。但 CommonApplication 类允许您返回字符串数组,这些字符串将被放置在模板占位符中。
简单示例
class ExampleApplication extends CommonApplication { /** * Constructor. */ function __construct( $template ) { parent::__construct( $template ); } function actionSimplePage() { return [ 'title' => 'Route title' , 'main' => 'Route main' ]; } }
在此示例中,路由处理程序生成页面 /simple-page/ 的两部分 - 'title' 和 'main'。这两部分将分别插入到 {title} 和 {main} 占位符中。
更复杂示例
class ExampleApplication extends CommonApplication { /** * Constructor. */ function __construct($template) { parent::__construct($template); } function actionSimplePage() { return [ 'title' => 'Route title' , 'main' => new View('Generated main content') ]; } }
在此示例中,我们将 View(或从 View 派生的任何类)的实例传递给应用程序页面编译器。它将调用 View::render 方法,该方法必须返回编译后的 HTML 内容。
路由配置
您还可以在配置中保留所有路由。您可以使用 JSON 配置
[ { "route": "/route1/", "callback": "route1", "method": "GET" }, { "route": "/route2/", "callback": "route2", "method": ["GET","POST"] } ]
此数据必须存储在项目 './conf/' 目录中。或者如以下所示显式加载配置(使用方法 loadRoutesFromConfig)。
我们还需要在应用程序类中这些方法。
class ExampleApplication extends CommonApplication { /** * Constructor. */ function __construct($template) { parent::__construct($template); // loading config on custom path $this->loadRoutesFromConfig('./my-routes.json'); } function route1() { return [ // here result ]; } function route2() { return [ // here result ]; } }
请注意,您可以使用一次 loadRoutesFromConfigs 方法的调用加载多个配置
function __construct($template) { parent::__construct($template); $this->loadRoutesFromConfigs(['./conf/my/routes.json', './conf/my-routes.php']); }
或相同的方式
function __construct($template) { parent::__construct($template); $this->loadRoutesFromDirectory('./conf'); }
操作消息
您可以创建包含消息的文件,这些消息将替换模板变量 action-message
此文件必须存储在目录 %your-application-class-directory%/res/action-messages.json 中
然后,如果类找到 $_GET['action-message'] 参数,则将触发 action-message 替换。
视图
静态视图
您可以将 *.tpl 文件的內容直接输出为普通视图。如果您需要渲染静态页面或静态页面部分,这可能很有用。这将让您避免为这些目的创建单独的类和单独的视图方法。
这很简单
// here $template is an instance of the \Mezon\HtmlTemplate\HtmlTemplate class // and 'block-name' is a block name in this class $view = new \Mezon\Application\ViewStatic($template, 'block-name');
有关 Mezon 模板更详细的信息,请参阅 此处
控制器
待定