Laravel 4.2:强制URL呈现为HTTP或HTTPS
Laravel 4.2: Force URL to be rendered as HTTP or HTTPS
我们定义了一个简单的路由,如下所示:
路线
// Home
Route::get('/home', [
'as' => 'home::index',
'uses' => 'IndexController@home'
]);
查看
<a href="{{ route('home::index') }}">Home</a>
我们需要强制我们的 link/route 视图呈现为 HTTP 或在某些情况下呈现为 HTTPS 路由,例如http://host.domain/home
或 https://host.domain/home
。
我们不能使用 URL::forceSchema("http")
或 URL::forceSchema("https")
,因为我们需要在 HTTP 页面上强制使用 HTTPS,在 HTTPS 页面上强制使用 HTTP。我们有一个多域应用程序。一些域 运行 通过 HTTP 一些通过 HTTPS。一个link到不同的域/"application section"可以放在任何地方。无法通过 HTTP 访问通过 HTTPS 访问的域 运行。无法通过 HTTPS
访问 HTTP 上的域 运行
如何强制将路由呈现为特定的超文本协议?
Laravel documentaion 中有一个标题为强制通过 HTTPS 提供路由 的部分。它没有提到为路由生成 URL,但我可以在 Illuminate\Routing\UrlGenerator
代码中看到此设置在 getRouteScheme
方法中得到遵守。因此,向操作数组添加一个新值 http
/https
应该可以解决问题:
强制路由呈现为 HTTP:
Route::get('/home', [
'as' => 'home::index',
'uses' => 'IndexController@home',
'http'
]);
强制路由呈现为 HTTPS:
Route::get('/home', [
'as' => 'home::index',
'uses' => 'IndexController@home',
'https'
]);
现在 route('home::index')
应该根据您定义的架构生成 URL。
我们定义了一个简单的路由,如下所示:
路线
// Home
Route::get('/home', [
'as' => 'home::index',
'uses' => 'IndexController@home'
]);
查看
<a href="{{ route('home::index') }}">Home</a>
我们需要强制我们的 link/route 视图呈现为 HTTP 或在某些情况下呈现为 HTTPS 路由,例如http://host.domain/home
或 https://host.domain/home
。
我们不能使用 URL::forceSchema("http")
或 URL::forceSchema("https")
,因为我们需要在 HTTP 页面上强制使用 HTTPS,在 HTTPS 页面上强制使用 HTTP。我们有一个多域应用程序。一些域 运行 通过 HTTP 一些通过 HTTPS。一个link到不同的域/"application section"可以放在任何地方。无法通过 HTTP 访问通过 HTTPS 访问的域 运行。无法通过 HTTPS
如何强制将路由呈现为特定的超文本协议?
Laravel documentaion 中有一个标题为强制通过 HTTPS 提供路由 的部分。它没有提到为路由生成 URL,但我可以在 Illuminate\Routing\UrlGenerator
代码中看到此设置在 getRouteScheme
方法中得到遵守。因此,向操作数组添加一个新值 http
/https
应该可以解决问题:
强制路由呈现为 HTTP:
Route::get('/home', [
'as' => 'home::index',
'uses' => 'IndexController@home',
'http'
]);
强制路由呈现为 HTTPS:
Route::get('/home', [
'as' => 'home::index',
'uses' => 'IndexController@home',
'https'
]);
现在 route('home::index')
应该根据您定义的架构生成 URL。