如何自动加载 Zend Framework 模块模型? [英] How to autoload Zend Framework module models?

查看:25
本文介绍了如何自动加载 Zend Framework 模块模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Zend Framework 中构建一个新的 CMS,我对 ZF 的接触不多.客户端需要名为 Admin 和 FE 的两个部分.所以,我的应用程序结构如下.

<前>- 地点- 应用---- 配置---- 布局---- 模块- - - - 默认------------ 控制器- - - - - - 形式- - - - - - 楷模------------ 意见------------ Bootstrap.php- - - - 行政------------ 控制器- - - - - - 形式- - - - - - 楷模------------ 意见------------ Bootstrap.php---- Bootstrap.php- 民众- 图书馆-- index.php

当我访问像 http://site 这样的网站时,我的结构工作正常,并且布局和控制器正在加载="http://site/admin" rel="nofollow">http://site/admin.

我的问题是1.) 我将如何在模块中自动加载我的模型.在特定于模型的引导程序文件中,我添加了以下代码.但它不起作用.

class Admin_Bootstrap 扩展 Zend_Application_Module_Bootstrap{受保护的函数 _initAutoload(){$autoloader = new Zend_Application_Module_Autoloader(array('basePath' =>APPLICATION_PATH.'/modules/admin/','命名空间' =>'','资源类型' =>大批('形式' =>大批('路径' =>'形式/','命名空间' =>'形式_',),'模型' =>大批('路径' =>'楷模/','命名空间' =>'CPModel_')),));返回 $autoloader;}}

2.) 我将如何为不同的模块使用不同的布局?

解决方案

这里有两个问题:

  1. 自动加载模型
  2. 特定于模块的布局

对于自动加载模型,首先确保您的模块引导程序类扩展了 Zend_Application_Module_Bootstrap.这将注册一个包含映射的资源自动加载器,以便可以将名为 Admin_Model_User 的模型类存储在文件 application/modules/admin/models/User.php 中(注意路径名中的复数模型*s*).对于您在上面描述的用法,您似乎不需要自己定义任何此类映射.

有一些与默认模块相关的技巧.IIRC,默认模块使用 appnamespace,通常默认为 Application_.因此,例如,默认模块中的用户模型将命名为 Application_Model_User 并存储在文件 application/modules/default/models/User.php 中.[如果这不起作用,请尝试命名 Default_Model_User]

[但是,如果你真的坚持为你的管理模块使用一个空的应用程序命名空间,并为你的模型坚持一个 CPModel 的前缀 - 正如你的例子所暗示的 - 那么其中的一些变化.]

结果是,由于这些文件夹中的大多数不在 include_path 中,因此需要在某些时候告诉系统哪些类前缀将哪些类前缀与哪些目录关联/映射.

对于特定于模块的布局,通常我创建一个 front- 实现 preDispatch() 钩子的控制器插件.如果您将布局保持在 application/layouts/scripts/ 中的顶层,那么您的插件可能类似于存储在 application/plugins/Layout.php 中的以下内容:

class Application_Plugin_Layout 扩展 Zend_Controller_Plugin_Abstract{公共函数 preDispatch(Zend_Controller_Request_Abstract $request){Zend_Layout::getMvcInstance()->setLayout($request->getModuleName());}}

通过applications/config/application.ini在您的应用级Bootstrap中注册您的插件:

resources.frontController.plugin.layout = "Application_Plugin_Layout"

或在application/Bootstrap.php中的应用级Bootstrap:

受保护的函数 _initPlugins(){$this->bootstrap('frontController');$front = $this->getResource('frontController');$front->registerPlugin(new Application_Plugin_Layout());}

然后,例如,您的管理布局可以存储在 application/layouts/scripts/admin.phtml 中.

I am building a new CMS in Zend Framework and I don't have much exposure to ZF. Client requires two sections called Admin and FE. So, I have structured my application structure as follows.

- SITE
-- application
---- configs
---- layouts
---- modules
-------- default
------------ controllers
------------ forms
------------ models
------------ views
------------ Bootstrap.php
-------- admin
------------ controllers
------------ forms
------------ models
------------ views
------------ Bootstrap.php
---- Bootstrap.php
-- public
-- library
-- index.php

My structure is working fine and layouts and controllers are loading when I am accessing site like http://site or http://site/admin.

My question is 1.) How will I autoload my models in modules. In the model specific bootstrap file I have added below code. But it is not working.

class Admin_Bootstrap extends Zend_Application_Module_Bootstrap 
{
    protected function _initAutoload()
    {
        $autoloader = new Zend_Application_Module_Autoloader(array(
            'basePath' => APPLICATION_PATH.'/modules/admin/',
            'namespace' => '',
            'resourceTypes' => array(
                'form' => array(
                    'path' => 'forms/',
                    'namespace' => 'Form_',
                ),
                'model' => array(
                    'path' => 'models/',
                    'namespace' => 'CPModel_'
                )
            ),
        ));
        return $autoloader;
    }
}

2.) How will I use different layouts for different module?

解决方案

Two questions here:

  1. Autoloading models
  2. Module-specific layout

For autoloading models, first make sure that your module bootstrap class extends Zend_Application_Module_Bootstrap. This will register a resource autoloader that includes a mapping so that a model class named Admin_Model_User can be stored in the file application/modules/admin/models/User.php (note the plural model*s* in the path name). For the usage you describe above, it does not appear that you need to define any such mappings yourself.

There is a bit of trickiness associated to the default module. IIRC, the default module uses the appnamespace, typically defaulting to Application_. So, for example, a user model in the default module would be named Application_Model_User and stored in the file application/modules/default/models/User.php. [If that doesn't work, then try naming Default_Model_User]

[However, if you really insist on an empty appnamespace for your admin module and a prefix of CPModel for your models - as your example suggests - then some of this changes.]

The upshot is that since most of these folders are not on the include_path, the system needs to be told at some point what class prefixes to associate/map with what directories.

For module-specific layouts, typically I create a front-controller plugin that implements the preDispatch() hook. If you keep your layouts at the top-level in application/layouts/scripts/, then your plugin can look something like the following stored in application/plugins/Layout.php:

class Application_Plugin_Layout extends Zend_Controller_Plugin_Abstract
{

    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        Zend_Layout::getMvcInstance()->setLayout($request->getModuleName());
    }
}

Register your plugin in your app-level Bootstrap, either via applications/config/application.ini:

resources.frontController.plugin.layout = "Application_Plugin_Layout"

or in the app-level Bootstrap in application/Bootstrap.php:

protected function _initPlugins()
{
    $this->bootstrap('frontController');
    $front = $this->getResource('frontController');
    $front->registerPlugin(new Application_Plugin_Layout());
}

Then, for example, your admin layout could be stored in application/layouts/scripts/admin.phtml.

这篇关于如何自动加载 Zend Framework 模块模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆