Zend Framework : Subdirectories in controllers directory

妖精的绣舞 提交于 2019-12-10 16:45:45

问题


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, 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.


回答1:


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



回答2:


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.

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 with a request test/default/sub/other/index, you can call the indexAction in OtherController.php



来源:https://stackoverflow.com/questions/2401547/zend-framework-subdirectories-in-controllers-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!