Zend Framework:控制器目录中的子目录

发布于 2024-08-24 11:22:30 字数 571 浏览 4 评论 0原文

我正在为我的网站使用 Zend Framework,并且刚刚创建了一个特殊的模块“api”来创建……嗯,一个 API。

现在,我的模块中有很多控制器,我想在此控制器目录中创建子目录以“整理”它。我的新结构将是这样的:

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

但是,我发现自己完全无法找到使用 Zend_Controller_Router_Route 可以映射到这些控制器的哪种 url 和重定向。是否可以以某种方式做到这一点,或者我应该回到正常结构并将所有控制器放在同一个目录中?


我尝试使用 smack0007 建议的分隔符 _ ,并且考虑到 Zend Framework 通常如何引用子目录,这似乎是合乎逻辑的,但我收到了错误。


编辑:删除了长错误文本,因为它与问题无关,因为这只是一个问题,因为我没有使用正确的大小写,相信我必须将大写字母放在目录的第一个字母中。现在一切正常。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你的往事 2024-08-31 11:22:30

我在 1.5 版本的项目中已经这样做了,但我不知道它是否还能工作。

您必须在控制器前添加“{FOLDER}_”前缀,然后在网址中使用全名。

因此,在您的情况下,您将有一个名为:的控制器

contents_FooController

和一条路线:

/contents_foo/index

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
独孤求败 2024-08-31 11:22:30

我试图在旧应用程序的 url 上实现多个级别,并避免使用大量 url 规则。
所以我考虑将控制器分组到子目录中并为其定义 url 规则。

对于结构

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

在模块的 Bootstrap.php 中我添加了:

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 是

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');
    }

所以这将转发到子目录中的控制器。

  • “$level1”对应于控制器名称,例如,如果$level1 =“other”,则我们转发到Othercontroller.php。
  • “$level2”是操作,例如$level1 =“add”,然后调用addAction()。

最后,添加路由:

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'
)

现在,例如,通过请求 test/default/sub/other/index,您可以在 OtherController.php 中调用 indexAction

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文