How to automate function name in symfony routing?
Asked Answered
H

3

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} }
Humidify answered 22/9, 2013 at 13:5 Comment(0)
I
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

Intermittent answered 23/9, 2013 at 10:12 Comment(0)
F
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...).

Formyl answered 22/9, 2013 at 16:28 Comment(0)
V
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');
    }
}
Venge answered 23/9, 2013 at 7:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.