一个控制器的两个路由或一组操作的两个路由

时间:2017-01-03 15:41:33

标签: symfony routing

以下是所需网址的方案:

/service/getBalance    should map to CustomerController::getBalance
/service/addBalance    should map to CustomerController::addBalance

/customer/getBalance    should map to CustomerController::getBalance
/customer/addBalance    should map to CustomerController::addBalance

这是一个简单的控制器

<?php

namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class CustomerController extends Controller {

    /**
     * @Route("/getBalance")
     */
    public function getBalanceAction(Request $request) {

    }

    /**
     * @Route("/addBalance")
     */
    public function addBalanceAction(Request $request) {

    }

} // class CustomerController

尝试过以下方法。他们都没有工作。

# rounting.yml
v1:
    resource: "@AppBundle/Controller/CustomerController.php"
    prefix:   /service
    type:     annotation

v2:
    resource: "@AppBundle/Controller/CustomerController.php"
    prefix:   /customer
    type:     annotation

加载具有不同前缀的相同资源始终会覆盖上一个事件(最后一个出现)。以下也不会出于同样的原因而产生相同的行为。

# rounting.yml
v1:
    resource: "@AppBundle/Resources/config/routing_customer.yml"
    prefix:   /service

v2:
    resource: "@AppBundle/Resources/config/routing_customer.yml"
    prefix:   /customer


# routing_customer.yml
getBalance:
    path: /getBalance
    defaults : { _controller: "AppBundle:Customer:getBalance" }

addBalance:
    path: /addBalance
    defaults : { _controller: "AppBundle:Customer:addBalance" }

第三个不工作选项:

# rounting.yml
v1:
    resource: "@AppBundle/Resources/config/routing_v1.yml"
    prefix:   /     # evenr putting /service here instead of inside

v2:
    resource: "@AppBundle/Resources/config/routing_v2.yml"
    prefix:   /     # evenr putting /customer here instead of inside

# routing_v1.yml
getBalance:
    path: /service/getBalance
    defaults : { _controller: "AppBundle:Customer:getBalance" }

addBalance:
    path: /service/addBalance
    defaults : { _controller: "AppBundle:Customer:addBalance" }

# routing_v2.yml
getBalance:
    path: /customer/getBalance
    defaults : { _controller: "AppBundle:Customer:getBalance" }

addBalance:
    path: /customer/addBalance
    defaults : { _controller: "AppBundle:Customer:addBalance" }

我实际上需要将//customer路由到同一个控制器: /getBalance/customer/getBalance

我想为一组方法提供两个前缀。如何在Symfony中做到这一点?

我的试验结论,@ goto的回答和@ Cerad的评论:

如果我使用不同的路由名称,上面的最后一个示例可能会有效。路线名称在整个项目中是唯一的(不仅仅是文件中的唯一)。 v1_getBalancev2_getBalance

另一个解决方案是使用custom loader作为@goto描述。

1 个答案:

答案 0 :(得分:3)

你可以这样做路线:

     @Route(
          "/{subject}/getBalance",
          requirements={
              "subject": "customer|service"
          }
      )

in yml:

subject_getbalance:
  path:     /{subject}/getBalance
  requirements:
      subject:  customer|service

这个要求比没有任何东西更安全:它允许你在另一个控制器上路由另一条路由,如foo/getBalance(因为它与要求不符)

编辑:对于您的特殊情况,您需要/ getBalance也可以映射到您的路线:

subject_getbalance:
      path:     /{subject}/getBalance
      default: { _controller: YourBundle:Controller }
      requirements:
          subject:  customer|service
default_getbalance:
      path:     /getBalance
      default: { _controller: YourBundle:Controller, subject: customer }

编辑:最后一个想法是custom route loader(但我从未尝试过):

class ExtraLoader extends Loader
{
    public function load($resource, $type = null)
    {

       /* ... */

        $prefixes = [ 'default_' =>'','customer_' =>'/customer','service_' =>'/service']

        // prepare a new route
        $path = '/getbalance/{parameter}';
        $defaults = array(
            '_controller' => 'YourBundle:Actiona',
        );
        $requirements = array(
            'parameter' => '\d+',
        );
       foreach($prefixes as $prefixName => $prefixRoute) {
          $route = new Route($prefixRoute . $path, $defaults, $requirements);
          $routes->add($prefixName . 'getBalance', $route);
       }

这将允许您生成3条路线:

default_getBalance: /getBalance
customer_getBalance: /customer/getBalance
service_getBalance: /service/getBalance