开发者

Zend Framework : Subdirectories in controllers directory

I'm using the Zend Framework for my website, and just created a special module "api" to create... Well, an API.

Now, I have a lot of controllers in my module and I'd like to create subdirectories in this controllers directory in order to "tidy" it. My new structure would be something like this :

 - controllers/
 - controllers/contents/[controllers]
 - controllers/users/[controllers]
 - controllers/misc/[controllers]

However开发者_StackOverflow, I find myself totally unable to find what kind of urls and redirections using Zend_Controller_Router_Route could map to these controllers. Is it possible to do this somehow or should I just go back to the normal structure and put all my controllers in the same directory ?


I tried using the separators _ as suggested by smack0007 and as it seemed logical given how Zend Framework usually refers to subdirectories, but I got an error.


Edit : Removed the long error text as it was not related to the question since it was only a problem because I didn't use the propre case, believing I had to put an uppercase to the first letter of the directory. All works well now.


I've done this in project back in the 1.5 version but I don't know if it will work anymore.

You have to prefix your controllers with "{FOLDER}_" and then use the full name in the url.

So in your case you would have a controller named:

contents_FooController

and a route:

/contents_foo/index


I was trying to accomplish multiple levels at the url for an old application and avoiding to use a lot of url roules. So I thought about grouping controllers into subdirectories and defining a url roule for it.

For a structure

modules
 --test
   --controllers
     --sub
        -- OtherController.php
     --DefaultController.php

In the Bootstrap.php of the module I added:

public function __construct($application)
    {
        parent::__construct($application);
        $frontController = Zend_Controller_Front::getInstance();
        $frontController->addControllerDirectory(__DIR__ . '/controllers',
            'test');
        $frontController->addControllerDirectory(__DIR__ . '/controllers/sub',
            'test_sub');
    }

DefaultController.php is

class Test_DefaultController extends Zend_Controller_Action {
     public function subAction()
    {
         $level1 = $this->getRequest()->getParam('level1');
         $level2 = $this->getRequest()->getParam('level2');
         return $this->_forward($level2, $level1, 'test_sub');
    }

So this will forward to our controller in the sub directory.

  • "$level1" corresponds to the Controller name, e.g. if $level1 = "other", then we forward to Othercontroller.php
  • "$level2" is the action, e.g. $level1 = "add" then addAction() is called.

Finally, added the route:

new Zend_Controller_Router_Route_Regex('([a-z-]+)/([a-z-]+)/([a-z-]+)/([a-z-]+)/([a-z-]+)',
    array(),
    array(1 => 'module', 2 => 'controller', 3 => 'action', 4 => 'level1', 5 => 'level2'),
    '%s/%s/%s/%s/%s'
)

Now e.g. with a request test/default/sub/other/index, you can call the indexAction in OtherController.php

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜