我目前正在使用Laravel 4,并且一直在探索如何覆盖请求::secure()方法,我正在编写一个应用程序,它将位于负载均衡器的后面,如果从负载平衡器应用的标头值,我宁愿函数返回true。
理想的做法是怎样做呢?我在这里读过这篇博文,http://fideloper.com/extend-request-response-laravel似乎有点过火了。
我不完全理解Laravel的正面概念?我的答案是如何做到这一点,这有可能吗?
发布于 2013-09-16 04:41:36
正如他在文章中提到的那样,扩展Request类与普通类略有不同。不过,更简单:
1.创建扩展的Request类并确保它可以自动加载;
ExtendedRequest.php
namespace Raphael\Extensions;
use Illuminate\Support\Facades\Response as IlluminateResponse;
class Response extends IlluminateResponse {
public function isSecure() {
return true;
}
}注意,我们扩展了isSecure方法,而不是secure。这是因为secure只是从Symfony的基类Request类中调用isScure。
2.确保Laravel使用扩展类。为此,请修改start.php文件;
bootstrap/start.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 Illuminate\Foundation\Application;
Application::requestClass('Raphael\Extensions\Request');
$app = new Application;
$app->redirectIfTrailingSlash();3.确保在app.php配置文件中设置了正确的别名。
app/config/app.php
'aliases' => array(
// ...
'Request' => 'Raphael\Extensions\Request',
// ...
),https://stackoverflow.com/questions/18817923
复制相似问题