symfony-cmf / routing-auto-bundle

Adding automatic route generating on top of the symfony cmf routing
https://cmf.symfony.com
14 stars 29 forks source link

Method to call Controllers and Templates based on class #66

Closed awdng closed 9 years ago

awdng commented 10 years ago

The DynamicRouter has the possibility to call different controllers and template by class or type, but for AutoRouting it doesn't work because the Route Documents are all of the same type. This doesn't seem to be documented so i wonder if its possible at all.

Of course this could be done in other ways, i was just wondering if there is a way to do it like DynamicRouter intends to.

dantleech commented 10 years ago

Using custom controllers or templates by content class will work the same as with "normal" routing.

Specifying controller / template by route "type" would require the AutoRoute document to have the _type option set, but not sure there is much of a use case for that?

dbu commented 10 years ago

the class is always the class of the content document the route points to, not the route class. so i think this should already work. the type however would not work - but if i am not mistaken, autoroute is based on the content class, so there would be no use to set a type from the configuration. @dantleech if he would set a type manually afterwards, that would stay intact until the route has to be moved? or even when its moved around?

awdng commented 10 years ago

hm ok, yeah class is all i need but i cant get that to work, it will always go to the default controller

dynamic:
        enabled: true
        generic_controller: cmf_content.controller:indexAction
        controllers_by_class:
            Kuoni\BackendBundle\Document\Page: 'cmf_content.controller:indexAction'
            Kuoni\BackendBundle\Document\Continent: 'some other controller'
        templates_by_class:
            Kuoni\BackendBundle\Document\Page: 'KuoniBackendBundle:Page:show.html.twig'
            Kuoni\BackendBundle\Document\Continent: 'KuoniBackendBundle:Page:show_continent.html.twig'
            Kuoni\BackendBundle\Document\Home: 'KuoniBackendBundle:Page:show.html.twig'
        persistence:
            phpcr:
                route_basepath: /cms/routes

this doesnt change anything in my case, is it because the ContentBundle interferes with that ?

cmf_content:
    default_template: 'KuoniBackendBundle:Page:show.html.twig'
    persistence:
        phpcr:
            content_basepath: /cms/content

If this works i would volunteer to add an example to the docs, if/where it makes sense.

dantleech commented 10 years ago

I don't use ContentBundle, but I don't think that would intefere. I use controllers_by_class on my homepage:

https://github.com/dantleech/dtlweb/blob/master/app/config/config.yml#L106

Note that it is somewhat redundant to specify templates by class when you specify a controller (you can render the correct template from the controller).

dantleech commented 10 years ago

@dbu the options don't get changed automatically, so yeah you could modify the type afterwards, I wouldn't recommend it though.

dbu commented 10 years ago

@dantleech is the route tree of the RoutingAutoBundle at /cms/routes or somewhere else?

dantleech commented 10 years ago

@dbu the routes are handled by the DynamicRouter so they need to be in the configured path, i.e. /cms/routes, but if you don't mind them not working, they can be anywhere :)

awdng commented 10 years ago

Ok thanks this works for me now, i can't even tell why it didn't before. I know that controllers_by_template is redundant but i was just testing it instead. Thanks a bunch, could be closed from my end but i could still update the docs with an example if that would make sense.

dbu commented 10 years ago

well, it is not strictly wrong to use both - you could map several of your documents to the same controller but pass different templates. that is a valid scenario, or also if you don't want to hardcode a template in your controller, can be more elegant to let the dynamic router tell it.

wouterj commented 9 years ago

So let's close this, as it turns out it is already possible. Only the *_by_type isn't possible, but we kinda agree that there isn't much usecase for this.