Php 拉雷维尔在做任何事情后都会改变方向
现在使用Laravel5.6,很快就会更新到5.8 问题很简单,我想在登录后重定向(这一次,只想添加一些重定向逻辑,以便稍后使用)。 在本例中,我设置了登录中间件,将Php 拉雷维尔在做任何事情后都会改变方向,php,laravel,Php,Laravel,现在使用Laravel5.6,很快就会更新到5.8 问题很简单,我想在登录后重定向(这一次,只想添加一些重定向逻辑,以便稍后使用)。 在本例中,我设置了登录中间件,将$request->getBackURI()作为参数发送到登录视图 登录表单后,我可以将重定向到作为查询字符串,但当我登录时,我希望看到$request->get('redirect\u to')它当然是空的,因为我没有将其作为参数发送到实际登录控制器 我在请求会话中保存了它,但突然出现了一个我没有看到的其他变量 {session:
$request->getBackURI()
作为参数发送到登录视图
登录表单后,我可以将重定向到作为查询字符串,但当我登录时,我希望看到$request->get('redirect\u to')
它当然是空的,因为我没有将其作为参数发送到实际登录控制器
我在请求会话中保存了它,但突然出现了一个我没有看到的其他变量
{session:
...
url => www.foo.bar/pathtoredirect,
redirect_to => /pathtoredirect} -> this is the one I stored
问题是,一旦我使用重定向方法,我真的无法访问任何东西,这是一个无效函数
protected function redirectTo()
{
return route('main.index');
}
如果我包括我得到的请求对象
Too few arguments to function App\Http\Controllers\Auth\LoginController::redirectTo(), 0 passed ... on line 15 and exactly 1 expected
我知道,对于这种情况,这只是一个重定向,从路径->登录->路径我可以使用
protected function redirectTo()
{
return back();
}
我只是想知道是否有任何方法可以实现我的方法,或者Laravel方法是否可以在每次我想要的时候工作(例如:不止一个重定向,想象一些中间件请求或facebook登录)您好,您可以覆盖Laravel中的默认登录功能和登录后功能
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
class LoginController extends Controller
{
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
/**
* The user has been authenticated.
*
* @param \Illuminate\Http\Request $request
* @param mixed $user
* @return mixed
*/
protected function authenticated(Request $request, $user)
{
// in here you can do what ever you want
$path = $request->get('redirect_to')
return redirect($path);
}
}
你在使用laravel内置登录吗?我应该真正改进我的POO概念。您能解释一下为什么
trait AuthenticatesUsers
中的authenticates函数为空吗??被sendLoginResponse
调用,还是否?(我无法在此发布代码)laravel的早期版本没有类似的功能。我们只能重定向该路径。但新版本授权后,我们可以保存会话,或者我们可以做任何我们需要的事情。它是空的,我们可以直接使用该函数,但当您更新composer时,您的文件将被更改。所以最好覆盖这个函数。顺便问一下,您使用默认登录过程吗?是的,请发布并让我知道你到底需要什么?是的,我使用默认和社交名将登录facebook。登录后,我需要重定向到某个路径。通过查询参数或任何其他方式。
protected function redirectTo()
{
$path = request()->redirect_to;
or
$path = \Request::get('redirect_to');
return back();
}