Routing and controllers
Routing and controllers
We will build a very simple inventory system to display our album collection. The home page will list our collection and allow us to add, edit and delete albums. Hence the following pages are required:
| Page | Description |
|---|---|
| Home | This will display the list of albums and provide links to edit and delete them. Also, a link to enable adding new albums will be provided. |
| Add new album | This page will provide a form for adding a new album. |
| Edit album | This page will provide a form for editing an album. |
| Delete album | This page will confirm that we want to delete an album and then delete it. |
Before we set up our files, it's important to understand how the framework expects the pages to be organised. Each page of the application is known as anaction and actions are grouped into controllers within modules. Hence, you would generally group related actions into a controller; for instance, a news controller might have actions of current, archived, and view.
As we have four pages that all apply to albums, we will group them in a single controller AlbumController within our Album module as four actions. The four actions will be:
| Page | Controller | Action |
|---|---|---|
| Home | AlbumController |
index |
| Add new album | AlbumController |
add |
| Edit album | AlbumController |
edit |
| Delete album | AlbumController |
delete |
The mapping of a URL to a particular action is done using routes that are defined in the module’s module.config.php file. We will add a route for our album actions. This is the updated module config file with the new code highlighted using comments.
namespace Album;
use Zend\ServiceManager\Factory\InvokableFactory;
return [
'controllers' => [
'factories' => [
Controller\AlbumController::class => InvokableFactory::class,
],
],
// The following section is new and should be added to your file:
'router' => [
'routes' => [
'album' => [
'type' => 'segment',
'options' => [
'route' => '/album[/:action[/:id]]',
'constraints' => [
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
],
'defaults' => [
'controller' => Controller\AlbumController::class,
'action' => 'index',
],
],
],
],
],
'view_manager' => [
'template_path_stack' => [
'album' => __DIR__ . '/../view',
],
],
];
The name of the route is ‘album’ and has a type of ‘segment’. The segment route allows us to specify placeholders in the URL pattern (route) that will be mapped to named parameters in the matched route. In this case, the route is/album[/:action[/:id]] which will match any URL that starts with /album. The next segment will be an optional action name, and then finally the next segment will be mapped to an optional id. The square brackets indicate that a segment is optional. The constraints section allows us to ensure that the characters within a segment are as expected, so we have limited actions to starting with a letter and then subsequent characters only being alphanumeric, underscore, or hyphen. We also limit the id to digits.
This route allows us to have the following URLs:
| URL | Page | Action |
|---|---|---|
/album |
Home (list of albums) | index |
/album/add |
Add new album | add |
/album/edit/2 |
Edit album with an id of 2 | edit |
/album/delete/4 |
Delete album with an id of 4 | delete |
Create the controller
We are now ready to set up our controller. For zend-mvc, the controller is a class that is generally called {Controller name}Controller; note that{Controller name} must start with a capital letter. This class lives in a file called{Controller name}Controller.php within the Controller subdirectory for the module; in our case that is module/Album/src/Controller/. Each action is a public method within the controller class that is named {action name}Action, where {action name} should start with a lower case letter.
Conventions not strictly enforced
This is by convention. zend-mvc doesn't provide many restrictions on controllers other than that they must implement the
Zend\Stdlib\Dispatchableinterface. The framework provides two abstract classes that do this for us:Zend\Mvc\Controller\AbstractActionControllerandZend\Mvc\Controller\AbstractRestfulController. We'll be using the standardAbstractActionController, but if you’re intending to write a RESTful web service,AbstractRestfulControllermay be useful.
Let’s go ahead and create our controller class in the filezf2-tutorials/module/Album/src/Controller/AlbumController.php:
namespace Album\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class AlbumController extends AbstractActionController
{
public function indexAction()
{
}
public function addAction()
{
}
public function editAction()
{
}
public function deleteAction()
{
}
}
We have now set up the four actions that we want to use. They won't work yet until we set up the views. The URLs for each action are:
| URL | Method called |
|---|---|
http://zf2-tutorial.localhost/album |
Album\Controller\AlbumController::indexAction |
http://zf2-tutorial.localhost/album/add |
Album\Controller\AlbumController::addAction |
http://zf2-tutorial.localhost/album/edit |
Album\Controller\AlbumController::editAction |
http://zf2-tutorial.localhost/album/delete |
Album\Controller\AlbumController::deleteAction |
We now have a working router and the actions are set up for each page of our application.
It's time to build the view and the model layer.
Initialise the view scripts
To integrate the view into our application, we need to create some view script files. These files will be executed by the DefaultViewStrategy and will be passed any variables or view models that are returned from the controller action method. These view scripts are stored in our module’s views directory within a directory named after the controller. Create these four empty files now:
module/Album/view/album/album/index.phtmlmodule/Album/view/album/album/add.phtmlmodule/Album/view/album/album/edit.phtmlmodule/Album/view/album/album/delete.phtml
We can now start filling everything in, starting with our database and models.
Routing and controllers的更多相关文章
- Laravel Controllers
Basic Controllers Instead of defining all of your route-level logic in a single routes.php file, you ...
- Orchard源码分析(7):ASP.NET MVC相关
概述 Orchard归根结底是一个ASP.NET MVC(以后都简称为MVC)应用,但在前面的分析中,与MVC相关内容的涉及得很少.MVC提供了非常多的扩展点,本文主要关注Orchard所做的扩展.主 ...
- 返璞归真 asp.net mvc (13) - asp.net mvc 5.0 新特性
[索引页][源码下载] 返璞归真 asp.net mvc (13) - asp.net mvc 5.0 新特性 作者:webabcd 介绍asp.net mvc 之 asp.net mvc 5.0 新 ...
- ASP.NET MVC相关
Orchard源码分析(7):ASP.NET MVC相关 概述 Orchard归根结底是一个ASP.NET MVC(以后都简称为MVC)应用,但在前面的分析中,与MVC相关内容的涉及得很少.MVC提供 ...
- Professional C# 6 and .NET Core 1.0 - Chapter 41 ASP.NET MVC
What's In This Chapter? Features of ASP.NET MVC 6 Routing Creating Controllers Creating Views Valida ...
- Pylons架构网站开发从0到1
首先说明下这里的从0到1指的是从没有听说过pylons到开发出一个看上去还不错的网站.一个月前,我没有听说过也不知道什么是pylons,HTML只知道一些标签,JavaScript也不怎么懂,由于只倾 ...
- 【ASP.NET Core】MVC 控制器的模型绑定(宏观篇)
欢迎来到老周的水文演播中心. 咱们都知道,MVC的控制器也可以用来实现 Web API 的(它们原本就是一个玩意儿),区别嘛也就是一个有 View 而另一个没有 View.于是,在依赖注入的服务容器中 ...
- ASP.NET Core 中文文档 第四章 MVC(4.1)Controllers, Actions 和 Action Results
原文:Controllers, Actions, and Action Results 作者:Steve Smith 翻译:姚阿勇(Dr.Yao) 校对:许登洋(Seay) Action 和 acti ...
- 解读ASP.NET 5 & MVC6系列(12):基于Lamda表达式的强类型Routing实现
前面的深入理解Routing章节,我们讲到了在MVC中,除了使用默认的ASP.NET 5的路由注册方式,还可以使用基于Attribute的特性(Route和HttpXXX系列方法)来定义.本章,我们将 ...
随机推荐
- 【转】蓝牙ble app开发(三) -- 抓包
原文网址:http://blog.csdn.net/lckj686/article/details/43156617 关于android 蓝牙app开发抓包的重要性在 android 蓝牙ble ap ...
- Event事件详解
首先提到event,先要明白event的产生,也要先明白焦点,什么是焦点.焦点 : 使浏览器能够区分用户输入的对象,当一个元素有焦点的时候,那么他就可以接收用户的输入. 我们可以通过一些方式给元素设置 ...
- 使用SVN小结
自从加入团队后,开始使用SVN,以下是个人对SVN的一些认识和小结. 一.SVN是什么? SVN是版本管理工具,譬如团队进行项目开发,项目代码都储存在服务器上,成员可用SVN在本地获得并更新代码. 二 ...
- Beginning Android 4 Programming Book学习
Chapter 3 EditText不自动获取焦点,自动获取焦点但不显示软键盘 Page 122-123 只有定义了android:id属性的控件在屏幕翻转后状态才会被持久化 Page 133 C ...
- oracle 问题若干 提醒注意
1.Powerdesigner 里生成sql,在oracle中运行时报错:ORA-00907: 缺失右括号 解决:这样的问题很多时候是因为用了不正确的数据类型造成的.比如写作nvarchar(n),但 ...
- 3D魔方游戏
初学OpenGL时做的小程序,涉及到了OpenGL的大部分基本内容,如视图模型变换.色彩.纹理贴图.材质.光照.显示列表.选择等 三阶魔方有3×3×3个方块组成,每个方块的类当中都有一个4×4的矩阵, ...
- Excel2010: Excel使用小技巧(不断更新)
目录 1. 如何设置定时保存2. 调出“开发工具”功能区3. 在Excel单元格中输入对号和错号4. 设置单元格自动换行5. 向Excel中导入.txt文件6. 如何批量删除超链接7. 如何冻结窗格8 ...
- Linux与Windows的8个不同
(整理来自网络) 对刚刚接触Linux的人来说,很容易从windows的观念去理解Linux系统.今天扒一扒Win和Linux之间常见的8个区别. 一.Linux终端输入密码不回显字符 用户的密码在L ...
- Hive基础介绍
HIVE结构 Hive 是建立在 Hadoop 上的数据仓库基础构架.它提供了一系列的工具,可以用来进行数据提取转化加载(ETL),这是一种可以存储.查询和分析存储在 Hadoop 中的大规模数据的机 ...
- POJ3468--A Simple Problem with Integers(Splay Tree)
虽然有点难,但是这套题都挂了一个月了啊喂…… 网上模板好多……最后还是抄了kuangbin聚聚的,毕竟好多模板都是抄他的,比较习惯…… POJ 3468 题意:给n个数,两种操作,区间整体加一个数,或 ...