dongqi19827 2015-10-26 16:00
浏览 34

控制器即服务 - 如何在高级案例中传递和返回值?

Using Symfony, I am displaying a table with some entries the user is able to select from. There is a little more complexity as this might include calling some further actions e. g. for filtering the table entries, sorting by different criteria, etc.

I have implemented the whole thing in an own bundle, let's say ChoiceTableBundle (with ChoiceTableController). Now I would like to be able to use this bundle from other bundles, sometimes with some more parametrization.

My desired workflow would then look like this:

  1. User is currently working with Bundle OtherBundle and triggers chooseAction.
  2. chooseAction forwards to ChoiceTableController (resp. its default entry action).
  3. Within ChoiceTableBundle, the user is able to navigate, filter, sort, ... using the actions and routing supplied by this bundle.
  4. When the user has made his choice, he triggers another action (like choiceFinishedAction) and the control flow returns to OtherBundle, handing over the results of the users choice.
  5. Based on these results, OtherBundle can then continue working.

Additionally, OtherOtherBundle (and some more...) should also be able to use this workflow, possibly passing some configuration values to ChoiceTableBundle to make it behave a little different.

I have read about the "Controller as Service" pattern of Symfony 2 and IMHO it's the right approach here (if not, please tell me ;)). So I would make a service out of ChoiceTableController and use it from the other bundles. Anyway, with the workflow above in mind, I don't see a "good" way to achieve this:

  • How can I pass over configuration parameters to ChoiceTableBundle (resp. ChoiceTableController), if neccessary?
  • How can ChoiceTableBundle know from where it was called?
  • How can I return the results to this calling bundle?

Basic approaches could be to store the values in the session or to create an intermediate object being passed. Both do not seem particularly elegant to me. Can you please give me a shove in the right direction? Many thanks in advance!

  • 写回答

1条回答 默认 最新

  • douwendu2460 2015-10-26 17:12

    The main question is if you really need to call your filtering / searching logic as a controller action. Do you really need to make a request?

    I would say it could be also doable just by passing all the required data to a service you define.

    This service you should create from the guts of your ChoiceTableBundleand let both you ChoiceTableBundle and your OtherBundle to use the extracted service.

    service / library way

    // register it in your service container
    class FilteredDataProvider
        * @return customObjectInterface or scallar or whatever you like
        public function doFiltering($searchString, $order)
            return $this->filterAndReturnData($searchString, $order)
    class OtherBundleController extends Controller {
         public function showStuffAction() {
             $result = $this->container->get('filter_data_provider')
                  ->doFiltering('text', 'ascending')

    controller way

    The whole thing can be accomplished with the same approach as lipp/imagine bundle uses.

    Have a controller as service and call/send all the required information to that controller when you need some results, you can also send whole request.

    class MyController extends Controller
        public function indexAction()
            // RedirectResponse object
            $responeFromYourSearchFilterAction = $this->container
                    $this->request, // http request
                    'parameter1' // like search string
                    'parameterX' // like sorting direction
            // do something with the response
            // ..

    A separate service class would be much more flexible. Also if you need other parameters or Request object you can always provide it.

    Info how to declare controller as service is here:

    How liip uses it:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀