Php 重定向到预期的URL流明
我正在用一个简单的API和身份验证构建一个小应用程序 我希望将用户重定向到预期的URL,如果他自己访问/auth/login,我希望他重定向到/foo 其中有一个函数:return redirect->destinated'/foo' 当我在路由中使用此选项时,服务器日志中会出现以下错误:Php 重定向到预期的URL流明,php,redirect,laravel,lumen,Php,Redirect,Laravel,Lumen,我正在用一个简单的API和身份验证构建一个小应用程序 我希望将用户重定向到预期的URL,如果他自己访问/auth/login,我希望他重定向到/foo 其中有一个函数:return redirect->destinated'/foo' 当我在路由中使用此选项时,服务器日志中会出现以下错误: [30-Apr-2015 08:39:47 UTC] PHP Fatal error: Call to undefined method Laravel\Lumen\Http\Redirector::int
[30-Apr-2015 08:39:47 UTC] PHP Fatal error: Call to undefined method Laravel\Lumen\Http\Redirector::intended() in ~/Sites/lumen-test/app/Http/routes.php on line 16
我认为这是因为它是的一个较小版本,可能此函数尚未实现。我认为您必须在预期的方法中指定路由名称,而不是URI:
return redirect()->intended('foo');
假设您已经命名了路线,我认为这仍然有效:
return Redirect::intended('/foo');
更新:
试试这个:
检索请求的URI:
$uri = Request::path(); // Implemented in Lumen
return redirect($uri);
然后重定向到请求的URI:
$uri = Request::path(); // Implemented in Lumen
return redirect($uri);
这可能有用 我认为您必须在预期的方法中指定路由名称,而不是URI:
return redirect()->intended('foo');
假设您已经命名了路线,我认为这仍然有效:
return Redirect::intended('/foo');
更新:
试试这个:
检索请求的URI:
$uri = Request::path(); // Implemented in Lumen
return redirect($uri);
然后重定向到请求的URI:
$uri = Request::path(); // Implemented in Lumen
return redirect($uri);
这可能有用 事实上,从Lumen的源代码来看,它没有实现: 你的选择是: 查一下拉威尔的Symfony的?实现并将其放入您自己的代码中 完全编写您自己的实现——一种非常简单的方法是将请求URL存储在会话中,重定向到登录页面,当用户成功登录时,从会话中检索URL并重定向他
实际上,从Lumen的源代码来看,它没有实现: 你的选择是: 查一下拉威尔的Symfony的?实现并将其放入您自己的代码中 完全编写您自己的实现——一种非常简单的方法是将请求URL存储在会话中,重定向到登录页面,当用户成功登录时,从会话中检索URL并重定向他
我通过稍微调整中间件并在会话中存储Request::path解决了这个问题 这就是我的中间件的外观:
class AuthMiddleware {
public function handle($request, Closure $next) {
if(Auth::check()){
return $next($request);
} else {
session(['path' => Request::path()]);
return redirect('/auth/login');
}
}
}
在my routes.php中,我有一条路线,我将尽快将其外包给控制器:
$app->post('/auth/login', function(Request $request) {
if (Auth::attempt($request->only('username', 'password'))){
if($path = session('path')){
return redirect($path);
} else {
return redirect('/messages');
}
} else {
return redirect()->back()->with("error", "Login failed!");
}
});
感谢您建议使用Request::path方法。
希望这将有助于一些新手
,顺便说一句,这是一个很好的框架: 我通过稍微调整我的中间件以及在会话中存储Request::path解决了这个问题 这就是我的中间件的外观:
class AuthMiddleware {
public function handle($request, Closure $next) {
if(Auth::check()){
return $next($request);
} else {
session(['path' => Request::path()]);
return redirect('/auth/login');
}
}
}
在my routes.php中,我有一条路线,我将尽快将其外包给控制器:
$app->post('/auth/login', function(Request $request) {
if (Auth::attempt($request->only('username', 'password'))){
if($path = session('path')){
return redirect($path);
} else {
return redirect('/messages');
}
} else {
return redirect()->back()->with("error", "Login failed!");
}
});
感谢您建议使用Request::path方法。
希望这将有助于一些新手
,顺便说一句,这是一个很好的框架: 可能是您可以使用命名路由:问题是,预期的方法在Lumen中不起作用:/可能是您可以使用命名路由:问题是,预期的方法在Lumen中不起作用:/n这不起作用。不幸的是,我已经尝试用这种方式解决问题。请尝试Request::path;不幸的是,这不起作用,我已经尝试过用这种方法解决这个问题;谢谢,我已经考虑过了,但我想在自己动手之前确保它没有实现谢谢,我已经考虑过了,但我想在自己动手之前确保它没有实现通过使用$request->path for request和在构造函数中使用$auth{$this->auth=$auth;}来避免使用Facades。通过使用$request->path for request和在构造函数中使用$auth{$this->auth=$auth;}来避免使用Facades。