快速创建一个zend framework 2的module

资源 ZendSkeletonModulehttps://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.php
autoload_function.php
autoload_register.php
config
LICENSE.txt
Module.php
README.md
src
tests
view

将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的内容,用一下内容替换:
<?php
return array();
2.module配置信息
 module的配置文件位置:config/module.config.php; 
默认内容如下:
<?php
return 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 都正确显示。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值