Multiple templates in a single action
- blashstar
- Member | 2
I'm developing a site with friendly urls like that:
http://domain/<name>
The problem is that <name> can be a blog post or a section.
can i change the template inside an action?
I need invoque the blog or section template depending of the page type to
render.
- GEpic
- Member | 566
I'm developing a site with friendly urls like that:
you are trying to build friendly URLs, but it's too much (and not
SEO-friendly).
It's always better to
use http://domain/<category/section/post>/<name>
can i change the template inside an action?
yes, you can do it with following and it calls corresponding render method:
$this->setView('blog / post / section / whatever')
be always sure that you are setting another view in action method
Last edited by GEpic (2017-02-21 00:59)
- GEpic
- Member | 566
dkorpar wrote:
IMHO you are looking in wrong direction. You need to implement this in routing (determine which url run which Presenter:Action) and not switching template.
The problem is that @blashstar wants simple route likes this for everything, then probably look up database for record with that slug with additional information what record type (blog, section, new, etc.) it is and with this information set another view.
$router[] = new Route('<slug>', 'Homepage:default');
That's really bad idea (described above) and can be done nicer, but there is a solution for what he wants.
Last edited by GEpic (2017-02-21 16:37)
- GEpic
- Member | 566
dkorpar wrote:
I got that, but then he just needs to create his route (class implementing IRouter) and there in match method define which presenter should be called. This problem has to be solved on routing level and not like this.
But then you must call database on routing level, cause the slugs will be the same for all the posts / pages / etc., don't know what's better atm. :/
If you have 10 static pages, then you can write them into the router directly.
But if sections / blog / etc. is created dinamically, then you have no other options.
Last edited by GEpic (2017-02-22 00:34)
- mrtnzlml
- Member | 140
Calling database on routing level is absolutely fine and in fact I prefer that. But if you really want – there is another approach for inspiration. It's maybe three years old, but it still works in production.
It works only with this simple route rule. Nowadays I would implement it directly in the router but it may be quite complicated especially for newcomers.