既然如此,我无法在SO中找到这样的问题,我正在写这篇文章,希望这不是重复...
我有一个使用 KnockoutJs 开发的页面,我用它来保护Laravel身份验证。基本上,我仅使用Laravel进行登录/注册,一旦用户登录/登录,他就会被重定向到KnockoutJS页面。
现在我们假设我有一个网址
http://example.com/mypage#q=some+parameters&p=somethingsomething
如果我与我的一个朋友分享该网址,很明显会将我的朋友重定向到登录页面。现在,登录页面URL(他被重定向到的位置)是
http://example.com/login#q=some+parameters&p=somethingsomething
但是一旦他登录,他就会被重定向到
http://example.com/mypage#
这显然是不对的,因为我需要参数......
我的路线页面如下,
Route::group(['middleware' => 'web'], function() {
Route::auth();
Route::get('/', 'MainController@index')->name('home.index');
});
Route::group(['middleware' => ['web','auth']], function () {
Route::get('/mypage', 'MyController@index')->name('mypage.index');
});
我的 AuthController 有 redirectTo 设置了网址
protected $redirectTo = '/mypage';
我应该做什么改变(在AuthController中,或在路由中?或在MiddleWare中)将用户重定向到
http://example.com/mypage#q=some+parameters&p=somethingsomething
登录后?
答案 0 :(得分:0)
您可以在Laravel中使用预期的方法。
例如:
public function authenticate()
{
if (Auth::attempt(['email' => $email, 'password' => $password])) {
// Authentication passed...
return redirect()->intended('dashboard');
}
}
这会将用户重定向到登录页面的目标URL,或者如果没有预期的URL,则重定向到仪表板。
https://laravel.com/docs/5.2/authentication#authenticating-users
答案 1 :(得分:0)
由于Laravel的Redirect::intended
对我不起作用,我自己发布了这个问题的答案......
用户 Jquery / Javscript 在重定向后创建Cookie,如下所示:
(function(){
'use strict';
var intended = window.location.hash;
(intended.length > 0) ? jQuery.cookie('intended', '/app' + intended) : jQuery.cookie('intended', '/app');
})();
并将这些更改变为Laravel 5.2的默认AuthController
//Replace the default constructor with this
public function __construct(Request $request) {
if(isset($_COOKIE['intended'])) {
$this->setRedirect($request);
}
}
//Copy paste it anywhere in your AuthController Class
protected function setRedirect($request) {
$intended = (url($_COOKIE['intended']));
$url['intended'] = $intended;
$request->session()->put('url', $url);
}
希望这有助于某人......
谢谢,))