Laravel 具有多个可选值的深度路由
Laravel deep routing with multiple optional values
我正在寻找解决这个问题的最佳方法。
我的表单 post 是一个简单的搜索,但是有多个可选值。然后控制器将 post 重定向到更易于阅读的 URL。
例如
startdate=2018-01-01
enddate=2018-01-31
department=2,4
这将创建一个 URL
/2018-01-01/2018-01-31/2,4/
但如果他们也按员工搜索,那么 return 以下
employee=9,5,1
/2018-01-01/2018-01-31/2,4/9,5,1/
他们也可以不搜索部门而只搜索员工
/2018-01-01/2018-01-31/???/9,5,1/
考虑到下面显示的完整 URL 路线规划,您将如何处理嵌套的可选属性?另外,之后您将如何在路线中获取这些值?
Route::get('/{locale}/WIPReport/show/{startdate}/{enddate}/{regions?}/{offices?}/{departments?}/{clients?}/{employees?}', 'WIPReportController@reportdata')
->where(['regions' => '[0-9,]+', 'offices' => '[0-9,]+', 'departments' => '[0-9,]+', 'clients' => '[0-9,]+', 'employees' => '[0-9,]+'])
可以使用多个可选的路由参数,但除非你输入 /null
、/none
、/0
等,否则你肯定会 运行 出问题。以此 URL 和 Route 为例:
Route::get("{primary?}/{secondary?}/{tertiary?}", ExampleController@handleColours);
public function handleColours($primary = null, $secondary = null, $tertiary = null){
// Handle URL
}
"mysite.com/red/blue/green"
在上面,一切正常,因为所有 3 个都已定义。删除 green
也可以,因为 $tertiary
将默认为 null
。接下来,给定这条路线:
"mysite.com/red/green"
如果您打算 green
成为第三级,并期望基于此的结果,您会 运行 陷入 $secondary
未在 [=38] 中定义的问题=],因此 $secondary
将是 green
而不是 null
。如果将 URL 更改为
"mysite.com/red/null/green"
// OR
"mysite.com/red/none/green"
// OR
"mysite.com/red/0/green"
然后事情会按预期进行(考虑到一些额外的逻辑来将字符串 "null"
或 "none"
转换为 null
),但是 URL 可以得到有点泥泞。其他选项是使用查询字符串来明确指定参数:
"mysite.com?primary=red&tertiary=green"
因此有多种处理方式可供选择;选择最适合你的。
我正在寻找解决这个问题的最佳方法。
我的表单 post 是一个简单的搜索,但是有多个可选值。然后控制器将 post 重定向到更易于阅读的 URL。
例如
startdate=2018-01-01
enddate=2018-01-31
department=2,4
这将创建一个 URL
/2018-01-01/2018-01-31/2,4/
但如果他们也按员工搜索,那么 return 以下
employee=9,5,1
/2018-01-01/2018-01-31/2,4/9,5,1/
他们也可以不搜索部门而只搜索员工
/2018-01-01/2018-01-31/???/9,5,1/
考虑到下面显示的完整 URL 路线规划,您将如何处理嵌套的可选属性?另外,之后您将如何在路线中获取这些值?
Route::get('/{locale}/WIPReport/show/{startdate}/{enddate}/{regions?}/{offices?}/{departments?}/{clients?}/{employees?}', 'WIPReportController@reportdata')
->where(['regions' => '[0-9,]+', 'offices' => '[0-9,]+', 'departments' => '[0-9,]+', 'clients' => '[0-9,]+', 'employees' => '[0-9,]+'])
可以使用多个可选的路由参数,但除非你输入 /null
、/none
、/0
等,否则你肯定会 运行 出问题。以此 URL 和 Route 为例:
Route::get("{primary?}/{secondary?}/{tertiary?}", ExampleController@handleColours);
public function handleColours($primary = null, $secondary = null, $tertiary = null){
// Handle URL
}
"mysite.com/red/blue/green"
在上面,一切正常,因为所有 3 个都已定义。删除 green
也可以,因为 $tertiary
将默认为 null
。接下来,给定这条路线:
"mysite.com/red/green"
如果您打算 green
成为第三级,并期望基于此的结果,您会 运行 陷入 $secondary
未在 [=38] 中定义的问题=],因此 $secondary
将是 green
而不是 null
。如果将 URL 更改为
"mysite.com/red/null/green"
// OR
"mysite.com/red/none/green"
// OR
"mysite.com/red/0/green"
然后事情会按预期进行(考虑到一些额外的逻辑来将字符串 "null"
或 "none"
转换为 null
),但是 URL 可以得到有点泥泞。其他选项是使用查询字符串来明确指定参数:
"mysite.com?primary=red&tertiary=green"
因此有多种处理方式可供选择;选择最适合你的。