我正在关注Symfony 2 网站上的 How to Override any Part of a Bundle页面。这是有趣的:
您可以通过在 app/config/config.yml 中设置来将保存服务的类名的参数设置为您自己的类。当然,这只有在类名被定义为包含服务的捆绑包的服务配置中的参数时才有可能。
所以我看了看,/vendor/symfony/src/Symfony/Bundle/FrameworkBundle/Resources/config
发现这session.xml
是定义%session.class%
参数,所以应该很容易扩展 SymfonySession
类,如下所示:
namespace Acme\HelloBundle\Component\HttpFoundation;
use Symfony\Component\HttpFoundation\Session;
class ExtendedSession extends Session
{
public function setSuccessFlashText($text, array params = array())
{
parent::setFlash('success', $this->getTranslator()->trans($text, $params);
}
}
我还没有测试过这个。但是我怎样才能对request
特殊服务做同样的事情呢?我想添加一些方便的快捷方式,以使我的代码更易于阅读。
我在services.xml
文件中找到了这个:
<!--
If you want to change the Request class, modify the code in
your front controller (app.php) so that it passes an instance of
YourRequestClass to the Kernel.
This service definition only defines the scope of the request.
It is used to check references scope.
-->
<service id="request" scope="request" synthetic="true" />
这是我的app.php
。我应该如何传递我的自定义请求类的实例?
require_once __DIR__.'/../app/bootstrap.php.cache';
require_once __DIR__.'/../app/AppKernel.php';
//require_once __DIR__.'/../app/AppCache.php';
use Symfony\Component\HttpFoundation\Request;
$kernel = new AppKernel('prod', false);
$kernel->loadClassCache();
//$kernel = new AppCache($kernel);
$kernel->handle(Request::createFromGlobals())->send();