【问题标题】:How to combine these two routes?如何结合这两条路线?
【发布时间】:2015-09-13 19:21:21
【问题描述】:

我有这两条路线:

Route::post('/post',
    array(
        'uses' => 'PostController@newPost'
    )
);

Route::post('/post/picture',
    array(
        'uses' => 'PostController@newPost'
    )
);

在实际的控制器中,我区分了这两个参数(因为它们使用的是同一个控制器),但是如何将上面的两个路由结合起来呢?

【问题讨论】:

    标签: laravel laravel-4 routing laravel-5


    【解决方案1】:

    试试下面的

    Route::post('/post/{picture?}',
        array(
            'uses' => 'PostController@newPost'
        )
    );
    

    picture 包装在{} 括号中会将URL 段视为参数。在参数末尾使用? 将其视为可选。一个警告:这也会捕捉到像

    /post/some-other-thing
    

    如果您担心抓到其他物品,请尝试以下方法

    Route::post('/post/{myvar?}',
        array(
            'uses' => 'PostController@newPost'
        )
    )->where('myvar','picture');;
    

    where 方法的第二个参数可以是任何 PCRE 正则表达式。

    【讨论】:

    • 如果picture 需要与picture 完全一致怎么办?您的路线允许 url 中的任何内容,例如 /post/hey/post/hello。我只需要/post/picture
    • 你更新的路由没有链接到控制器。
    猜你喜欢
    • 2021-09-21
    • 1970-01-01
    • 1970-01-01
    • 2013-08-14
    • 2015-09-07
    • 1970-01-01
    • 2017-06-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多