您的所有應用程序將從此類派生,或將使用該應用程序類的兄弟姐妹的類。
該課程提供:
隨著時間的流逝,您的應用程序將增加,路線數量將增加。因此,我們提供了方便的方式來存儲所有路由在獨立配置文件中。因此,不必在應用程序(或任何派生類)對象的構造函數中初始化所有路由。
讓我們找出如何使用它。
首先,創建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