Laravel 5.1 - 尝试使用 Auth Boilerplate 作为 API 来防止 302 URL 重定向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30970887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.1 - Trying to Prevent 302 URL Redirect Using Auth Boilerplate as API
提问by mtpultz
I've setup an Ionic app to use Laravel 5.1 as an API. When I try to login, register, or email a reset password link using the boilerplate AuthController of Laravel the request sent via AngularJS ngResource to the associated RESTful endpoint is received, but it always does a 302 URL redirect to load a view before returning a 200 response from a different initiator with an error related to not not finding a view, which I can see occur in Chrome's network tab (see below for output). I would expect that the view wouldn't be found as I'm using Laravel as an API and there are no views except for an email template. What I'm trying to do is remove the redirect.
我已经设置了一个 Ionic 应用程序来使用 Laravel 5.1 作为 API。当我尝试使用 Laravel 的样板 AuthController 登录、注册或通过电子邮件发送重置密码链接时,会收到通过 AngularJS ngResource 发送到相关 RESTful 端点的请求,但它总是执行 302 URL 重定向以在返回 200 之前加载视图来自不同发起者的响应与未找到视图相关的错误,我可以在 Chrome 的网络选项卡中看到这种情况发生(输出见下文)。我希望不会找到该视图,因为我使用 Laravel 作为 API,并且除了电子邮件模板之外没有任何视图。我想要做的是删除重定向。
For example a forgotten password email request would go out (and the email is received with the reset link using the email template), and instead of a JSON response for that request a URL redirect occurs (code 302) and then a response 200 with an error that the view was not found:
例如,忘记密码的电子邮件请求将发出(并且使用电子邮件模板通过重置链接接收电子邮件),而不是该请求的 JSON 响应发生 URL 重定向(代码 302),然后响应 200 与未找到视图的错误:
** Error Response **
** 错误响应 **
Sorry, the page you are looking for could not be found.
NotFoundHttpException in RouteCollection.php).
Chrome Console - Network Tab
Chrome 控制台 - 网络标签
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
What I would expect is:
我期望的是:
Name Status Type Initiator Size Time
forgot 200 text/html ionic.bundle.js:18526 614?B 2.38?s
Which occurs if I use a route that doesn't use a the AuthController actions, and instead just returns a JSON response like this:
如果我使用的路由不使用 AuthController 操作,而是只返回这样的 JSON 响应,就会发生这种情况:
Route::post('email', function () {
return response()->json([ 'message' => 'Send an email!' ], 200);
});
Laravel Routes
Laravel 路由
+--------+----------+------------------------------+----------------------+-------------------------------------------------------------+------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+----------+------------------------------+----------------------+-------------------------------------------------------------+------------+
| | GET|HEAD | test | | Closure | |
| | POST | api/auth/login | | Project\Http\Controllers\Auth\AuthController@postLogin | guest |
| | GET|HEAD | api/auth/logout | | Project\Http\Controllers\Auth\AuthController@getLogout | |
| | POST | api/auth/register | | Project\Http\Controllers\Auth\AuthController@postRegister | guest |
| | POST | api/password/forgot | | Project\Http\Controllers\Auth\PasswordController@postEmail | guest |
| | POST | api/password/reset | | Project\Http\Controllers\Auth\PasswordController@postReset | guest |
+--------+----------+------------------------------+----------------------+-------------------------------------------------------------+------------+
So I looked through the different Auth related classes and traits and with some help found that I can overwrite postLogin, postRegister, postEmail, postReset, and getLogout routes by adding them to the AuthController. So for example postEmail now returns a response and is located in AuthController:
因此,我查看了不同的 Auth 相关类和特征,并在一些帮助下发现我可以通过将 postLogin、postRegister、postEmail、postReset 和 getLogout 路由添加到 AuthController 来覆盖它们。例如 postEmail 现在返回一个响应并且位于 AuthController 中:
** postEmail Override in AuthController** This is a copy of the original postEmail method, with a change in the return statements.
** AuthController 中的 postEmail 覆盖** 这是原始 postEmail 方法的副本,但对 return 语句进行了更改。
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);
}
}
But, with this and the others (postLogin, postRegister, postReset, and getLogout) overwritten in AuthController, and the redirects overwritten with JSON responses, for some reason the 302 URL redirect still occurs as if the changes I made to AuthController aren't being applied. Why isn't this working still?
但是,由于这个和其他(postLogin、postRegister、postReset 和 getLogout)在 AuthController 中被覆盖,并且重定向被 JSON 响应覆盖,出于某种原因,302 URL 重定向仍然发生,就好像我对 AuthController 所做的更改没有被应用。为什么这仍然不起作用?
采纳答案by mtpultz
Reason for this occurring is:
出现这种情况的原因是:
- The postEmail and postReset functions do not belong in the AuthController where I accidentally pasted them along side postLogin and postRegister, they belong in the PasswordController (yeesh)
- Both controllers were missing a few dependencies after dropping a copy of the original methods into the controllers to override and change the redirects to JSON responses
- Also the guest middleware from the constructor in AuthController had to be removed to prevent the redirect to /home from occurring on Authentication
- postEmail 和 postReset 函数不属于我不小心将它们粘贴到 postLogin 和 postRegister 旁边的 AuthController,它们属于 PasswordController (yeesh)
- 在将原始方法的副本放入控制器以覆盖并将重定向更改为 JSON 响应后,两个控制器都缺少一些依赖项
- 此外,必须删除 AuthController 中构造函数的来宾中间件,以防止在身份验证时重定向到 /home
Now I don't have anymore 302 URL redirects occurring, and I'm a bit wiser...? :)
现在我不再有 302 URL 重定向发生,而且我有点聪明......?:)
回答by ThreeAccents
you can return a json response.
你可以返回一个json响应。
public function register()
{
// do what ever to register the user
// then just return a json response
return response()->json([
'message' => 'user was registered'
]);
}
EDIT:
编辑:
add this method to Http\Requests\Request:
将此方法添加到 Http\Requests\Request:
public function response(array $errors)
{
return new JsonResponse($errors, 422);
}
I think there might be a bug in Laravel 5.1 where its handling every request as a regular request and not an ajax request. That method should fix it, it did for me.
我认为 Laravel 5.1 中可能存在一个错误,它将每个请求处理为常规请求而不是 ajax 请求。这种方法应该可以解决它,它对我有用。
回答by Pantelis Peslis
You can override the redirectPath
method of Illuminate\Foundation\Auth\RedirectsUsers
trait.
您可以覆盖trait的redirectPath
方法Illuminate\Foundation\Auth\RedirectsUsers
。
public function redirectPath()
{
return response()->json([
'msg' => 'your custom message'
]);
}
回答by Wenrui Zhao
I think Ive got this problem.
我想我有这个问题。
Notice that there is a middleware loaded in app/Http/Kernel.php which names RedirectIfAuthenticated. It contains the logic about redirecting to the home controller. Just comment it, and it will be done.
请注意,在 app/Http/Kernel.php 中加载了一个名为 RedirectIfAuthenticated 的中间件。它包含有关重定向到家庭控制器的逻辑。只需评论它,它就会完成。