我目前正在使用 Laravel 4,并且正在徘徊如何覆盖 Request::secure() 方法,我正在编写一个应用程序,该应用程序将位于负载均衡器后面,因此我宁愿让函数返回 true,如果从负载均衡器应用标头值。
理想情况下应该如何做?我在这里读过这篇博文 http://fideloper.com/extend-request-response-laravel 这似乎有点过分了。
我不完全理解拉拉维尔的外墙概念?有没有可能这就是我如何做到这一点的答案?
正如 fideloper 在他的文章中提到的,扩展Request
类与普通类略有不同。不过更简单:
1. 创建扩展Request
类并确保它可以自动加载;
扩展请求.php
namespace RaphaelExtensions;
use IlluminateSupportFacadesResponse as IlluminateResponse;
class Response extends IlluminateResponse {
public function isSecure() {
return true;
}
}
请注意,我们扩展了 isSecure
方法,而不是 secure
。这是因为secure
只是从Symfony的基础Request
类中调用isScure
。
2. 确保 Laravel 使用您的扩展类。为此,请更改您的起始.php文件;
引导/启动.php
/*
|--------------------------------------------------------------------------
| Create The Application
|--------------------------------------------------------------------------
|
| The first thing we will do is create a new Laravel application instance
| which serves as the "glue" for all the components of Laravel, and is
| the IoC container for the system binding all of the various parts.
|
*/
use IlluminateFoundationApplication;
Application::requestClass('RaphaelExtensionsRequest');
$app = new Application;
$app->redirectIfTrailingSlash();
3. 确保在 app.php 配置文件中设置了正确的别名。
应用程序/配置/应用程序.php
'aliases' => array(
// ...
'Request' => 'RaphaelExtensionsRequest',
// ...
),