是否可以在 Zend 框架中路由我的简单 URL(不指定模块、控制器和操作)?
我的项目使用 zend 框架,并且需要将路径路由到我想要的位置。例如: 我有一个路径 www.example.com/module/controller/action1,我想在内部路由到 www.example.com/module/controller/action2。
但主要问题是我不想使用必须指定模块、控制器和操作的函数 [$this->_forward('action2', 'controller', 'module');],只是我像这样的:$this->_forward('module/controller/action2');。
如果有人有解决方案,请建议我。我的项目迫切需要。
谢谢, 极土
I am using zend framework for my project and I have a requirement to route the path where I want it. For example:
I have a path www.example.com/module/controller/action1 and I want to internally route in to www.example.com/module/controller/action2.
But the main problem is I do not want to use the function in which I have to specify the module, controller and action [$this->_forward('action2', 'controller', 'module');], simply I something like this: $this->_forward('module/controller/action2');.
Please suggest me if anybody has its solution. its urgent need of my project.
Thanks,
Jitu
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
控制器方法
_forward()
只能接受与您转发的第一个操作位于同一控制器和模块中的操作。因此,在您的
action1Action()
方法中,您只需调用:$this->_forward('action2');
如果您的第二个操作不在同一模块中/控制器,您可以将 Zend_Controller_Action 子类化为您自己的“基本”应用程序控制器,所有其他操作控制器都继承自该控制器(我发现在 ZF 项目上这是一个很好的实践),然后创建另一个名为
_forwardFromUrl()
或其他名称的控制器就像这样,它将您的 URL 分开并将其传递给_forward()
(或者如果您只需要这一额外的东西,则创建一个操作控制器助手)。此示例经过简化,并假设您的
$url
将始终采用module/controller/action
格式:希望有帮助!
The controller method
_forward()
can accept only the action if it is in the same controller and module as the first action you are forwarding from.So in your
action1Action()
method, you can simply call:$this->_forward('action2');
If your second action is not in the same module/controller, you could subclass Zend_Controller_Action into your own "base" application controller that all your other action controllers inherit from (a good practice on ZF projects I find) and then create another one called
_forwardFromUrl()
or something like that, which breaks your URL apart and passes it to_forward()
(or create an action controller helper if you just need this one extra thing).This example is simplified and assumes your
$url
will always be in the formatmodule/controller/action
:Hope that helps!