View Single Post
  #3 (permalink)  
Old 03-03-2008, 12:58 PM
mcleod@spaceweb.nl mcleod@spaceweb.nl is offline
Junior Member
 
Join Date: Mar 2008
Posts: 4
Default

Quote:
Originally Posted by danield View Post
Hello,

I'm a beginner with zf, and I'm trying now to create some controllers in different directorys.

I created an IndexController into the directory /application/backend/controllers/cars. But I don't know how to reach the controller.

I know that it will work fine, if I will create an CarController.php into the directory /application/backend/controllers.

But because I want to create many Controllers, I would like to seperate them in different directorys.

Is there a good way to organize the controllers, and how can I do this?
And how can I call the controllers in subdirectorys?

/application/backend/controllers
AuthController.php
ErrorController.php
IndexController.php

/application/backend/controllers/cars
IndexController.php

/application/backend/index.php
PHP Code:
set_include_path('.' PATH_SEPARATOR '../../library' PATH_SEPARATOR '../../application/backend/models/' PATH_SEPARATOR get_include_path());
require_once 
'Zend/Controller/Front.php';
.
.
.
/**
 * Setup controller
 */
$frontController Zend_Controller_Front::getInstance();
$controller Zend_Controller_Front::getInstance();
$controller->setControllerDirectory('../../application/backend/controllers');
$controller->throwExceptions(true); // should be turned on in development time 
Thanks for help!
The manual has a good section on this (section 7.11). So if you do not understand my reply, please consult the manual.

If you have a module other than the default one and want to set the path to its controller, you can call:
PHP Code:
$controller->addControllerDirectory('path_to_cars_controllers','cars'); 
The index controller for cars should be called Cars_IndexController, so it has to be prefixed with the module name.

Do NOT prefix the filename of the Cars_IndexController, it should be called IndexController.php!

An easy way to avoid have to add directories for all of your modules is to ditch all of the calls to setControllerDirectory (yes, all of them) and to just call:
$controller->addModuleDirectory('your_application_root'); This will work if you use the default directory structure, with all your modules located under the application root, like in myapps/admin, myapps/default, myapps/cars etc.

Bart McLeod

Last edited by mcleod@spaceweb.nl : 03-03-2008 at 09:43 PM.
Reply With Quote