【问题标题】:Laravel routes accepts only specific formatLaravel 路由只接受特定格式
【发布时间】:2015-12-28 21:14:51
【问题描述】:

我正在使用 laravel 5.1,我想知道我是否只能在我的 routes.php 中允许特定的路由格式?

路由格式应如下所示(严格):

http://example.com/archive/2015-09

其中 2015-09 为年月。它应该只接受这种格式(如上所述)。具有不同的格式只会重定向到主页。示例:

http://example.com/archive/2015
http://example.com/archive/asd
http://example.com/archive/2015-9
http://example.com/archive/2
http://example.com/archive/2015-09-01

所以在我的路线中我有这个:

Route::get('archive/{date}', 'ArchiveController@index');

我在文档中看到了 regular expressions could be used,但我不确定我该怎么做。

【问题讨论】:

    标签: php regex laravel laravel-4 laravel-5


    【解决方案1】:

    这就是你想要的:

    Route::get('archive/{date}', function(){
        // Your code
    })->where(['date' => '[0-9]{4}-[0-9]{2}']);;
    

    这将匹配...

    • 从 0 到 9 的数字,正好是四次
    • 后跟连字符-
    • 后跟从 0 到 9 的数字,正好是两次

    请注意,这并不能保证日期没有像“9999-99”这样的错误,但您可以在控制器中进行这些检查。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-01
      • 1970-01-01
      • 2019-06-25
      • 1970-01-01
      • 2018-12-04
      • 2016-04-10
      • 1970-01-01
      • 2013-03-28
      相关资源
      最近更新 更多