【问题标题】:404 not found error when routing with Laravel使用 Laravel 路由时找不到 404 错误
【发布时间】:2013-10-07 01:43:30
【问题描述】:

我正在尝试按照 David Mosher 在 youtube 上使用 Angular JS 进行端到端的教程:http://www.youtube.com/watch?v=hqAyiqUs93c

在我尝试将 /auth/login 和 auth/logout url 路由到身份验证服务之前,一切都很顺利,如 14:30 左右的视频所示。当我尝试登录时,收到 404 Not Found 错误。我试过弄乱路由的 URL,但无济于事。我正在使用 MySQL 和 Laravel 在 MAMP 上本地运行。

这是我的 routes.php 代码

Route::get('/', function(){
    return View::make('index');
});

Route::post('/auth/login/', 'AuthController@login');
Route::get('/auth/logout/', 'AuthController@logout');

还有我的 AuthController 代码

<?php

class AuthController extends BaseController {
  public function login()
  {
    if(Auth::attempt(array('email' => Input::json('email'), 'password' => Input::json('password'))))
    {
      return Response::json(Auth::user());
    } else {
      return Response::json(array('flash' => 'Invalid username or password'), 500);
    }
  }

  public function logout()
  {
    Auth::logout();
    return Response::json(array('flash' => 'Logged Out!'));
  }

}

最后,验证服务代码

angular.module("epicApp")
    .factory('AuthenticationService', function($http, $location){
        return{
            login: function(credentials){
                return $http.post("/auth/login/", credentials);
            },
            logout: function(){
                return $http.get("/auth/logout/");
            }
        }
    })

【问题讨论】:

  • 你能从控制台复制错误网址吗?

标签: mysql angularjs laravel http-status-code-404 laravel-routing


【解决方案1】:

我不知道这个问题是否仍然相关,但如果在公用文件夹中索引文件不是 .php,您将获得带有任何 HTTP URL 的 404。 您可以重命名 Angular 应用程序的 index.html。

【讨论】:

    【解决方案2】:

    唯一的问题是您必须在路由和角度脚本中将所有/auth/login/ 替换为auth/login/auth/logout/auth/logout

    所以你的路线应该是,

    Route::get('/', function(){
        return View::make('index');
    });
    
    Route::post('auth/login', 'AuthController@login');
    Route::get('auth/logout', 'AuthController@logout');
    

    还有js,

    angular.module("epicApp")
        .factory('AuthenticationService', function($http, $location){
            return{
                login: function(credentials){
                    return $http.post("auth/login", credentials);
                },
                logout: function(){
                    return $http.get("auth/logout");
                }
            }
        })
    

    【讨论】:

      猜你喜欢
      • 2015-02-04
      • 2016-10-20
      • 1970-01-01
      • 2020-03-26
      • 2019-05-20
      • 2016-09-11
      • 2014-12-29
      • 1970-01-01
      • 2018-01-15
      相关资源
      最近更新 更多