您的所有应用程序将从此类派生,或将使用该应用程序类的兄弟姐妹的类。
该课程提供:
随着时间的流逝,您的应用程序将增加,路线数量将增加。因此,我们提供了方便的方式来存储所有路由在独立配置文件中。因此,不必在应用程序(或任何派生类)对象的构造函数中初始化所有路由。
让我们找出如何使用它。
首先,创建config file ./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 '
]
]
];请注意,未设置“方法”字段,然后将其默认为获取。
您还可以指定自己的配置文件。
然后只需调用应用程序:: loadRoutesFromConfig()
$ app -> loadRoutesFromConfig ( ' ./conf/my-config.php ' );此类提供简单的应用程序例程,并使用更复杂的渲染和错误处理。
在应用程序类中,路由可能仅返回字符串。但是CommonApplication类允许您返回将放置在模板占位符中的字符串数组。
简单示例:
class ExampleApplication extends CommonApplication
{
/**
* Constructor.
*/
function __construct ( $ template )
{
parent :: __construct ( $ template );
}
function actionSimplePage ()
{
return [
' title ' => ' Route title ' ,
' main ' => ' Route main '
];
}
}此处路由的处理程序生成页面 /简单页面 / - '标题'和“ 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 ' )
];
}
}在这里,我们将类视图的实例(或从视图派生的任何类)传递给应用程序页面编译器。它将调用视图::渲染方法,该方法必须返回编译的HTML内容。
您还可以将AL路线保留在配置中。您可以使用JSON配置:
[
{
"route" : "/route1/" ,
"callback" : "route1" ,
"method" : "GET"
} ,
{
"route" : "/route2/" ,
"callback" : "route2" ,
"method" : [ "GET" , "POST" ]
}
]该数据必须存储在您项目的'./conf/'dir中。或如下所示,明确加载配置(使用方法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
];
}
}请注意,您可以使用Method LoadRoutesfromConfig的一个调用加载多个配置
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 MezonHtmlTemplateHtmlTemplate class
// and 'block-name' is a block name in this class
$ view = new Mezon Application ViewStatic ( $ template , ' block-name ' );有关Mezon模板的更多详细信息,请参阅
TBA