相关疑难解决方法(0)

如何将容器作为服务的参数

在我的服务构造函数中

public function __construct(
        EntityManager $entityManager,
        SecurityContextInterface $securityContext)
{
     $this->securityContext = $securityContext;
    $this->entityManager = $entityManager;
Run Code Online (Sandbox Code Playgroud)

我将entityManager和securityContext作为参数传递.我的services.xml也在这里

    <service id="acme.memberbundle.calendar_listener" class="Acme\MemberBundle\EventListener\CalendarEventListener">
        <argument type="service" id="doctrine.orm.entity_manager" />
        <argument type="service" id="security.context" />
Run Code Online (Sandbox Code Playgroud)

但现在,我想在服务中使用容器

$this->container->get('router')->generate('fos_user_profile_edit') 
Run Code Online (Sandbox Code Playgroud)

我怎样才能将容器传递给服务?

php symfony symfony-2.3

29
推荐指数
4
解决办法
5万
查看次数

Symfony 如何在我的服务中获取容器

我有项目,在这个项目中,我有很大的自己的服务,它庞大而复杂,有自己的依赖项等......我想为这个服务创建,目的是使用我的服务controllers

$myService = $this->container->get('service_from_my_domain');
Run Code Online (Sandbox Code Playgroud)

我的问题是如何在我的门面内部访问container服务的依赖项。我只知道一种方法 - 是在 yaml 配置中将依赖注入到服务中。
但是还有其他方法可以做到吗?喜欢:

$dependency = Container::getInstance()->get('my_dependency_service');
Run Code Online (Sandbox Code Playgroud)

我找到了这个答案,但是使用全局变量感觉就像回到了过去......

PS:我不想通过 yaml 配置(不是构造函数注入或 setter 注入)注入依赖项,因为我在这里不需要 IoC()。

php design-patterns facade inversion-of-control symfony

11
推荐指数
2
解决办法
2万
查看次数

访问实体存储库或Anywhere中的Symfony配置参数

我有很多配置设置,我希望能够在整个应用程序中访问.例如,我有一个包含一些自定义查询的实体存储库.我想有一个全局参数,将默认的"限制"设置为10条记录,除非我专门更改它.

class ViewVersionRepository extends EntityRepository {

    /**
     * Get all the versions for the specified view id, ordered by modification time.
     * @param integer $id
     * @param integer $limit
     * @param integer $offset default 0
     * @return array
     */
    public function findByViewIdLimit($id, $limit = NULL, $offset = 0) {

        // this won't work because we don't have access to the container... ;(
        $limit = ($limit != NULL) ? $limit : $this->container->getParameter('cms.limit');

        return $this->createQueryBuilder('v')
            ->where('v.viewId = :id')
            ->orderBy('v.timeMod', 'DESC')
            ->setParameter('id', $id) …
Run Code Online (Sandbox Code Playgroud)

php symfony

5
推荐指数
1
解决办法
1万
查看次数