首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Laravel的框架中请求类的安全函数的扩展或覆盖功能,最佳操作?

在Laravel的框架中请求类的安全函数的扩展或覆盖功能,最佳操作?
EN

Stack Overflow用户
提问于 2013-09-15 21:59:44
回答 1查看 265关注 0票数 0

我目前正在使用Laravel 4,并且一直在探索如何覆盖请求::secure()方法,我正在编写一个应用程序,它将位于负载均衡器的后面,如果从负载平衡器应用的标头值,我宁愿函数返回true。

理想的做法是怎样做呢?我在这里读过这篇博文,http://fideloper.com/extend-request-response-laravel似乎有点过火了。

我不完全理解Laravel的正面概念?我的答案是如何做到这一点,这有可能吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-09-16 04:41:36

正如他在文章中提到的那样,扩展Request类与普通类略有不同。不过,更简单:

1.创建扩展的Request类并确保它可以自动加载;

ExtendedRequest.php

代码语言:javascript
复制
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

代码语言:javascript
复制
/*
|--------------------------------------------------------------------------
| 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

代码语言:javascript
复制
'aliases' => array(
    // ...
    'Request' => 'Raphael\Extensions\Request',
    // ...
),
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18817923

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档