简体   繁体   English

Laravel:使用查询字符串参数重定向到安全路由

[英]Laravel: Redirecting to a Secure Route with Query String Parameters

Using Laravel 4, you can generate a URL to a named route with the following code 使用Laravel 4,您可以使用以下代码生成指向命名路由的URL

$url = route('route-name');
$url = app('url')->route('route-name');    
$url = URL::to('route-name');

Is it possible to generate a secure URL (https) to a named route? 是否可以生成到命名路由的安全 URL(https)?

I know you can "force" a route to be secure when you set it up 我知道您可以在设置路线时“强制”路线安全

Route::get('route-name', array('https', function()
{
    return 'Must be over HTTPS';
}));

However, "force" (seems to?) mean making the route invisible to http based browsing. 但是,“强制”(似乎?)意味着使该路由对于基于http的浏览不可见。 The routing methods/functions don't pick up on this, and still generate http URLs. 路由方法/功能不会继续使用,仍然会生成http URL。

To clarify, I've setup a route like this 为了澄清,我已经设置了这样的路线

Route::get('/stack-overflow', array('as'=>'route-name', function(){

}));

That's a route with a path of stack-overflow and a name of route-name . 那是一条路径,该路径的stack-overflow且名称为route-name I can generate a plaintext http link using the name ( route-name ). 我可以使用名称( route-name )生成纯文本http链接。 I want to generate a secure https link using the route name , not the path. 我想使用路由名称而不是路径来生成安全的https链接。 I also want the generated URL to contain query string parameters 我还希望生成的URL包含查询字符串参数

https://example.com/stack-overflow?foo=bar

You are looking for URL::secure . 您正在寻找URL :: secure

URL::secure('absolute/path');
//generates https://domain.com/absolute/path

With a named route: 使用命名路线:

URL::secure(URL::route('route-name'));

Capturing a comment from Razor above, you can use the following code to generate the sort of secure, query string parameter URL I was looking for in Laravel 4.2.8 从上面的Razor捕获评论,您可以使用以下代码生成我在Laravel 4.2.8中寻找的那种安全的查询字符串参数URL。

//first parameter is the configured route name, second is the list of 
//query string parameters, the third parameter (`$absolute`), ensures
//the protocol and server is stripped from the return value (giving us /path/to/route
//vs. http://example.com/path/to/route
$url_path   = URL::route('route-name',['foo'=>'bar'],false);

//with the URL path generated, we can use the secure URL generation to
//turn that path into an https url 
$secure_url = URL::secure($url_path);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM