[英]How to autoload Zend Framework module models?
I am building a new CMS in Zend Framework and I don't have much exposure to ZF. 我正在Zend Framework中构建一个新的CMS,我没有太多接触ZF。 Client requires two sections called Admin and FE.
客户端需要两个名为Admin和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 . 当我访问http:// site或http:// site / admin等网站时,我的结构工作正常,布局和控制器正在加载。
My question is 1.) How will I autoload my models in modules. 我的问题是1.)我将如何在模块中自动加载我的模型。 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? 2.)我如何为不同的模块使用不同的布局?
Two questions here: 这里有两个问题:
For autoloading models, first make sure that your module bootstrap class extends Zend_Application_Module_Bootstrap
. 对于自动加载模型,首先要确保模块引导类扩展
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). 这将注册包含映射的资源自动加载器,以便名为
Admin_Model_User
的模型类可以存储在文件application/modules/admin/models/User.php
(请注意路径名中的复数模型* s *)。 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_
. IIRC,默认模块使用appnamespace,通常默认为
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
. 因此,例如,默认模块中的用户模型将命名为
Application_Model_User
并存储在文件application/modules/default/models/User.php
。 [If that doesn't work, then try naming Default_Model_User
] [如果这不起作用,那么尝试命名
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.] [但是,如果您真的坚持为您的管理模块设置一个空的appnamespace,并为您的模型添加CPModel的前缀 - 正如您的示例所示 - 那么其中一些会发生变化。
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. 结果是,由于大多数这些文件夹不在include_path上,因此需要在某个时候告诉系统将哪些类前缀与哪些目录关联/映射。
For module-specific layouts, typically I create a front-controller plugin that implements the preDispatch()
hook. 对于特定于模块的布局,通常我会创建一个实现
preDispatch()
挂钩的前端控制器插件 。 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
: 如果您将布局保留在
application/layouts/scripts/
的顶层,那么您的插件可能看起来像存储在application/plugins/Layout.php
的以下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
: 通过
applications/config/application.ini
在您的应用级Bootstrap
注册您的插件:
resources.frontController.plugin.layout = "Application_Plugin_Layout"
or in the app-level Bootstrap
in application/Bootstrap.php
: 或者在
application/Bootstrap.php
中的应用程序级Bootstrap
中:
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
. 然后,例如,您的管理布局可以存储在
application/layouts/scripts/admin.phtml
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.