在Zra2中传递Laravel 4路由中的默认值

时间:2014-07-15 08:21:12

标签: php laravel-4 routing

在ZF2中,我能够创建一条路线:

'member-login-after-expired-session' => array(
    'type'     => 'segment',
    'options'  =>  array(
        'route'    =>  '/loginAgain',
        'defaults' =>  array(
            'controller' => 'Foo\Controller\Bar',   
            'action'     => 'someMethod',
            'activity'   => 'login',
            'reason'     => 'expired-session',
),),),

可以通过domain.com/loginAgain访问此路线,并传递两个参数"活动"和"原因" 没有他们在网址。我可以通过以下方式在分配的zf2控制器中访问这些参数以及任何其他参数:

$this->params('activity')
$this->params('reason')

如何在Laravel 4.2中实现这一目标?

到目前为止,我在(http://laravel.com/docs/routing)上阅读的文档表明所有参数都必须通过实际网址传递,而这不是我想要的。我试过这个:

Route::get
(
    '/loginAgain',
    array
    (
        'as'        =>  'loginAgain',
        'uses'      =>  'BarController@someMethod',
    ),
    array
    (
        'activity'  =>  'login',
        'reason'    =>  'expired-session',
    )
)

附带控制器:

class BarController extends BaseController
{
    public function someMethod($activity, $reason)
    {
        echo $activity;
        echo $reason;
     ...

然而,我错过了参数错误。我究竟应该做什么工匠?

1 个答案:

答案 0 :(得分:0)

如果我理解正确 - 这会有用

Route::get('/login/{param1}/{param2}', ['uses' => 'BarController @someMethod']);
Route::get('/login',  ['uses' => 'BarController @otherMethod']);

然后在你的控制器中我会做

class BarController extends BaseController
{
    public function someMethod($activity, $reason)
    {
        echo $activity;
        echo $reason;
        ...
    }


    public function otherMethod()
    {
        return $this->someMethod('default1', 'default2');
    }

或者这可能有效(未经测试)

Route::get('/login/{param1}/{param2}', ['uses' => 'BarController @someMethod']);
Route::get('/login',  ['uses' => 'BarController @someMethod']);

然后在你的控制器中

class BarController extends BaseController
{
    public function someMethod($activity = 'default1', $reason = 'default2')
    {
        echo $activity;
        echo $reason;
        ...
    }