在Symfony 3中搜索等效的sfAction(每个文件一个动作)

时间:2017-11-11 12:30:53

标签: symfony-3.3

是否有可供选择的操作语法在单独的文件中调度操作 - 每个操作一个文件。原因:CVS协作的可能性更大(合并冲突更少)。我找到了这个解决方案,但是我觉得从名为Controller的类扩展一个名为Action的类感觉不舒服。 (因为下面的渲染方法,我需要Controller类)。将控制器作为参数传递不起作用,因为它不是可注入服务。有一种不那么难看的方式吗?例如,在services.yml中定义规则,让操作自动从" Controller"类?

THX

 // ./src/AppBundle/Action/LayoutsAction.php

 <?php  

 namespace AppBundle\Action;

 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
 use Symfony\Component\HttpFoundation\Request; 
 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
 use Symfony\Component\Serializer\Serializer;

 class LayoutsAction extends Controller {
     private $serializer;

     /**
     * The action is automatically registered as a service and dependencies are autowired.
     * Typehint any service you need, it will be automatically injected.
     */
     public function __construct(Serializer $serializer)
     {
         $this->serializer = $serializer;
     }

     /**
     * @Route("/", name="homepage")
     */
     public function __invoke(Request $request)
     {
         return $this->render('layout-editor/layouts.html.twig', [
             'initialState' => $this->serializer->normalize([
                 'layouts' => $this-get('layouts.repository.layout')-findAll()
             ])
         ]);
     }
 }

我的services.yml看起来像这样:

// ./app/config/services.yml

...

services:
     _defaults:
         autowire: true
         autoconfigure: true
         public: false

     AppBundle\:
         resource: '../../src/AppBundle/*'
         exclude: '../../src/AppBundle/{Entity,Repository,Tests}'

     AppBundle\Action\:
         resource: '../../src/AppBundle/Action'
         public: true

     AppBundle\Controller\:
         resource: '../../src/AppBundle/Controller'
         public: true
         tags: ['controller.service_arguments']
...

0 个答案:

没有答案