如何将参数值传递给 ZF2 中的另一个控制器动作

How to pass the paramter values to another controller's action in ZF2

大家好,我是 zf2 的新手。我正在控制器中创建一个动作,一切正常。当表单有效时,我想将一些值传递给重定向页面。我怎样才能做到这一点?请帮帮我。

我的控制器动作是

public function studenteditAction(){

      $id = (int) $this->params()->fromRoute('id', 0);

     if (!$id) {
         return $this->redirect()->toRoute('manager', array(
             'action' => 'students'
         ));
     }
     $form  = new StudentsForm();

     $request = $this->getRequest();
     if ($request->isPost()) {

         $students = new Students();

         $form->setData($request->getPost());

         if ($form->isValid()) {


            $students->exchangeArray($form->getData());

              $table = $this->getServiceLocator()->get('Students\Model\StudentsTable');

              $table->profileStudents($students);

             return $this->redirect()->toRoute('manager',array('controller'=>'manager','action'=>'student-view','id'=>$id,'status' => 'profile-ready'));


         }
     }

     return array(
         'id' => $id,
         'form' => $form,
     );
 }

我无法在控制器视图中获取传递的状态值。 'status' => 'profile-ready'

提前致谢

您需要修改路由配置以接受 'id' 和 'status' 值。

       'manager' => array(
            'type' => 'Segment',
            'options' => array(
                'route'    => '/manager[/:id][/:status]',
                'defaults' => array(
                    'controller' => 'Application\Controller\Manager',
                    'action'     => 'studentView',
                ),
                'constraints' => array(
                    'id' => '[0-9]*',
                    'status' => '[a-z-]*'
                ),
            ),
        ),

控制器:

public function studentViewAction()
{
    $id = $this->params()->fromRoute('id');
    $status = $this->params()->fromRoute('status');
    return new ViewModel(array('id' => $id, 'status' => $status));
}

查看

<?php 
echo $this->id;
echo $this->status;
?>

这就是我将所需值传递给另一个控制器的操作的方式