ZEND 控制器 -- 如何从不同的控制器调用操作
我想显示一个有 2 个表单的页面。顶部表单是该页面独有的,但底部表单已经可以从不同的控制器呈现。我使用以下代码来调用其他表单的操作,但不断收到此错误:
"Message: id is not specified" #0 .../library/Zend/Controller/Router/Rewrite.php(441): Zend_Controller_Router_Route->assemble(Array, true, true)
我的代码:
第一个控制器:
abc_Controller
public function someAction()
{
$this->_helper->actionStack('other','xyz');
}
第二个控制器:
xyz_Controller
public function otherAction()
{
// code
}
所需的结果:
当调用 /abc/some 时,我想渲染“一些”内容与 xyz/其他内容。我认为我正确地遵循了文档(http://framework.zend。 com/manual/en/zend.controller.actionhelpers.html),但找不到任何有关发生该错误的原因的帮助。当我跟踪代码(使用 XDebug)时,xyz/other 操作正常完成,但是当 abc/some 操作到达末尾时,错误会在调度或路由期间的某个地方抛出。
非常感谢任何帮助。
I want to display a page that has 2 forms. The top form is unique to this page, but the bottom form can already be rendered from a different controller. I'm using the following code to call the action of the other form but keep getting this error:
"Message: id is not specified" #0 .../library/Zend/Controller/Router/Rewrite.php(441): Zend_Controller_Router_Route->assemble(Array, true, true)
My code:
First controller:
abc_Controller
public function someAction()
{
$this->_helper->actionStack('other','xyz');
}
Second controller:
xyz_Controller
public function otherAction()
{
// code
}
Desired results:
When calling /abc/some, i want to render the "some" content along with the xyz/other content. I think I followed the doc correctly (http://framework.zend.com/manual/en/zend.controller.actionhelpers.html) but can't find any help on why that error occurs. When I trace the code (using XDebug), the xyz/other action completes ok but when the abc/some action reaches the end, the error is thrown somewhere during the dispatch or the routing.
Any help is greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您可以在 phtml 中的 someAction 中完成此操作。因此,在 some.phtml 中放入
action('other','xyz');?>
这将呈现在 XyzController 的 otherAction 中找到的表单You can accomplish this in your phtml for your someAction. So in some.phtml put
<?php echo $this->action('other','xyz');?>
this will render the form found in the otherAction of XyzController想做这样的事情的冲动表明你正在以完全错误的方式去做这件事。如果您有重复使用内容的冲动,它可能应该属于模型。如果它确实是控制器代码,则应该由操作控制器插件封装
The urge to do something like this is an indication you're going about it in totally the wrong way. If you have the urge to re-use content, it should likely belong in the model. If it is truly controller code it should be encapsulated by an action controller plugin
在 phtml 文件中,您可以使用 $this->action() ;呈现页面,并且该响应将添加到当前响应中。
操作的语法如下:
In phtml file u can use the $this->action() ; to render the page and that response would be added to current response ..
The syntax for action is as follows::
您可以使用第二个控制器创建新对象并调用其方法(但这不是最好的方法)。
您可以使用第二个控制器扩展第一个控制器并调用
$this->methodFromSecond();
- 它也会使用其模板呈现第二个表单。顺便说一句 - 您想在两个控制器中执行什么类型的代码?
You can create new object with second controller and call its method (but it`s not the best way).
You can extend your first controller with the second one and call
$this->methodFromSecond();
- it will render second form too with its template.BTW - what type of code you want to execute in both controllers ?
只是一个更新。该错误与第二个控制器调用操作的方式完全无关。事实证明,在第二个控制器的布局中,有一个单独的 phtml 调用抛出错误 (layout/abc.phtml):
错误行:
我将单独调试它,以免混淆该线程。
感谢 Akeem 和 hsz 的及时回复。我从你的回复中了解到。
总而言之,从外部控制器调用操作有 3 种不同的方法:
希望这可以帮助其他 Zend 菜鸟。
Just an update. The error had absolutely nothing to do with how the action was being called from the second controller. It turns out that in the layout of the second controller, there was a separate phtml call that was throwing the error (layout/abc.phtml):
line of error:
I'll be debugging this separately as not to muddy this thread.
Thanks to Akeem and hsz for the prompt response. I learned from your responses.
To summarize, there were 3 different ways to call an action from an external controller:
Hope this helps other Zend noobs out there.
嗯,我找不到也不知道为什么你需要为一个视图使用不同的控制器。更好的做法是将所有功能都集成到一个控制器中。我在这个例子中使用这个
Hm I can't find and idea why you need to use diffrent Controlers for one view. Better practise is to have all in one Controller. I using this like in this example
这种路由定义:
需要一个参数才能起作用。您可以使用 actionStack 执行此操作,但也可以指定默认 id,以防未提供:
This kind of route definition:
requires a parameter in order to function. You can do this with actionStack but you can also specify a default id in case that none is provided:
对我来说这就像一个魅力
For Me this worked like a charm