使用 Laravel 路由时找不到 404 错误

Posted

技术标签:

【中文标题】使用 Laravel 路由时找不到 404 错误【英文标题】:404 not found error when routing with Laravel 【发布时间】: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/");
            
        
    )

【问题讨论】:

你能从控制台复制错误网址吗? 【参考方案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");
            
        
    )

【讨论】:

以上是关于使用 Laravel 路由时找不到 404 错误的主要内容,如果未能解决你的问题,请参考以下文章

为啥使用 Origin 标头时找不到路由

在提交 Vue js 和 Laravel 时找不到 POST 404?

使用 GWT 时找不到 Firebase Service Worker(404 错误)

使用 GWT 时找不到 Firebase Service Worker(404 错误)

在 Python3 中使用 Socket 库时找不到 404 错误

运行 REST API 时找不到 Tomcat 错误 404