ZEND 控制器 -- 如何从不同的控制器调用操作

发布于 2024-08-14 02:12:11 字数 885 浏览 9 评论 0原文

我想显示一个有 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 技术交流群。

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

发布评论

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

评论(8

原谅我要高飞 2024-08-21 02:12:11

您可以在 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

缱倦旧时光 2024-08-21 02:12:11

想做这样的事情的冲动表明你正在以完全错误的方式去做这件事。如果您有重复使用内容的冲动,它可能应该属于模型。如果它确实是控制器代码,则应该由操作控制器插件封装

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

悲念泪 2024-08-21 02:12:11

在 phtml 文件中,您可以使用 $this->action() ;呈现页面,并且该响应将添加到当前响应中。

操作的语法如下:

public function action($action, $controller, $module = null, array $params = array())

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::

public function action($action, $controller, $module = null, array $params = array())
那请放手 2024-08-21 02:12:11

您可以使用第二个控制器创建新对象并调用其方法(但这不是最好的方法)。

您可以使用第二个控制器扩展第一个控制器并调用 $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 ?

请持续率性 2024-08-21 02:12:11

只是一个更新。该错误与第二个控制器调用操作的方式完全无关。事实证明,在第二个控制器的布局中,有一个单独的 phtml 调用抛出错误 (layout/abc.phtml):

<?php echo $this->render('userNavigation.phtml') ?>

错误行:

echo $this->navigation()->menu()->renderMenu(...)

我将单独调试它,以免混淆该线程。

感谢 Akeem 和 hsz 的及时回复。我从你的回复中了解到。

总而言之,从外部控制器调用操作有 3 种不同的方法:

  1. 从第一个控制器实例化第二个控制器并调用操作。
  2. 使用 $this->_helper->actionStack
  3. 在第一个控制器的 phtml 中,action('other','xyz');?> (正如 Akeem 上面指出的)

希望这可以帮助其他 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):

<?php echo $this->render('userNavigation.phtml') ?>

line of error:

echo $this->navigation()->menu()->renderMenu(...)

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:

  1. Instantiate the second controller from the first controller and call the action.
  2. Use $this->_helper->actionStack
  3. In the phtml of the first controller, action('other','xyz');?> (as Akeem pointed out above)

Hope this helps other Zend noobs out there.

不交电费瞎发啥光 2024-08-21 02:12:11

嗯,我找不到也不知道为什么你需要为一个视图使用不同的控制器。更好的做法是将所有功能都集成到一个控制器中。我在这个例子中使用这个

DemoController extends My_Controller_Action() {
 ....
 public function indexAction() {
   $this->view->oForm = new Form_Registration();
 }
}

My_Controller_Action extends Zend_Controller_Action() {
   public function init() {
      parent::init();
      $this->setGeneralStuf();
   }

   public function setGeneralStuf() {
       $this->view->oLoginForm = new Form_Login();
   }
}

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

DemoController extends My_Controller_Action() {
 ....
 public function indexAction() {
   $this->view->oForm = new Form_Registration();
 }
}

My_Controller_Action extends Zend_Controller_Action() {
   public function init() {
      parent::init();
      $this->setGeneralStuf();
   }

   public function setGeneralStuf() {
       $this->view->oLoginForm = new Form_Login();
   }
}
梦里兽 2024-08-21 02:12:11

这种路由定义:

routes.abc.route = "abc/buy/:id/*" 
routes.abc.defaults.controller = "deal" 
routes.abc.defaults.action = "buy" 
routes.abc.reqs.id = "\d+"

需要一个参数才能起作用。您可以使用 actionStack 执行此操作,但也可以指定默认 id,以防未提供:

$this->_helper->actionStack('Action',
                            'Controller',
                            'Route',
                            array('param' => 'value')
);


routes.abc.defaults.id = "1" 

This kind of route definition:

routes.abc.route = "abc/buy/:id/*" 
routes.abc.defaults.controller = "deal" 
routes.abc.defaults.action = "buy" 
routes.abc.reqs.id = "\d+"

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:

$this->_helper->actionStack('Action',
                            'Controller',
                            'Route',
                            array('param' => 'value')
);

routes.abc.defaults.id = "1" 
流年已逝 2024-08-21 02:12:11

对我来说这就像一个魅力

    class abcController extends Zend_Controller_Action
    {
        public function dashBoardAction()
        {
            $this->_helper->actionStack('list-User-Data', 'xyz');
        }
    }

    class XyzController extends Zend_Controller_Action {
        public function listUserDataAction()
        {
            $data = array('red','green','blue','yellow');
            return $data;
        }
    }

For Me this worked like a charm

    class abcController extends Zend_Controller_Action
    {
        public function dashBoardAction()
        {
            $this->_helper->actionStack('list-User-Data', 'xyz');
        }
    }

    class XyzController extends Zend_Controller_Action {
        public function listUserDataAction()
        {
            $data = array('red','green','blue','yellow');
            return $data;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文