zoukankan      html  css  js  c++  java
  • Modules

    Modules

    zend-mvc uses a module system to organise your main application-specific code within each module. The Application module provided by the skeleton is used to provide bootstrapping, error, and routing configuration to the whole application. It is usually used to provide application level controllers for the home page of an application, but we are not going to use the default one provided in this tutorial as we want our album list to be the home page, which will live in our own module.

    We are going to put all our code into the Album module which will contain our controllers, models, forms and views, along with configuration. We’ll also tweak the Application module as required.

    Let’s start with the directories required.

    Setting up the Album module

    Start by creating a directory called Album under module with the following subdirectories to hold the module’s files:

    zf2-tutorial/
        /module
            /Album
                /config
                /src
                    /Controller
                    /Form
                    /Model
                /view
                    /album
                        /album

    The Album module has separate directories for the different types of files we will have. The PHP files that contain classes within the Album namespace live in the src/ directory. The view directory also has a sub-folder called album for our module's view scripts.

    In order to load and configure a module, Zend Framework provides aModuleManager. This will look for a Module class in the specified module namespace (i.e., Album); in the case of our new module, that means the classAlbumModule, which will be found in module/Album/src/Module.php.

    Let's create that file now, with the following contents:

    namespace Album;
    
    use ZendModuleManagerFeatureConfigProviderInterface;
    
    class Module implements ConfigProviderInterface
    {
        public function getConfig()
        {
            return include __DIR__ . '/../config/module.config.php';
        }
    }

    The ModuleManager will call getConfig() automatically for us.

    Autoloading

    While Zend Framework provides autoloading capabilities via its zend-loadercomponent, we recommend using Composer's autoloading capabilities. As such, we need to inform Composer of our new namespace, and where its files live.

    Open composer.json in your project root, and look for the autoload section; it should look like the following by default:

    "autoload": {
        "psr-4": {
            "Application\": "module/Application/src/"
        }
    },

    We'll now add our new module to the list, so it now reads:

    "autoload": {
        "psr-4": {
            "Application\": "module/Application/src/",
            "Album\": "module/Album/src/"
        }
    },

    Once you've made that change, run the following to ensure Composer updates its autoloading rules:

    $ composer dump-autoload

    Configuration

    Having registered the autoloader, let’s have a quick look at the getConfig()method in AlbumModule. This method loads the config/module.config.php file under the module's root directory.

    Create a file called module.config.php underzf2-tutorial/module/Album/config/:

    namespace Album;
    
    use ZendServiceManagerFactoryInvokableFactory;
    
    return [
        'controllers' => [
            'factories' => [
                ControllerAlbumController::class => InvokableFactory::class,
            ],
        ],
        'view_manager' => [
            'template_path_stack' => [
                'album' => __DIR__ . '/../view',
            ],
        ],
    ];

    The config information is passed to the relevant components by theServiceManager. We need two initial sections: controllers and view_manager. The controllers section provides a list of all the controllers provided by the module. We will need one controller, AlbumController; we'll reference it by its fully qualified class name, and use the zend-servicemanager InvokableFactoryto create instances of it.

    Within the view_manager section, we add our view directory to theTemplatePathStack configuration. This will allow it to find the view scripts for the Album module that are stored in our view/ directory.

    Informing the application about our new module

    We now need to tell the ModuleManager that this new module exists. This is done in the application’s config/modules.config.php file which is provided by the skeleton application. Update this file so that the array it returns contains theAlbum module as well, so the file now looks like this:

    (Changes required are highlighted using comments; original comments from the file are omitted for brevity.)

    return [
        'ZendForm',
        'ZendDb',
        'ZendRouter',
        'ZendValidator',
        'Application',
        'Album',          // <-- Add this line
    ];

    As you can see, we have added our Album module into the list of modules after the Application module.

    We have now set up the module ready for putting our custom code into it.

  • 相关阅读:
    如何从零开始创建一个IT信息系统
    Linux常用命令
    vue.js 3.2.20: 用rem实现移动端和pc的兼容
    vue.js3.2.6:路由处理404报错(vue-router@4.0.11)
    vue.js项目在nginx上部署:使spring后端记录真实ip地址
    vue.js 3.0.5:用vue-i18n开发i18n国际化功能(vue-i18n@9.2.0)
    前台项目基础框架之spring boot后端(spring boot v2.5.4)
    前台项目基础框架之vue前端(vue@3.2.6)
    intellij idea 2021.2:为一个spring boot项目改名
    git:修改项目的remote地址(git version 2.30.2)
  • 原文地址:https://www.cnblogs.com/chunguang/p/5642704.html
Copyright © 2011-2022 走看看