4
votes

If I wanted to make it so that every url call apart from ones I have defined after act upon /ExplicitControllerName/ExplicitActionToRun... how might the routing look like.

for example some pseudo code:

default_pathing:
    pattern:  /{controller}/{action}
    defaults: { _controller: Bundle:Default:index }

So if I went to www.example.com/Page/About

it would call my controller

class Page extends Controller
{
    public AboutAction()
    {
        // Called by above URL
    }
}

This question does not answer: Symfony2 / routing / use parameters as Controller or Action name

Imagine I have 100 pages with lots of sub routing pages doing pretty much the same routing every time. I want to do 1 routing for all those 100 controllers. How would we do this?

P.S I'm really going for something like the C#.NET MVC 4.0 routing in which it allows you to set a routing for a typical setup you might have even if at the very least its for development

2

2 Answers

2
votes

Your question is not totally clear but here are some hints.

I can imagine two use cases you're trying to solve:

1) You've a lot of some sort of CMS page, like your about example, these pages don't have much logic and just render some view, in such case you would something like:

class CMSPageController
{
    public function renderPage($pageSlug)
    {
        $page = $this->cmsPageRepository->findBySlug($pageSlug);

        // your logic to render the view
    }
}

And the according routing configuration:

<route id="cms_page_view" pattern="/cms/{pageSlug}">
  <default key="_controller">cms_page.controller.page:renderPage</default>
  <requirement key="_method">GET</requirement>
  <requirement key="slug">[0-9a-zA-Z\-\.\/]+</requirement>
</route>

2) You have much more complex requirements, and/or follow a specific pattern to name your controller/action, therefore you need to write a custom UrlMatcherInterface implementation. Take a look at the native implementation to know where to start. It would allow you define a fallback.

0
votes

This can be achieved using either SensioFrameworkExtraBundle's @Route annotation on class- and method-level excessively...

... or more elegant with less annotations using FOSRestBundle's automatic route generation with implicit resource names. Maybe you'll need to correct some of the generated routes using some of FOSRestBundle's manual route definition annotations.

Both methods originally still leave the need to explicitly add the route resources to your app/config/routing.yml.

Example import for @Route

# import routes from a controller directory
blog:
    resource: "@SensioBlogBundle/Controller"
    type:     annotation

Example import for FOSRestBundle

users:
    type:     rest
    resource: Acme\HelloBundle\Controller\UsersController

You could work around having to import all the resources by:

  • introducing a custom annotation (class-level)
  • creating a compiler pass or a custom route loader in which you ...
  • use the Finder to find all controller classes in all bundles with that annotation
  • finally add all those as resources with type annotation/rest to the route collection in there

If you don't plan to use hundreds of controllers and don't have too much experience with compiler-passes, custom annotations, ... etc. you'll definitely be faster just registering the resources in the routing config.