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
                ->get('my_search_filter_controller')
                ->filterSearchAction(
                    $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: http://symfony.com/doc/current/cookbook/controller/service.html

    How liip uses it: https://github.com/liip/LiipImagineBundle#using-the-controller-as-a-service

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

报告相同问题?

悬赏问题

  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型