【问题标题】:How to access to the Symfony2 session from anywhere?如何从任何地方访问 Symfony2 会话?
【发布时间】:2014-04-01 16:56:54
【问题描述】:

我做了我自己的 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\config\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;
}

** ERROR here on ->set (: Call to a member function set() on a non-object in )

【问题讨论】:

  • 您可以扩展构造函数以接受 SessionInterface 对象并在那里注入@session 服务;你试过吗?
  • 您能否更新您的问题以包含您注册 url 生成器的部分?
  • 你看到这个问题了吗? stackoverflow.com/questions/13901536/… 它告诉你如何在你的类中使用容器,你可以使用它来获取 session 服务。

标签: php session symfony


【解决方案1】:

正如 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 是否是一个好习惯。

app/config/config.yml

ex.service:
  class: Test\YourBundle\Service\UrlGenerator
  arguments: [@router]
  calls:
       - [setSession, ["@session"]]

src/Test/YourBundle/Service/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/Test/YourBundle/Controller/DefaultController.php

class DefaultController {
    // ...
    public function indexAction()
    {
            // Retrieve my service
            $service = $this->get('ex.service');
            echo $service->generate('_demo');
    }
    // ...
}

【讨论】:

  • 但我只能在控制器中访问,不是吗?
  • Symfony 2 提供了一个容器(其中包含服务)。您可以使用命令行列出它们:php app/console container:debug。在第二个链接(第二个答案)中,您有一个示例。
  • 完成此操作后,我得到:“FatalErrorException:错误:调用 C:\xampp\htdocs\Symfony\src\Ex\Bundle\ 中非对象上的成员函数 set() ExBundle\Routing\Generator\Urlgenerator.php line 42" 当我将它调用到 $this->session->set('ex','ex');
  • Urlgenerator 是一项服务吗?您是否将会话注入该服务?
  • 你从哪里得到 __construct() 中的其他参数?我尝试了没有 doGenerate() 方法的代码,并且对象 Session 被正确检索。也许是由于受保护的方法/继承类?我不知道。
【解决方案2】:

试试这个解决方案

use Symfony\Component\HttpFoundation\Session\Session;`

$session = new Session();
$session->get('variable');

【讨论】:

    猜你喜欢
    • 2010-11-03
    • 2012-12-01
    • 2017-03-20
    • 2011-11-03
    • 1970-01-01
    • 1970-01-01
    • 2010-10-11
    • 1970-01-01
    • 2015-04-11
    相关资源
    最近更新 更多