6

Here is the simple route defined in the custom bundle

my_admin_route:
    pattern:  /admin/{name}
    defaults: { _controller: NamespaceCustomBundle:CustomControl:login }

Above routing code will call the CustomControlController's method loginAction() my question is how can i automate the function name in routing like for each function i don't have to define the route again there should be one route and call the function automatically as defined parameter {name} in the route like below

my_admin_route:
    pattern:  /admin/{name}
    defaults: { _controller: NamespaceCustomBundle:CustomControl:{name} }
M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118

3 Answers3

5

you can look at KNP Rad Bundle: http://rad.knplabs.com/

It does a lot of good things including the one you are talking about

Shaheer
  • 2,105
  • 4
  • 25
  • 39
2

You probably want to create a custom route loader, see.

As far as I know there is currently no out-of-the-box solution to directly map the controllers -> methods -> parameters to a specific route controller/method/param1/param2 like other frameworks do (CodeIgniter, FuelPHP...).

Onema
  • 7,331
  • 12
  • 66
  • 102
2

Indeed this can be achieved with custom route loader as @Onema said.

I can think of two other options, none of which do exactly what you wanted but may be of interest:

1. Creating a controller action which would just forward request to other actions
2. Using @Route annotation

1.

In AdminController create action:

public function adminAction($actionName)
{
    return $this->forward('MyBundle:TargetController:' . $actionName);
}

2.

Annotation routing since it allows you to define routes without naming them. Name will be implicitly created by convention: Annotation routing.

Doesn't do exactly what you wanted but is pretty elegant too if you don't want to make custom route loader:

/**
 * @Route("/admin/dosmt")
 */
public function dosmtAction()
{
    return new Response('smtAction');
}

Additionally you can mount all controller actions on a prefix just like with YAML routing:

/**
 * @Route("/admin")
 */
class MyController extends Controller
{
    /**
     * @Route("/dosmt")
     */
    public function dosmtAction()
    {
        return new Response('smtAction');
    }
}
Igor Pantović
  • 9,107
  • 2
  • 30
  • 43