使用Symfony,我将显示一个表,其中包含一些用户可以从中进行选择的条目。有一点更复杂,因为这可能包括调用一些进一步的操作,例如过滤表项,按不同的标准排序等。
我已经在一个自己的包中实现了整个东西,假设是ChoiceTableBundle
(与ChoiceTableController
)。现在我希望能够从其他包中使用这个包,有时需要更多的参数化。
我想要的工作流程是这样的:
- 用户当前正在使用Bundle
OtherBundle
并触发chooseAction
。 -
chooseAction
转发到ChoiceTableController
。 - 在
ChoiceTableBundle
中,用户可以导航,过滤,排序,…使用这个bundle提供的动作和路由。 - 当用户做出选择时,他触发另一个动作(如
choiceFinishedAction
),控制流返回到OtherBundle
,交出用户选择的结果。根据这些结果,OtherBundle
可以继续工作。
此外,OtherOtherBundle
(和更多…)也应该能够使用这个工作流,可能传递一些配置值给ChoiceTableBundle
,使其行为有点不同。
我读过Symfony 2的"控制器即服务"模式,我认为这是正确的方法(如果不是,请告诉我;))。所以我将从ChoiceTableController
中创建一个服务,并从其他捆绑包中使用它。无论如何,考虑到上面的工作流程,我没有看到一个"好"的方法来实现这一点:
- 如何将配置参数传递给
ChoiceTableBundle
(resp. 14) ?ChoiceTableController
),如果有必要的话? -
ChoiceTableBundle
如何知道它被调用的位置? - 我如何将结果返回给这个调用bundle?
基本方法可以是将值存储在会话中或创建要传递的中间对象。在我看来,两者都不是特别优雅。你能把我推到正确的方向吗?提前感谢!
主要的问题是,如果你真的需要调用你的过滤/搜索逻辑作为一个控制器动作。你真的需要提出要求吗?
我想说,这也可以通过传递所有所需的数据到您定义的服务。
您应该从ChoiceTableBundle
的内部创建此服务,并让您的ChoiceTableBundle
和OtherBundle
使用提取的服务。
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')
}
}
<标题> 控制器方法整个过程可以用lipp/imagine bundle使用的相同方法完成。
有一个控制器作为服务,当你需要一些结果时,调用/发送所有需要的信息给控制器,你也可以发送整个请求。
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
// ..
}
}
单独的服务类将更加灵活。此外,如果您需要其他参数或请求对象,您可以随时提供它。
如何将controller声明为service的信息在这里:http://symfony.com/doc/current/cookbook/controller/service.html
唇彩的用法:https://github.com/liip/LiipImagineBundle using-the-controller-as-a-service
标题>