【问题标题】:Zend Framework + Doctrine1.2 project structure with more modulesZend Framework + Doctrine1.2 项目结构,模块较多
【发布时间】:2011-11-02 20:48:27
【问题描述】:

应用加上 ZendFramework + Doctrine 1.2 的实现已经有一段时间了,但现在他们有了更多模块的使用经验。我要求每个人都可以看到具有特定布局的默认模块和具有不同布局的管理模块。

到目前为止,在我的应用程序中,我一直使用以下结构:

/app
    /application
        /acls
        /configs
        /controllers
        /forms
        /layouts
        /models --> models by doctrine
            /generated --> base models by doctrine
        /plugins
        /views
        /Bootstrap.php
    /data
    /doctrine
        /data
        /migrations
        /schema
            /schema.yml
        /doctrine.php
    /library
    /public
    /tests

所以我的问题是:我的应用程序的结构应该如何做所需要的?

我尝试使用zend工具,看看我创建了什么样的结构命令:

zf create module admin

启动后的课程

zf create project app

我注意到创建命令模块我在application 中创建了一个文件夹modules。 在modules 中创建了admin,并在其中创建了controllersmodelsviews

所以zf除了意味着分离控制器和视图正确之外,还意味着模型。 但是我的学说一方面创造了所有的模型! :D

如何使用由学说为每个模块创建的模板?

那我该如何为管理模块分配新的布局呢?

对于聚会的客人,您建议我离开当前使用的设施或将其全部移动到default的表格中?

对不起,如果我提出了很多问题,也许太多了,但我真的很困惑!

谢谢

【问题讨论】:

    标签: zend-framework structure doctrine-1.2


    【解决方案1】:

    经过详尽的文档,我找到了正确的项目结构

    /app
    /application
        /acls
        /configs
            application.ini
        /layouts
            /scripts
                admin.phtml
                default.phtml
        /models --> models by doctrine
            /generated --> base models by doctrine
        /modules
            /admin
                /controllers
                /forms
                /view
            /default
                /controllers
                /forms
                /view
        /plugins
        /Bootstrap.php
    /data
    /doctrine
        /data
        /migrations
        /schema
            /schema.yml
        /doctrine.php
    /library
    /public
    /tests
    

    为了根据你所在的模块查看不同的布局,我使用了以下插件:

    class Plugin_Layout extends Zend_Controller_Plugin_Abstract
    {
        /**
         * Called before an action is dispatched by Zend_Controller_Dispatcher.
         *
         * @param  Zend_Controller_Request_Abstract $request
         * @return void
         */
        public function preDispatch(Zend_Controller_Request_Abstract $request)
        {
            $layout = Zend_Layout::getMvcInstance();
            $module = $request->getModuleName();
            switch ($module) {
                case 'default':
                    $layout->setLayout('default');
                    break;
                case 'admin':
                    $layout->setLayout('admin');
                    break;
                default:
                    $layout->setLayout('default');
                    break;
            }
        }
    }
    

    进入 Bootstrap.php 文件

     /**
     * @return Zend_Application_Module_Autoloader
     */
    protected function _initAutoload()
    {
        $autoloader = new Zend_Application_Module_Autoloader(array('namespace' => '', 'basePath' => APPLICATION_PATH));
        $autoloader->addResourceType('plugin', 'plugins', 'Plugin');
        return $autoloader;
    }
    

    进入 application.ini 文件

    resources.frontController.plugins.auth = Plugin_Layout
    resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
    

    上述插件会根据使用的模块,在“layouts/scripts”中设置名为module.phtml的布局。


    如何在模块中自动加载表单

    将下面两行添加到您的 application.ini 文件中

    resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
    resources.modules[] = ""
    

    为每个模块创建一个 boostrap 文件。该文件名为 Bootstrap.php,应放在模块目录的根目录下,类名应为 {module name}_Boostrap。这个引导文件将导致 zend 框架自动将新的表单目录添加到自动加载器。

    class Admin_Bootstrap extends Zend_Application_Module_Bootstrap {}
    

    将表单类添加到 /forms 目录。登录表单的文件名为 Login.php,类名为 {module name}_Form_Login

    class Admin_Form_Login extends Zend_Form
    

    从同一模块内的控制器文件中调用您的表单

    $form = new Admin_Form_Login();
    

    简单有效! ;)

    【讨论】:

    • 为了更清楚你的答案就是解决方案,请接受你的答案!
    • @KeesSchepers 正是我所做的。几天后我没有收到任何回复,然后我在其他地方找到了答案。然后我想把写作作为一种回应,以便以后可以服务于其他人。我错了吗?
    • @KeesSchepers:我能做到吗?违反规定?
    • 我不确定,但我看到更多的人建议并这样做,因此它认为它是允许的,甚至是建议的。检查这个:blog.stackoverflow.com/2009/01/accept-your-own-answers
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-08
    相关资源
    最近更新 更多