ZF-学习笔记(2)
来源:互联网 发布:王者荣耀代练接单软件 编辑:程序博客网 时间:2024/06/16 00:13
创建一个新模块
Action
action = 程序页面
module = action + action + …
需求
主页将显示列表之后允许我们添加,编辑和删除.
创建模块
A)新模块根路径下创建Module.php,包含AlbumModule类
Zend Framework 2 使用ModuleManager通过在模块根目录下寻找Module.php文件的XXModule类来加载和配置新模块。因此新建模块需要创建Module.php文件。
AlbumModule类中包含getConfig()方法,加载config/module.config.php;
B)我们在classmap提供一个空数组自动加载 创建一个叫做 autoload_classmap.php
文件在 zf2-tutorial/module/Album
下:
return array();
因为这是一个空数组, 当 autoloader 寻找Album
命名空间下的类的时候 , 他将为我们返回 StandardAutoloader.
然而推荐的做法是
打开根目录中composer.json找到autoload section。添加新模块
"autoload": {"psr-4": { "Application\\": "module/Application/src/", "Album\\": "module/Album/src/" }}
C)新模块config下创建module.config.php,初始化controllers和view_manager.
return array( 'controllers' => array( 'invokables' => array( 'Album\Controller\Album' =>'Album\Controller\AlbumController', ), ), 'view_manager' => array( 'template_path_stack' => array( 'album' => __DIR__ . '/../view', ), ),);
配置信息可以通过’ServiceManager’传递给其他组件。
D)更新config/application.config.php
modules部分包含新模块名称,告诉ModuleManager新模块存在。
return [ 'Zend\Form', 'Zend\Db', 'Zend\Router', 'Zend\Validator', 'Application', 'Album', // <-- Add this line];
添加路由和控制器
A)module.config.php中添加action的路由
module.config.php中定义了URL到特定action的映射。
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', ), ), );
segment允许在URL中指定占位符route参数映射到参数中,即/album[/:action][/:id]将会匹配/album
开始的URL。constraints已限制actions由一个字母开始之后后续的必须是数字或字母,同时id为数字。
B)创建控制器
控制器需大写字母开头,每个action必须是一个公共的方法小写字母开头。
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(){}}
C)设置每个action的URL地址
初始化视图
创建如下脚本
- module/Album/view/album/album/index.phtml
- module/Album/view/album/album/add.phtml
- module/Album/view/album/album/edit.phtml
- module/Album/view/album/album/delete.phtml
Zend_Layout
重复的头部和尾部代码可以移动到独立的布局显示脚本-zf-tutorial/appliaction/layouts/layout.phtml中,并删除重复代码
启用Zend_Layout
Zend_Layout::startMvc(array('layoutPath'=>'../application/layouts'));
References
Zend Framework Manual-EN
- ZF-学习笔记(2)
- ZF-学习笔记(3)
- [置顶] ZF框架学习笔记
- ZF学习
- Deep learning学习笔记(2):Visualizing and Understanding Convolutional Networks(ZF-net)
- ZF Validator 阅读笔记
- zf学习小结
- redis数据库(zf)
- ZF
- ZF
- ZF 编码规范速查笔记
- zf验证码类(普通)
- zf验证码类(Zend_Captcha_Image)
- zf权限问题(第一种)
- zf一些知识(遇见就补)
- ZendFramework学习第三章(核心组件—动态加载文件与类)附加学ZF感慨!
- 基于ZF中的.htaccess文件学习Apache的Rewrite语法
- 基于ZF中的.htaccess文件学习Apache的Rewrite语法
- <转>idear中解决SpringMVC开发过程中的中文乱码问题
- VRTK 瞬移
- mysql】count(*),count(1)与count(column)区别
- x264设置默认参数
- rtabmap_ros 下使用kinect xbox360 测试(一)
- ZF-学习笔记(2)
- git使用小结
- ligerui 实现二级联动代码简单
- 饥饿的奶牛
- iOS8新特性Self Sizing Cells,使用estimatedRowHeight自适应cell
- Struts2 将对象推入到值栈
- Software 2 parking Application
- 咳咳!!!关于Struts2.5版本的一点小见解
- UnityEngine.Time