繼 前一篇文章 我們初步建立了 Module 與設定,接下來要介紹 Routing 與 Controllers
Routing and controllers
依照官方的文件,我們列出下列的表格,分別去描述 Page 名稱與敘述頁面 (Page) 所要做什麼事情
[table “” not found /]從上面得知,我們有四個頁面,那每個頁面有相對應的動作,我們把每個動作寫在同一個 Controller (控制器)中,在 Controller 類別中,建立四個方法分別去對應上面表格中的四個動作。
[table “” not found /]編輯 /path/to/project-name/module/Album/config/module.config.php
從 code 得知,我們加入了一個 Controller 名字叫做:AlbumController 與一段 code 做為:router 路由網址,而下列的表格為路由網址:
[table “” not found /]Create the controller
一般來說 Controller 的命名為:{Controller name}Controller
/path/to/project-name/module/Album/src/Album/Controller
網址對應的動作為:
[table “” not found /]Initialise the view scripts 初始化視圖(樣版)腳本
- /path/to/project-name/module/Album/view/album/album/index.phtml
- /path/to/project-name/module/Album/view/album/album/add.phtml
- /path/to/project-name/module/Album/view/album/album/edit.phtml
- /path/to/project-name/module/Album/view/album/album/delete.phtml
修改 /path/to/project-name/module/Album/config/module.config.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
return array( | |
'controllers' => array( | |
'invokables' => array( | |
//加入這一行 | |
'Album\Controller\Album' => 'Album\Controller\AlbumController', | |
), | |
), | |
// The following section is new and should be added to your file (加入下面這一段) | |
'router' => array( | |
'routes' => array( | |
'album' => array( | |
'type' => 'segment', | |
'options' => array( | |
'route' => '/album[/:action][/:id]', | |
'constraints' => array( | |
'action' => '[a-zA-Z][a-zA-Z0-9_-]*', | |
'id' => '[0-9]+', | |
), | |
'defaults' => array( | |
'controller' => 'Album\Controller\Album', | |
'action' => 'index', | |
), | |
), | |
), | |
), | |
), | |
//加入上面這一段 | |
'view_manager' => array( | |
'template_path_stack' => array( | |
'album' => __DIR__ . '/../view', | |
), | |
), | |
); | |
?> |
下一篇則會介紹:建置資料庫與模型 (Model) 的關係