【问题标题】:How to make Laravel 5 redirect to Angular routes without the hash (#)?如何使 Laravel 5 重定向到没有哈希(#)的 Angular 路由?
【发布时间】:2016-05-05 08:54:37
【问题描述】:

我正在使用带有 Angular 的 Laravel 5。

默认情况下,Angular 将 /#/ 添加到 URL。

我不喜欢 /#/,所以我使用 Angular 中的 $locationProvider.html5Mode(true); 将其删除。

现在每当我尝试访问 Angular URL 时,Laravel 拒绝重定向我,而是导致错误 NotFoundHttpException 说:

抱歉,找不到您要查找的页面。

RouteCollection.php 第 161 行中的 NotFoundHttpException:

这是非常合乎逻辑的,因为 Laravel 的路由文件中没有定义路由。

如何解决这个问题?!

更多详情:

  • 当我访问 http://whatever.app/#/dashboard/home 时,一切正常(laravel 知道它是 Angular 路由,然后 Angular 去掉了 # 所以 URL 变为 http://whatever.app/dashboard/home 没有错误)。
  • 当我访问http://whatever.app/dashboard/home Laravel 时显示错误NotFoundHttpException

【问题讨论】:

    标签: angularjs laravel laravel-5 laravel-5.1 laravel-routing


    【解决方案1】:

    编辑app/Exceptions/Handler.php文件中的render函数:

    public function render($request, Exception $e)
    {
        if ($e instanceof ModelNotFoundException) {
            $e = new NotFoundHttpException($e->getMessage(), $e);
        }
    
        // handle Angular routes when accessed directly from the browser without the need of the '#'
        if ($e instanceof NotFoundHttpException) {
    
            $url = parse_url($request->url());
    
            $angular_url = $url['scheme'] . '://' . $url['host'] . '/#' . $url['path'];
    
            return response()->redirectTo($angular_url);
        }
    
        return parent::render($request, $e);
    }
    

    现在,每当您尝试访问 http://whatever.app/dashboard/home 时,laravel 都会检测到路由不存在并将其转换为 http://whatever.app/#/dashboard/home,然后再被 anguar 删除为 http://whatever.app/dashboard/home,但这次没有错误 :)

    【讨论】:

      【解决方案2】:

      当你访问下面的 url 时,Laravel 会显示错误 NotFoundHttpException。

      http://whatever.app/dashboard/home 
      

      意味着 laravel 在 routes.php 中没有这条路由

      可能当您点击http://whatever.app/#/dashboard/home 时,您的角度重定向到其他 url 到 laravel 工作正常(查看您的角度控制器 templateUrl 和此路由的 url 值,检查并管理从这里到您的 laravel 路由,如果您愿意)。

      但是当你点击 url http://whatever.app/dashboard/home 时,它直接进入 laravel 路由并报错。

      所以现在你想从 url 中删除 #。 这些是一些解决方案链接

      https://scotch.io/quick-tips/pretty-urls-in-angularjs-removing-the-hashtag

      Removing the fragment identifier from AngularJS urls (# symbol)

      AngularJS routing without the hash '#'

      【讨论】:

        【解决方案3】:

        使用

        missingMethod(){ return $this->getIndex(); }

        在控制器中。这将检查您的路线是否存在,如果它不是一些 laravel 路线 - 返回索引。

        【讨论】:

          【解决方案4】:

          Mahmoud Zalt 的答案是正确的。但不要考虑查询字符串。

          为了将 query 包含到 URL,您需要更改 URI 的 URL 并从中获取查询。

          // handle Angular routes when accessed directly from the browser without the need of the '#'
          if ($e instanceof NotFoundHttpException) {
          
              $url = parse_url($request->getUri());
          
              $angular_url = $url['scheme'] . '://' . $url['host'] . '/#' . $url['path'] . ( isset($url['query']) ? "?".$url['query'] : "" ) ;;
          
              return response()->redirectTo($angular_url);
          }
          

          【讨论】:

            猜你喜欢
            • 2014-09-16
            • 1970-01-01
            • 2019-06-11
            • 2018-07-22
            • 2015-11-23
            • 2012-12-28
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多