【问题标题】:Show a 404 page if route not found in Laravel 5.1如果在 Laravel 5.1 中找不到路由,则显示 404 页面
【发布时间】:2015-11-18 15:31:12
【问题描述】:

我试图弄清楚如果找不到路线,则显示 404 页面未找到。我遵循了许多教程,但它不起作用。 我在\laravel\resources\views\errors中有404.blade.php

也在 handler.php 中

public function render($request, Exception $e)
{
    if ($e instanceof TokenMismatchException) {
        // redirect to form an example of how i handle mine
        return redirect($request->fullUrl())->with(
            'csrf_error',
            "Opps! Seems you couldn't submit form for a longtime. Please try again"
        );
    }

    /*if ($e instanceof CustomException) {
        return response()->view('errors.404', [], 500);
    }*/

    if ($e instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException)
        return response(view('error.404'), 404);

    return parent::render($request, $e);
}

如果我在浏览器中输入错误的 URL,它会返回一个空白页面。我有

'debug' => env('APP_DEBUG', true),

在 app.php 中。

如果找不到路由,谁能帮我显示 404 页面?谢谢。

【问题讨论】:

  • 你目前得到了什么?
  • 使用中止(404);然后在错误文件夹 404.blade.php 中创建一个页面
  • @mdamia.where 我必须使用 abort(404) ?
  • @aug Post 指定 5.1。那是 4.2 的文档。

标签: php laravel laravel-5.1


【解决方案1】:

我收到了 500 个错误,而不是 404 个错误。我解决了这样的问题:

在app/Exceptions/Handler.php文件中,有一个render函数。

用这个函数替换函数:

public function render($request, Exception $e)
{
    if ($this->isHttpException($e)) {
        switch ($e->getStatusCode()) {

            // not authorized
            case '403':
                return \Response::view('errors.403',array(),403);
                break;

            // not found
            case '404':
                return \Response::view('errors.404',array(),404);
                break;

            // internal error
            case '500':
                return \Response::view('errors.500',array(),500);
                break;

            default:
                return $this->renderHttpException($e);
                break;
        }
    } else {
        return parent::render($request, $e);
    }
}

然后您可以使用保存在 views/errors/404.blade.php 等中的视图。

【讨论】:

    【解决方案2】:

    > abort 方法将立即引发一个异常,该异常将由异常处理程序呈现。或者,您可以提供响应文本:

    abort(403, 'Unauthorized action.');
    

    您的 app_debug 设置为 true 吗?如果是这种情况,Laravel 将使用回溯抛出错误以进行调试,如果您将值更改为 false,Laravel 将在错误文件夹中显示默认的 404 页面。话虽如此,您可以随时选择使用中止。在控制器级别或路由级别,完全取决于您。

    Route::get('/page/not/found',function($closure){
      // second parameter is optional. 
      abort(404,'Page not found');
      abort(403); 
    });
    

    【讨论】:

    • @mdamia.where 我必须使用 abort(403) ?
    • 这完全取决于您想在何时何地使用 abort。查看更新的答案。
    • ,我启用了true,但它显示空白页。
    • @mdamia.i 复制了您的代码并添加到 route.php 但它返回空白页。即使我启用调试 true 它也不会抛出异常
    • 您的错误文件夹中是否有与错误编号相对应的页面?在abort(404)页面名称404.blade.php
    【解决方案3】:

    @tester.你的问题已经解决了,在composer中试试下面的命令:

    php artisan view:clear
    

    然后使用未知 URL 再次尝试。因为我之前也遇到过同样的错误。

    【讨论】:

      【解决方案4】:

      您无需检查错误类型并手动渲染 404 视图。 Laravel 已经知道使用抛出的 HTTP 错误代码(404 = resources/views/errors/404.blade.php)渲染视图。去掉额外的检查,它应该可以正常工作。

      public function render($request, Exception $e)
      {
          if ($e instanceof TokenMismatchException) {
              // redirect to form an example of how i handle mine
              return redirect($request->fullUrl())->with(
                  'csrf_error',
                  "Opps! Seems you couldn't submit form for a longtime. Please try again"
              );
          }
      
          return parent::render($request, $e);
      }
      

      【讨论】:

      • 我试过你的代码,但它没有工作,之后我按照 mdamia 的建议创建了新的 laravel 项目。然后使用你的代码它完美地工作
      【解决方案5】:

      我在 app/Exceptions/Handler.php (Laravel 5.2) 中使用以下内容:

      /**
       * Render an exception into an HTTP response.
       *
       * @param  \Illuminate\Http\Request  $request
       * @param  \Exception  $e
       * @return \Illuminate\Http\Response
       */
      public function render($request, Exception $e)
      {
          if ($e instanceof \ReflectionException OR $e instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException) //Si la ruta no existe, mostar view 404.
              return response(view('errors.404'), 404);
          return parent::render($request, $e);
      }
      

      它看起来像这样:img

      【讨论】:

        【解决方案6】:

        在 apache 中,您可以将此代码放在主目录的 .htaccess 文件中,并确保在 httpd 配置文件中将 AllowOverride Directive 更改为 all

        ErrorDocument 404 the\path\to\404.blade.php
        

        【讨论】:

        • 。我在 Windows 7 中使用 xamp 本地服务器
        • @Mohammed Elhag。按照 patricus 和 mdamia 的建议解决了问题。问题出在 laravel 项目上。我创建了新项目并尝试了它完美的工作。
        【解决方案7】:

        在配置文件夹 app.php 中更改以下代码

        'debug' => env('APP_DEBUG', true),
        

        在 App->Exception->Handler.php 用下面的代码替换渲染函数

        public function render($request, Exception $exception)
        {
            
            if ($exception instanceof ModelNotFoundException) 
            {
            return response()->view('errors.404', [], 404);
            }
        
            
            if ($exception instanceof \ErrorException) {
            return response()->view('errors.500', [], 500);
            } 
            else {
            return parent::render($request, $exception);
            }
            return parent::render($request, $exception);
        }
        

        【讨论】:

          猜你喜欢
          • 2015-11-17
          • 1970-01-01
          • 1970-01-01
          • 2021-07-24
          • 2016-10-20
          • 2019-01-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多