zend框架2:如何将参数从一个控制器传递到另一个控制器

时间:2014-01-15 20:20:23

标签: php zend-framework2

有一个类似的线程,但它没有解决这个确切的问题,所以我想我会创建一个新线程,以明确这一点。

解释代码/问题:

这是我的SystemsController。

这里我有viewsystemAction()。这是加载的,从视图中调用脚本来加载ajaxviewsystemAction()。

在viewsystemAction()中,我使用params函数从我的路径中获取“id”参数。例如,systems / viewsystems / 222(< - 222是id。)。我回应这个只是为了表明它是正确的,它为页面提供了正确的ID。

ajaxviewsystemsAction就是问题所在。这是因为ajaxviewsystemsAction id的路由与viewsystemsAction不同。如果我在这里回显了参数中的id,则显示为0.

虽然如果我在浏览器中访问此页面,例如ajaxviewsystemsAction / 222,那么它的工作正常。但是,除了ajax表之外没有任何内容呈现,因为记住它是在一个脚本的viewsystemAction中调用的。

我需要做的是,以某种方式将来自viewsystemAction的id路由值传递给ajaxviewsystemAction,以使用相同的路由id。

当有人点击链接时,会加载viewsystems / 222,在viewsystemsAction和ajaxviewsystemsAction中执行正确的id。

这可能吗?如果不是,我怎么能做这样类似的东西。我正在使用集成了ajax的zftable。我需要将此id参数传递给查询。

   private function getSourceViewAllSystems($id)
    {   
         return $this->getSystemsTable()->fetchViewAllSystems($id); //paramater to model which executes sql ->where system = $id
    }

   public function viewsystemAction()
    {
        $id = (int) $this->params()->fromRoute('id', 0);
        echo $id; //i see the correct id for example 220 from the route in the browser
    }

   public function ajaxviewsystemAction()

    {
       $id = (int) $this->params()->fromRoute('id', 0);
       echo $id; //to see the id of the route with the ajax page
       //displays 0 and not the route id from the viewsystemAction

        $table = new TableExample\Advance();
        $table->setAdapter($this->getDbAdapter())
                ->setSource($this->getSourceViewAllSystems($id))
                ->setParamAdapter($this->getRequest()->getPost())
        ;
        return $this->htmlResponse($table->render('custom' , 'custom-b2'));

    }

---更新---

    <?php

return array(
     'controllers' => array(
         'invokables' => array(
             'Systems\Controller\Systems' => 'Systems\Controller\SystemsController',
         ),
     ),


          // The following section is new and should be added to your file
     'router' => array(
         'routes' => array(
             'systems' => array(
                 'type'    => 'segment',
                 'options' => array(
                     'route'    => '/systems[/][:action][/:id]',
                     'constraints' => array(
                         'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                         'id'     => '[0-9]+',
                     ),
                     'defaults' => array(
                         'controller' => 'Systems\Controller\Systems',
                         'action'     => 'index',
                     ),
                 ),
             ),
         ),
     ),

     'view_manager' => array(
         'template_path_stack' => array(
             'systems' => __DIR__ . '/../view',
         ),
     ),
 );

0 个答案:

没有答案