1

I'm trying to build my own CMS based on Symfony 2 since I swapped jobs and now require some symfony practice, but got stuck when trying to do something sensible with routes. In Zend it was possible with preDispatch so I hope Symfony 2 will have something similar too.

Consider this URL: http://www.example.com/page_one/sub_page/another_sub_page/yet_another_sub/

What I am trying to achieve is to have http://www.example.com/page_one/ matching to some controller and getting me the content of the page that has the url_string parameter same as that portion of URL. Then http://www.example.com/page_one/sub_page/ should also match to the same controller but render content for the sub_page portion of URL.

The route must match n portions because I cannot know how deep the structure might go. So I will not accept something like

/{slug1} or 
/{slug1}/{slug2} or 
/{slug1}/{slug2}/{slug3}

as that is nonsense.

I am planning to make this route as last one in the routing config so should there be some matches on other routes they will be picked up instead, and this one despite it is most important route will be last resort matching kinda thing.

Zistoloen
  • 879
  • 14
  • 29
tomhre
  • 295
  • 1
  • 4
  • 15
  • 3
    http://stackoverflow.com/questions/11415965/routing-an-unlimited-number-of-parameters – iswinky Aug 08 '14 at 09:07
  • @iswinky thanks dude I think this might work – tomhre Aug 08 '14 at 09:20
  • @iswinky actually not so sure, i have used "^.+" and despite i should have prior matches they are not being picked up at all any idea on how to match to /store route instead of the above if available – tomhre Aug 10 '14 at 20:43
  • @iswinky my bad dude, who would have guessed that var_dump on doctrine object that references other object will cause apache to die, lol, hate u Doctrine crap, at least var_export does not brake it – tomhre Aug 10 '14 at 22:42
  • I tried to implement this on the weekend, and it works pretty well, you just have make sure you place any additional routes above this one, or it will run for everything. I'll write an answer – iswinky Aug 11 '14 at 07:48
  • @iswinky I know, this one needs to be last otherwise it will always be picked up, I had a problem with the store bundle, because before I added category mapping I used var_dump to check what i got from DB, that breakes as soon as you have some mapping between the tables, thanks a lot man, shame I cannot give u more points – tomhre Aug 11 '14 at 08:00
  • Thought I'd share an answer anyway, every little helps! Good luck – iswinky Aug 11 '14 at 09:05

1 Answers1

1

Using the following config will match all urls, and the entire path will be passed into the controller "slug1/slug2/slug3".

SymfonyBundle/DefaultBundle/Resources/config/routing.yml:

symfony_default_page:
    path:     /{path}
    defaults: { _controller: SymfonyBundle:Default:page }
    requirements:
        path: "^.+"

Or** ou can also use regex in the requirements path. This will exclude "admin", "login", "blog", however it's much easier if you simplify things and just place the routing in the correct order like you said

requirements:
    path: "^(?!admin|login|blog).+"

app/config/routing.yml

By importing the default bundle routing last, it means any routing yml files above will take priority and therefore not match the default route which will catch any url.

SymfonyAdminBundle:
    resource: "@SymfonyAdminBundle/Resources/config/routing.yml"
    prefix:   /admin

#import this last
SymfonyDefaultBundle:
    resource: "@SymfonyDefaultBundle/Resources/config/routing.yml"
    prefix:   /

Controller:

class DefaultController extends Controller
{
    public function pageAction($path)
    {
        //You could split the URL and handle accordingly, also filter / escape?
        $parts = explode("/", $path);
    }
}
iswinky
  • 1,951
  • 3
  • 16
  • 47
  • got almost the same, just needed to add extra path to catch / for homepage, actually started editing my own question at work, guess i never submitted edits, lol. Now just to figure out how to if template or included template does not exists render something from default templates, some sort of theming system – tomhre Aug 11 '14 at 19:22
  • I'm working on a cms using Symfony2! Could use git and collaborate? I am just getting started on mine though. – iswinky Aug 11 '14 at 19:40
  • very early stages for me also this is my way of learning symfony, it would be nice to exchange some ideas though. I had it with wordpress – tomhre Aug 11 '14 at 19:45