Zend Framework v2 のリダイレクトに URL パラメーターを設定する
-
11-12-2019 - |
質問
コントローラーに次のリダイレクトスクリプトがあります(Zend Framework 2)
return $this->redirect()->toRoute('default', array(
'controller' => 'admin',
'action' => 'index'
));
現在リダイレクトしています localhost/zf2/public/admin/index
追加のパラメータを使用してリダイレクトするにはどうすればよいですか?
のように:
localhost/zf2/public/admin/index/update/1
または localhost/zf2/public/admin/index/page/2
私はこれを試してみました:
return $this->redirect()->toRoute('default', array(
'controller' => 'admin',
'action' => 'index'
'param' => 'updated/1'
));
しかし、にリダイレクトされます localhost/ttacounting/public/admin/index/updated%2F1
解決
これは動作例です。ルートスクリプト
$this->redirect()->toRoute('myaccount', array(
'controller' => 'admin',
'action' => 'index',
'param1' =>'updated',
'param2'=>'1'
));
次に、module.config.phpにパラメータを設定します。
'myaccount' => array(
'type' => 'Segment',
'options' => array(
'route' => '/myaccount[/:action][/:param1][/:param2]',
'defaults' => array(
'controller' => 'Main\Controller\MyAccount',
'action' => 'index',
),
),
),
これにより、MyAccountController、param1='updated' および param2='1' の IndexAction が表示されます。ただし、例の場合、アクションはパラメータ名「update」とパラメータ値「1」で更新する必要があります。
他のヒント
これは私にとってはうまくいきます。
ルート:
'user_view' => array(
'type' => 'Segment',
'options' => array(
'route' => '/user/view[/:user_id]',
'defaults' => array(
'controller' => 'user',
'action' => 'viewUser',
),
),
), // End of user_view route
そしてコントローラーからのリダイレクト:
return $this->redirect()->toRoute('user_view', array('user_id'=>$user_id));
リダイレクト ステートメントの Array キーがルート セグメントに対応していることに注意してください。[/:user_id] = 'user_id'=>$user_id
このステートメントは、localhost/leads/edit/112345 にリダイレクトします。
return $this->redirect()->toRoute('leads', array(
'action' => 'edit',
'id' => 112345
));
id は editAction で期待されるパラメータです。
それが役に立てば幸い。
別の方法は、ルートを更新する代わりに、それを 3 番目のパラメーターとして渡すことです (ZF3 でテスト済み)。
$this->redirect()->toRoute('dashboard', [], ['query' => ['welcome' => 1]]);
上記の解決策をいくつか試しましたが、どれもうまくいきませんでした。以下のものと一緒に来ました。
//Return to specific product pricing
return $this->redirect()->toRoute('showpricing', array('id' => $form_values['id']));
/* show pricing */
'showpricing' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/product/showpricing[?id=:id]',
'defaults' => array(
'controller' => 'Product\Controller\Product',
'action' => 'showpricing',
)
)
),
これが誰かの役に立つことを願っています。
前のルートの代わりにこのルートを試してください
return $this->redirect()->toRoute('default', [
'controller' => 'admin',
'action' => 'index'
'updated' => '1'
]);