我做了自己的URLGenerator(Symfony \ Component \ Routing \ Generator \ UrlGenerator),扩展了原始的URLGenerator。我想从那里访问Symfony2会话,但我不能。
use Symfony\Component\Routing\Generator\UrlGenerator as BaseUrlGenerator;
class UrlGenerator extends BaseUrlGenerator
{
protected function doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $referenceType, $hostTokens)
{
$url=parent::doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $referenceType, $hostTokens);
**//Here I want to access to session**
return $url;
}
}
我读了documentation,但我不知道怎么走。只能从控制器访问会话? 我搜索here,但我想调用一个Symfony对象进行会话。
\ Resoruces \配置\ services.yml
parameters:
router.options.generator_base_class: Ex\Bundle\ExBundle\Routing\Generator\UrlGenerator
更新1: 我补充说:
ex.service:
class: Ex\Bundle\ExBundle\Routing\Generator\UrlGenerator
arguments: [@session]
和:
private $session;
public function __construct($session, RouteCollection $routes, RequestContext $context, LoggerInterface $logger = null)
{
$this->routes = $routes;
$this->context = $context;
$this->logger = $logger;
$this->session = $session;
}
protected function doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $referenceType, $hostTokens)
{
$url=parent::doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $referenceType, $hostTokens);
$this->session->set($url,$url);
return $url;
}
**错误在这里 - > set(:在非对象上调用成员函数set())
答案 0 :(得分:0)
正如Maerlyn所说,也许你应该用一个结构来实现它。 看看如何在服务中使用参数:http://symfony.com/doc/current/book/service_container.html
更新1:检查第二个答案。它可能会有所帮助How do you access a users session from a service in Symfony2?
更新2:
对我来说这个代码有效,但我不知道我是否覆盖Symfony \ Component \ Routing \ Generator \ UrlGenerator是一个很好的做法。
应用程序/配置/ config.yml
ex.service:
class: Test\YourBundle\Service\UrlGenerator
arguments: [@router]
calls:
- [setSession, ["@session"]]
的src /测试/ YourBundle /服务/ UrlGenerator.php
namespace Test\YourBundle\Service;
use \Symfony\Component\Routing\Generator\UrlGenerator as BaseUrlGenerator;
use \Symfony\Component\Routing\RequestContext as RequestContext;
class UrlGenerator extends BaseUrlGenerator {
private $session;
public function __construct(\Symfony\Component\Routing\Router $routes, \Psr\Log\LoggerInterface $logger = null) {
parent::__construct($routes->getRouteCollection(), new RequestContext(), $logger);
}
public function setSession($session) {
$this->session = $session;
}
public function getSession() {
return $this->session;
}
protected function doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $referenceType, $hostTokens)
{
$url=parent::doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $referenceType, $hostTokens);
$this->session->set($url,$url);
return $url;
}
}
的src /测试/ YourBundle /控制器/ DefaultController.php
class DefaultController {
// ...
public function indexAction()
{
// Retrieve my service
$service = $this->get('ex.service');
echo $service->generate('_demo');
}
// ...
}
答案 1 :(得分:0)
尝试此解决方案
use Symfony\Component\HttpFoundation\Session\Session;`
$session = new Session();
$session->get('variable');