快速创建一个zend framework 2的module

来源:互联网 发布:屏蔽ip地址软件 编辑:程序博客网 时间:2024/06/06 12:29

资源 ZendSkeletonModule: https://github.com/zendframework/ZendSkeletonModule。

首先假定,已经部署好了zend framework 2项目框架。如果没有,参见前面的文章从零创建zend framework 2项目框架

 目录也参见上一个项目的目录:/var/www/newzf2
 将ZendSkeletonModule解压到/var/www/newzf2/module/下新增了一个目录/var/web/newzf2/module/ZendSkeletonModule-master
将ZendSkeletonModule-master改名为Register。
进入/var/web/newzf2/module/Register,
目录内容如下:
         
autoload_classmap.phpautoload_function.phpautoload_register.phpconfigLICENSE.txtModule.phpREADME.mdsrctestsview

将Module.php内容的namespace ZendSkeletonModule;改为namespace Users;
controller的修改
将/var/web/newzf2/module/Register/src/ZendSkeletonModule/Controller/SkeletonController.php 改为
/var/web/newzf2/module/Register/src/Register/Controller/IndexController.php
文件内容:
将IndexController.php的namespace ZendSkeletonModule\Controller; 改为namespace Register\Controller;
class SkeletonController  改为class IndexController 
namespace Register\Controller;use Zend\Mvc\Controller\AbstractActionController;class IndexController extends AbstractActionController{    public function indexAction()    {        return array();    }    public function fooAction()    {        // This shows the :controller and :action parameters in default route        // are working when you browse to /module-specific-root/skeleton/foo        return array();    }}

view的修改
将/var/web/newzf2/module/Register/view/zend-skeleton-module/skeleton/改为
/var/web/newzf2/module/Register/view/register/index/

module 配置文件:
cd /var/web/newzf2/module/Register
1. autoload配置文件。
修改autoload_classmap.php的内容,用一下内容替换:
<?phpreturn array();
2.module配置信息
 module的配置文件位置:config/module.config.php; 
默认内容如下:
<?phpreturn array(    'controllers' => array(        'invokables' => array(            'ZendSkeletonModule\Controller\Skeleton' => 'ZendSkeletonModule\Controller\SkeletonController',        ),    ),    'router' => array(        'routes' => array(            'module-name-here' => array(                'type'    => 'Literal',                'options' => array(                    // Change this to something specific to your module                    'route'    => '/module-specific-root',                    'defaults' => array(                        // Change this value to reflect the namespace in which                        // the controllers for your module are found                        '__NAMESPACE__' => 'ZendSkeletonModule\Controller',                        'controller'    => 'Skeleton',                        'action'        => 'index',                    ),                ),                'may_terminate' => true,                'child_routes' => array(                    // This route is a sane default when developing a module;                    // as you solidify the routes for your module, however,                    // you may want to remove it and replace it with more                    // specific routes.                    'default' => array(                        'type'    => 'Segment',                        'options' => array(                            'route'    => '/[:controller[/:action]]',                            'constraints' => array(                                'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',                                'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',                            ),                            'defaults' => array(                            ),                        ),                    ),                ),            ),        ),    ),    'view_manager' => array(        'template_path_stack' => array(            'ZendSkeletonModule' => __DIR__ . '/../view',        ),    ),);

修为动作:
controller改为:
'controllers' => array(        'invokables' => array(            'Register\Controller\Index' => 'Register\Controller\IndexController',        ),    ),
view修改:
'view_manager' => array(        'template_path_stack' => array(            'register' => __DIR__ . '/../view',        ),    ),
注意,view使用小写字母,多个词语的module使用“-”横划线连接。例如:ZendSkeleton要写成 zend-skeleton。
Routes的配置:(三处需要修改)
'module-name-here' => array(
'route'    => '/module-specific-root',
'__NAMESPACE__' => 'ZendSkeletonModule\Controller',
                        'controller'    => 'Skeleton',
                        'action'        => 'index',

'router' => array(        'routes' => array(            'module-name-here' => array(                'type'    => 'Literal',                'options' => array(                    // Change this to something specific to your module                    'route'    => '/module-specific-root',                    'defaults' => array(                        // Change this value to reflect the namespace in which                        // the controllers for your module are found                        '__NAMESPACE__' => 'ZendSkeletonModule\Controller',                        'controller'    => 'Skeleton',                        'action'        => 'index',                    ),                ),                'may_terminate' => true,                'child_routes' => array(                    // This route is a sane default when developing a module;                    // as you solidify the routes for your module, however,                    // you may want to remove it and replace it with more                    // specific routes.                    'default' => array(                        'type'    => 'Segment',                        'options' => array(                            'route'    => '/[:controller[/:action]]',                            'constraints' => array(                                'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',                                'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',                            ),                            'defaults' => array(                            ),                        ),                    ),                ),            ),        ),    ),



添加Application 配置文件
cd /var/web/newzf2/config,修改application.config.php
添加register module配置:
'modules' => array(        'Application',        'Register',        ),


为了调试方便,最好打开错误日志。
一种是在public/index.php
error_reporting(E_ALL);
ini_set("display_errors", 1);

一种是修改php.ini
error_reporting = E_ALL & ~E_STRICT,
打开短标签支持,方便ZF2模板编写:
short_open_tag = On
访问:http://host/register;  http://host/register/index/index; http://host/register/index/foo 都正确显示。


0 0
原创粉丝点击