在 Auth::attempt() Laravel 5.1 中禁用重定向
Posted
技术标签:
【中文标题】在 Auth::attempt() Laravel 5.1 中禁用重定向【英文标题】:Disable Redirect in Auth::attempt() Laravel 5.1 【发布时间】:2016-01-06 21:29:15 【问题描述】:我使用 Laravel 创建 REST API。我在登录 API 中,所以我使用从 Laravel 构建的 AuthController。
但是,当我第一次使用Auth::attempt()
验证用户登录时,我成功获取了用户信息,因为我调用了这个方法Auth::user()
。
但是,当我第二次再次运行时,出现以下错误:
NotFoundHttpException in RouteCollection.php line 161:
我知道如果用户已通过身份验证,它会自动发送重定向。并保存会话。
这是我在AuthController.php
中的登录代码
public function demo()
if (Auth::attempt(Input::only('email','password')))
return Auth::user();
else
return "false";
我这样写路线:
Route::post('demo', 'Auth\AuthController@demo');
在这种情况下如何禁用重定向? 因为我想从我的移动应用程序调用我的 API。
谢谢
【问题讨论】:
似乎重定向是由中间件触发的,请检查您的中间件配置 我没有使用任何中间件。 请分享一段您尝试验证用户的代码 - Auth::attempt() 不做任何重定向,只返回真/假。并且如果可能的话,您尝试访问的路线。也许您将其置于访客访问权限下 - 因此在授权用户后,它会被重定向到访客允许的路由之外。 @Silwerclaw 请检查我的问题。我添加了一些代码。谢谢 如果您没有更改默认的 AuthController - 所以在构造函数 $this->middleware('guest', ['except' => 'getLogout' ]); 【参考方案1】:回答
好的,我得到了解决方案,但这不是优雅的方式。我编辑班级
RedirectIfAuthenticated
并删除return redirect(/home)
inhandle
方法。
【讨论】:
以上是关于在 Auth::attempt() Laravel 5.1 中禁用重定向的主要内容,如果未能解决你的问题,请参考以下文章
Laravel 4 - Auth::attempt() 不工作
为啥 Auth::attempt 在 Laravel 5.3 中总是返回 false?