Laravel 路由在缺少必需参数时不显示错误
Laravel routes not displaying error when required params missing
目前我对 Laravel 感到有些奇怪,无法弄清楚哪里出了问题
如果我不在 url 中包含参数,我的控制器应该重定向,但它没有。我试过转出一些文本,但我得到的只是一个纯白色的屏幕,除非我在 url 中包含参数。该参数设置为可选。即使我删除了 ?
以使参数成为强制性的,我仍然会看到一个白屏,我预计会出现某种错误。
除了有问题的路由组之外,其余路由工作正常。
这是我的控制器的精简版
class CustomerRequirementsController extends Controller
{
public function index($id = '')
{
debug('sdfgsdfgdsg');
exit;
//if the id has been removed from the url, redirect back to quotes summary table
if(empty($id)) {
return redirect()->route('quote.index');
}
...
}
这是我的问题路线
Route::prefix('quotes')->group(function () {
Route::get('/{id?}', 'QuoteController@show')->name('quote.show');
Route::post('/customer-requirements/add-group', 'CustomerRequirementsController@ajax')->name('customer-requirements.add-group');
Route::get('/customer-requirements/{id?}', 'CustomerRequirementsController@index')->name('quote.customer-requirements.index');
Route::resource('/customer-requirements', 'CustomerRequirementsController', ['except' => ['show', 'destroy']]);
与其将 {id?} 设为可选,不如通过 {id} 将其设为强制性可能会解决您的问题。
你有一个逻辑错误,你的 if
永远不会为真,因为你的 $id
参数永远不会为空,它总是赋值,在这种情况下,如果它不存在于 url,它的赋值为 ''
,未通过 empty
检查。所以如果你 $id = null
然后 if(is_null($id))
你会过得更好
Laravel & Nginx :
如果您使用的是虚拟主机,请在虚拟主机配置文件中添加此行。
location / {
try_files $uri $uri/ /index.php?$query_string;
}
我以前遇到过同样的问题。
您的第一条路线 Route::get('/{id?}', 'QuoteController@show')->name('quote.show');
在任何其他路线之前被调用。
您的节目有 return 视图....或其他功能吗?如果是,请尝试移动到路由组的底部
目前我对 Laravel 感到有些奇怪,无法弄清楚哪里出了问题
如果我不在 url 中包含参数,我的控制器应该重定向,但它没有。我试过转出一些文本,但我得到的只是一个纯白色的屏幕,除非我在 url 中包含参数。该参数设置为可选。即使我删除了 ?
以使参数成为强制性的,我仍然会看到一个白屏,我预计会出现某种错误。
除了有问题的路由组之外,其余路由工作正常。
这是我的控制器的精简版
class CustomerRequirementsController extends Controller
{
public function index($id = '')
{
debug('sdfgsdfgdsg');
exit;
//if the id has been removed from the url, redirect back to quotes summary table
if(empty($id)) {
return redirect()->route('quote.index');
}
...
}
这是我的问题路线
Route::prefix('quotes')->group(function () {
Route::get('/{id?}', 'QuoteController@show')->name('quote.show');
Route::post('/customer-requirements/add-group', 'CustomerRequirementsController@ajax')->name('customer-requirements.add-group');
Route::get('/customer-requirements/{id?}', 'CustomerRequirementsController@index')->name('quote.customer-requirements.index');
Route::resource('/customer-requirements', 'CustomerRequirementsController', ['except' => ['show', 'destroy']]);
与其将 {id?} 设为可选,不如通过 {id} 将其设为强制性可能会解决您的问题。
你有一个逻辑错误,你的 if
永远不会为真,因为你的 $id
参数永远不会为空,它总是赋值,在这种情况下,如果它不存在于 url,它的赋值为 ''
,未通过 empty
检查。所以如果你 $id = null
然后 if(is_null($id))
Laravel & Nginx :
如果您使用的是虚拟主机,请在虚拟主机配置文件中添加此行。
location / {
try_files $uri $uri/ /index.php?$query_string;
}
我以前遇到过同样的问题。
您的第一条路线 Route::get('/{id?}', 'QuoteController@show')->name('quote.show');
在任何其他路线之前被调用。
您的节目有 return 视图....或其他功能吗?如果是,请尝试移动到路由组的底部