douchongbc72264
douchongbc72264
2013-05-18 21:28

Symfony2如何访问所有控制器中的一组数据?

已采纳

I have searched but I can't find any similar issues or maybe I am phrasing it wrong. What I want to achieve is access to an object in all the controllers in my Bundle. For example:

<?php

namespace Example\CoreBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class FolderController extends Controller
{

    function indexAction()
    {

         $title = $this->folder->getTitle();
         $description = $this->folder->getDescription();

    }

}

Usually outside of Symfony I would have extended the Controller class myself with BaseController extends Controller and set this up in the construct method but I know Symfony doesn't use the construct method so I am a bit stuck as to where to go.

I would usually do something like this:

class BaseController extends Controller
{

     function __construct()
     {

          parent::__construct();
          //load up the folder from my model with an ID
          $this->folder = $folder;

     }

}

I would then extend BaseController from FolderController and go from there but I have tried this with Symfony and it does not work. I have looked into Services but do not think this is what I need to make this work. If any more details are required please let me know, thanks.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • doushi3322 doushi3322 8年前

    If I understand your question correctly, services are indeed what you're looking for.

    First, define a service in services.yml:

    services:
        vendor.folder_manager:
            class: Vendor\FolderBundle\Entity\Manager\FolderManager
            arguments:
                em: "@doctrine.orm.entity_manager"
                class: Vendor\FolderBundle\Entity\Folder
    

    Then create that FolderManager class:

    <?php
    namespace Vendor\FolderBundle\Entity\Manager;
    
    use Doctrine\ORM\EntityManager;
    use Doctrine\ORM\EntityRepository;
    
    class FolderManager
    {
        protected $em;
    
        protected $repo;
    
        protected $class;
    
        public function __construct(EntityManager $em, $class) {
            $this->em = $em;
            $this->class = $class;
            $this->repo = $em->getRepository($class);
        }
    
        public function findById($id) {
            return $this->repo->findById($id);
        }
    
        public function getRepository() {
            return $this->repo;
        }
    }
    

    Finally, in your controller:

    $this->container->get('vendor.folder_manager')->findById($folderId);
    

    Or:

    $this->container->get('vendor.folder_manager')->getRepository()->findById($folderId);
    

    Symfony2 will automatically inject the entity manager the class into the manager, so all you have to provide in the controller is the folder's id.

    Edit: To make it prettier, you could also make a shortcut function in your controller:

    protected function getFolderManager()
    {
        return $this->container->get('vendor.folder_manager');
    }
    

    Then you can do:

    $this->getFolderManager()->findById($folderId);
    
    点赞 评论 复制链接分享