Zend Framework + Doctrine1.2 project structure wit

2019-07-18 10:53发布

问题:

applications coupled with the realization ZendFramework + Doctrine 1.2 for some time, but now they have their first experience with the use of more modules. I requested a default module is visible to everyone with a certain layout and an admin module with a different layout.

So far in my applications I have always used the following structure:

/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

So my question is: how should the structure of my application to do what is required?

I tried using zend tool and see what kind of structure I created the command:

zf create module admin

course after launching

zf create project app

I noticed that the create command module I created a folder modules in application. Into modules created admin and inside it has created controllers, models and views.

So in addition to separating means zf controller and view correctly, but also models. But my doctrine creates all the models on the one hand! :D

How can I do to use templates created by doctrine for each module?

Then how do I assign a new layout for the admin module?

For the party guest you advise me to leave the facility that currently use or move it all in a form default?

Sorry if I made a lot of questions, maybe too much, but I am really very confused about it!

Thanks

回答1:

After a thorough documentation I've found the right project structure

/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

To view a different layout according to where you are module I used the following plugins:

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;
        }
    }
}

into Bootstrap.php file

 /**
 * @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;
}

into application.ini file

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

the plugin described above according to the module in use will set the layout with the name of module.phtml within "layouts/scripts".


How to Autoload forms within a module

add the two below lines to your application.ini file

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

Create a boostrap file for each module. The file, named Bootstrap.php, should be placed in the root of the module directory and the class name should be {module name}_Boostrap. This bootstrap file will cause zend framework to automatically add the new forms directory to the autoloader.

class Admin_Bootstrap extends Zend_Application_Module_Bootstrap {}

Add for form class to the /forms directory. A login form would have a filename of Login.php and a class name of {module name}_Form_Login

class Admin_Form_Login extends Zend_Form

Call your form from a controller file from within the same module

$form = new Admin_Form_Login();

Simple and effective! ;)