mezon/application

小型应用程序脚本


README

Build Status codecov Scrutinizer Code Quality

简介

您所有的应用程序都将从这个类派生,或者将使用与 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 模板更详细的信息,请参阅 此处

控制器

待定