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:
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 namedAdmin_Model_User
can be stored in the fileapplication/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 namedApplication_Model_User
and stored in the fileapplication/modules/default/models/User.php
. [If that doesn't work, then try namingDefault_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 inapplication/layouts/scripts/
, then your plugin can look something like the following stored inapplication/plugins/Layout.php
:Register your plugin in your app-level
Bootstrap
, either viaapplications/config/application.ini
:or in the app-level
Bootstrap
inapplication/Bootstrap.php
:Then, for example, your admin layout could be stored in
application/layouts/scripts/admin.phtml
.