【问题标题】:Laravel: how to limit some route parameter to specific values?Laravel:如何将某些路由参数限制为特定值?
【发布时间】:2017-11-27 17:30:15
【问题描述】:

是否可以使route 仅接受特定字符串的字符串参数?示例:

Route::get('{user_tipe}/event', 'admin\EventC@index');

那条路线,我想让user_tipe 参数只允许两个字符串,如adminauthor。这可能吗?

【问题讨论】:

    标签: php laravel laravel-5 laravel-routing


    【解决方案1】:

    您可以在您的路线上使用regular expression constraits 来做到这一点:

    Route::get('{user_tipe}/event', 'admin\EventC@index')->where('user_tipe', 'admin|author');
    

    admin|author 是一个简单的正则表达式,它将匹配字符串adminauthor

    更新

    Here you can find使用Route::group时如何使用路由参数约束

    【讨论】:

    • 很好,路由的正则表达式是最好的方法
    • 谢谢@moppo。我在Route::group(['prefix' => '/{lang}'], function(){ //Routes for en and fr goes here. })->where('lang', 'en|fr'); 上尝试了相同的方法,但我收到了这个错误Call to a member function where() on null。请问有什么办法可以解决我的担心吗?
    猜你喜欢
    • 2021-04-03
    • 1970-01-01
    • 1970-01-01
    • 2020-01-17
    • 2012-06-18
    • 1970-01-01
    • 1970-01-01
    • 2018-12-20
    • 2015-03-12
    相关资源
    最近更新 更多