【问题标题】:Laravel routes not displaying error when required params missing缺少所需参数时,Laravel 路由不显示错误
【发布时间】:2018-02-20 11:20:06
【问题描述】:

我现在对 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']]);

【问题讨论】:

  • 调试时是否打印任何“sdfgsdfgdsg”?
  • ehhmmm 你在早期就退出了脚本
  • storage/logs/laravel.log 说什么?
  • 我认为他只是在调试索引函数的前两行。

标签: php laravel routes laravel-5.4


【解决方案1】:

我以前也遇到过同样的问题。

您的第一条路线 Route::get('/{id?}', 'QuoteController@show')->name('quote.show'); 在任何其他路线之前被调用。

您的 show 函数是否返回视图....或任何内容?如果是,请尝试移动到路由组的底部

【讨论】:

    【解决方案2】:

    而不是将 {id?} 设为可选,而通过 {id} 强制它可能会解决您的问题。

    【讨论】:

      【解决方案3】:

      您有一个逻辑错误,您的 if 永远不会为真,因为您的 $id 参数永远不会为空,它始终具有赋值,在这种情况下,如果它不存在于 url 中,则赋值为 '' empty 检查失败。所以你最好先$id = null 然后if(is_null($id))

      【讨论】:

        【解决方案4】:

        Laravel 和 Nginx :

        如果您使用的是虚拟主机,请在 vhost conf 中添加此行。

        location / {
              try_files $uri $uri/ /index.php?$query_string;
        }
        

        了解更多:https://laravel.com/docs/5.4#web-server-configuration

        【讨论】:

          猜你喜欢
          • 2016-05-17
          • 1970-01-01
          • 2019-01-09
          • 2020-08-07
          • 2020-12-25
          • 1970-01-01
          • 2022-01-21
          • 2020-11-01
          相关资源
          最近更新 更多