簡體   English   中英

Laravel:使用查詢字符串參數重定向到安全路由

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

使用Laravel 4,您可以使用以下代碼生成指向命名路由的URL

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

是否可以生成到命名路由的安全 URL(https)?

我知道您可以在設置路線時“強制”路線安全

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

但是,“強制”(似乎?)意味着使該路由對於基於http的瀏覽不可見。 路由方法/功能不會繼續使用,仍然會生成http URL。

為了澄清,我已經設置了這樣的路線

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

}));

那是一條路徑,該路徑的stack-overflow且名稱為route-name 我可以使用名稱( route-name )生成純文本http鏈接。 我想使用路由名稱而不是路徑來生成安全的https鏈接。 我還希望生成的URL包含查詢字符串參數

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

您正在尋找URL :: secure

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

使用命名路線:

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

從上面的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