Php 若未提供参数,则Laravel routes中的可选参数始终返回404页

Php 若未提供参数,则Laravel routes中的可选参数始终返回404页,php,laravel,laravel-routing,Php,Laravel,Laravel Routing,我正在处理一个申请,我不明白这里发生了什么 在我的routes中,我添加了一个可选参数,但如果我不提供可选参数,它将返回404页。路由应在有或无可选参数的两个实例上工作 另一件事是,我还想在route参数上添加where条件,但它不起作用 web.php Route::get('invoices/{type?}/create', 'InvoiceController@create')->where('type', '[A-Za-z]+')->name('invoices.create

我正在处理一个申请,我不明白这里发生了什么

在我的routes中,我添加了一个可选参数,但如果我不提供可选参数,它将返回404页。路由应在有或无可选参数的两个实例上工作

另一件事是,我还想在route参数上添加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然后它在控制器中回显“循环”值

我甚至尝试删除路线上的where条款,但仍然不起作用。并尝试清除路由缓存和重新排序路由。但还是有同样的问题

php(即使以下内容也不起作用)


您需要通过将可选参数移动到路由的末尾来避免歧义

Check
php artisan route:list
Yes@catcon,但我对Laravel的这种行为完全感到惊讶。所以我们不能在中间使用可选参数。对吗?
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' 
]);