2017-09-28 91 views
0

我想可选的参数添加到我家的路线是可选参数,基本路线添加在Laravel

Route::get('/{selectDate?}', [ 
    "as" => "/", 
    "uses" => "[email protected]" 
]); 

和我的索引功能就像

public function index($date = null) 
    { 
     if($date!=null){ 
     //some line of codes 
     } 

    } 

和我引发的可选参数在切换功能的选择列表是

$(function() { 
      $('#selectDate').on('change', function() { 
       window.location.href = '{{route("/")}}' + "/" + this.value; 
      }); 
}); 

现在的问题是这样的,当我打任何其他链接从主页他们都在索引功能不在自己的书面功能。 需要有关此问题的帮助。

+0

请告诉我们你的'路径/文件web.php' – Sebastian

+0

@Sebastian'路线/ web.php'已经第一块 –

+0

上公布@MayankPandeyz这是只有一个路由。我想看到整个文件。 – Sebastian

回答

1

任何字符串都会匹配一个可选参数,有2个选项可以解决这个问题。

您可以将此路线放在您的web.php路径文件的底部,路线将在先来先服务的基础上进行匹配,因此只有在没有其他路线与给定网址匹配的情况下才会落入此路线。

你的第二个选择是使用正则表达式来定义其中可以找到更多信息的可选参数here。 总之你可以使用

Route::get('/{selectDate?}', [ 
    "as" => "/", 
    "uses" => "[email protected]" 
])->where('selectDate', '/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})Z$/'); 

这个selectDate后应该总是一个模式相匹配的01-01-2000
正则表达式未经检验和this复制SO回答

1

在这种情况下,你必须使用Regular Expression Constraints等以检查路由参数:

Route::get('{user_tipe}/event', 'admin\[email protected]')->where('user_tipe', 'admin'); 

其中路由将匹配admin,如果匹配,那么具体路线将工作,否则不行。

+0

这是有帮助的 –