2015-06-22 22 views
1

我已經設置了一個使用Laravel 5.1作爲API的Ionic應用程序。當我嘗試使用Laravel的樣板AuthController嘗試登錄,註冊或通過電子郵件發送重置密碼鏈接時,會收到通過AngularJS ngResource發送到相關RESTful端點的請求,但它始終會執行302 URL重定向以在返回200之前加載視圖來自不同發起人的響應,其中有一個與未找到視圖相關的錯誤,我可以在Chrome的網絡標籤中看到這一點(請參閱下面的輸出)。我期望該視圖不會被找到,因爲我使用Laravel作爲API,除了電子郵件模板外,沒有任何視圖。我想要做的是刪除重定向。Laravel 5.1 - 嘗試使用Auth Boilerplate作爲API來阻止302 URL重定向

例如,一個被遺忘的密碼電子郵件請求會消失(並且電子郵件會通過使用電子郵件模板的重置鏈接接收),而不是針對該請求的JSON響應,會發生URL重定向(代碼302),然後是響應200有錯誤,該視圖未找到:

**錯誤響應**

Sorry, the page you are looking for could not be found. 
NotFoundHttpException in RouteCollection.php). 

鉻控制檯 - 網絡選項卡

Name  Status Type   Initiator        Size Time 
forgot  302 text/html ionic.bundle.js:18526     614 B 2.38 s 
localhost 200 xhr   http://project.dev/api/password/forgot 2.3 KB 2.00 ms 

我會想到的是:

Name  Status Type   Initiator        Size Time 
forgot  200 text/html ionic.bundle.js:18526     614 B 2.38 s 

如果我使用不使用的AuthController行動路線發生,而是直接返回這樣的JSON響應:

Route::post('email', function() { 
    return response()->json([ 'message' => 'Send an email!' ], 200); 
}); 

Laravel路由

+--------+----------+------------------------------+----------------------+-------------------------------------------------------------+------------+ 
| Domain | Method | URI       | Name     | Action              | Middleware | 
+--------+----------+------------------------------+----------------------+-------------------------------------------------------------+------------+ 
|  | GET|HEAD | test       |      | Closure              |   | 
|  | POST  | api/auth/login    |      | Project\Http\Controllers\Auth\[email protected]  | guest  | 
|  | GET|HEAD | api/auth/logout    |      | Project\Http\Controllers\Auth\[email protected]  |   | 
|  | POST  | api/auth/register   |      | Project\Http\Controllers\Auth\[email protected] | guest  | 
|  | POST  | api/password/forgot   |      | Project\Http\Controllers\Auth\[email protected] | guest  | 
|  | POST  | api/password/reset   |      | Project\Http\Controllers\Auth\[email protected] | guest  | 
+--------+----------+------------------------------+----------------------+-------------------------------------------------------------+------------+ 

所以我通過不同的身份驗證相關CLASSE看着s和traits,並有一些幫助發現,我可以通過將它們添加到AuthController來覆蓋postLogin,postRegister,postEmail,postReset和getLogout路由。因此,例如postEmail現在返回一個響應,並位於AuthController中:

** postEmail覆蓋AuthController ** 這是原始postEmail方法的副本,返回語句發生更改。

public function postEmail(Request $request) 
{ 
    $this->validate($request, [ 'email' => 'required|email' ]); 

    $response = Password::sendResetLink($request->only('email'), function (Message $message) { 
     $message->subject($this->getEmailSubject()); 
    }); 

    switch ($response) { 
     case Password::RESET_LINK_SENT: 
      return response()->json([ 'message' => trans($response) ], 200); 

     case Password::INVALID_USER: 
      return response()->json([ 'message' => trans($response) ], 400); 
    } 
} 

但是,與這和其他人(postLogin,的postRegister,postReset和getLogout)在AuthController覆蓋,並用JSON響應覆蓋重定向,出於某種原因,302 URL重定向仍然存在,就好像改變我對AuthController所做的更改不會被應用。爲什麼不能繼續工作?

+0

請顯示您的routes.php文件,並且可能需要使用JSON響應而不是重定向的控制器。 –

回答

1

原因發生這種情況是:

  1. 的postEmail和postReset功能沒有在AuthController,我不小心粘貼他們沿側postLogin和的postRegister屬於,他們在PasswordController(yeesh)
  2. 屬於
  3. 兩個控制器在將原始方法的副本放入控制器以覆蓋並將重定向更改爲JSON響應後,缺少一些依賴關係
  4. 此外,來自構造器的來賓中間件r在AuthController不得不被刪除,以防止在身份驗證上發生重定向到/ home

現在我不再有302個URL重定向發生,而且我有點聰明......? :)

1

你可以返回一個json響應。

public function register() 
{ 
    // do what ever to register the user 
    // then just return a json response 

    return response()->json([ 
     'message' => 'user was registered' 
    ]); 
} 

編輯:

添加此方法HTTP \請求\索取:

public function response(array $errors) 
{ 
    return new JsonResponse($errors, 422); 
} 

我覺得可能是在Laravel 5.1,其中它的處理每一個請求作爲常規請求和錯誤不是ajax請求。該方法應該修復它,它爲我做了。

+0

嗨,所以在AuthController @ create中,像你一樣返回響應,而不是從create返回的內容,這似乎不起作用。我仍然得到一個404說:「RouteFollection.php中的NotFoundHttpException」。所以它看起來仍然嘗試使用Laravel 5.1的AuthController進行重定向。爲了以防萬一,我添加了我的創建操作。 – mtpultz

+0

@mtpultz我想我知道這個問題,它可能是Laravel 5.1中的一個錯誤,因爲它也發生在我身上。在你的抽象請求類添加我剛添加到我的答案的方法。 – ThreeAccents

+0

嗨,難道這不會在每個響應中返回422嗎? – mtpultz

1

您可以覆蓋Illuminate\Foundation\Auth\RedirectsUsers特徵的redirectPath方法。

public function redirectPath() 
{ 
    return response()->json([ 
     'msg' => 'your custom message' 
    ]); 
} 
+0

嗨,這仍然產生一個404錯誤「NotFoundHttpException」放置在Laravel 5.1的AuthController。 – mtpultz

0

我想我得到了這個問題。

請注意,在app/Http/Kernel.php中加載了一個名爲RedirectIfAuthenticated的中間件。它包含有關重定向到家庭控制器的邏輯。只需發表評論,它就會完成。