【发布时间】:2020-12-10 12:24:42
【问题描述】:
我正在处理一个应用程序,但我不明白这里发生了什么。
在我的路由中,我添加了一个可选参数,但如果我不提供可选参数,它会返回 404 页。路由应该在有或没有可选参数的两个实例上工作。
另一件事是我也想在路由参数上添加 where 条件,但它不起作用。
web.php
Route::get('invoices/{type?}/create', 'InvoiceController@create')->where('type', '[A-Za-z]+')->name('invoices.create');
Route::post('invoices/{type?}', 'InvoiceController@store')->where('type', '[A-Za-z]+')->name('invoices.store');
Route::resource('invoices', 'InvoiceController')->except([
'create', 'store'
]);
InvoiceController.php
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create(Request $request, $type = null)
{
echo $type;
die;
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request, InvoiceValidator $invoiceValidator, $type = null)
{
echo $type;
die;
}
当我输入 URL“http://localhost/project/invoices/create”时,它返回 404,当我输入“http://localhost/project/invoices/recurring/create”时,它会回显“recurring”值在控制器中。
我什至尝试删除路线上的 where 子句,但仍然无法正常工作。并尝试清除路由缓存和重新排序路由。但还是有同样的问题。
web.php(连下面的都不行)
Route::get('invoices/{type?}/create', 'InvoiceController@create')->name('invoices.create');
Route::post('invoices/{type?}', 'InvoiceController@store')->name('invoices.store');
Route::resource('invoices', 'InvoiceController')->except([
'create', 'store'
]);
【问题讨论】:
-
检查
php artisan route:list -
是的@catcon,但我对 Laravel 的这种行为感到非常惊讶。所以我们不能在中间使用可选参数。对吗?
标签: php laravel laravel-routing