在 Laravel 4 中生成查询字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17216221/
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
Generating query strings in Laravel 4
提问by eski009
I have a named route
我有一条命名路线
Route::get('login/facebook', array(
'as' > 'login.facebook.authorise',
'uses' => 'AuthController@getFBAuthorise'
));
and want to produce a URL pointing to it with a query string appended.
并希望生成一个指向它的 URL,并附加一个查询字符串。
For example:
例如:
URL::route('login.facebook.authorise', array("next"=>'/dashboard/')
URL::route('login.facebook.authorise', array("next"=>'/dashboard/')
would produce
会产生
/login/facebook?next=/dashboard/
/login/facebook?next=/dashboard/
Is something like this built in to the framework? If not, what is the best way to try and do this?
框架中是否内置了这样的东西?如果没有,尝试这样做的最佳方法是什么?
Thanks.
谢谢。
采纳答案by Alexandre Danault
The URL helper can do this.
URL 助手可以做到这一点。
http://laravel.com/docs/helpers#urls
http://laravel.com/docs/helpers#urls
echo link_to_route('route.name', $title, $parameters = array(), $attributes = array());
echo link_to_route('route.name', $title, $parameters = array(), $attributes = array());
EDIT: To get only the URL :
编辑:仅获取 URL:
http://laravel.com/docs/routing#named-routes
http://laravel.com/docs/routing#named-routes
$url = URL::route('route.name', $parameters);
$url = URL::route('route.name', $parameters);
回答by mattems
I am a bit late to the party
我参加晚会有点晚
Just dove into the classes and methods for building urls and you can do this:
只需深入研究用于构建 url 的类和方法,您就可以执行以下操作:
link_to_route('login.facebook.authorise','Facebook Login',array('next'=>'/Dashboard/'))
So. the array will always fallback to query params unless the names are declared in the route.
所以。除非在路由中声明了名称,否则数组将始终回退到查询参数。
回答by tymondesigns
If the use case for this is redirecting to a page after logging in then you could use
如果此用例在登录后重定向到页面,那么您可以使用
Redirect::intended('dashboard');
As seen here http://laravel.com/docs/security#authenticating-users
如此处所示http://laravel.com/docs/security#authenticating-users
Sorry if that's not what you're trying to achieve.
抱歉,如果这不是您想要实现的目标。