Content routing
Intro
XalokNext ships with the wf_cms.content_router
service that allows generating links to different "contents" of the CMS (articles, tags, categories, author pages).
Using this service instead of Symfony's router ($this->router->generate('wf_article_show', ['slug' => $article->getSlug()])
) is highly recommended!
On one hand, it's offering a simpler usage:
Example: Using Symfony's router to generate the link to an article:
$this->router->generate('wf_article_show', ['articleSlug' => $article->getSlug()]);
Example: Using the wf_cms.content_router
service to generate the link to an article:
$this->contentRouter->generate($article)
This also makes potential changes to the route easier - e.g.: if it asks for additional parameters, these can be passed from the ContentRouter
, without having to change all places that generate that route. For example, a project wanted to serve all video articles from videos.project.com
domain, instead of the main domain (www.project.com
).
Most importantly, for articles there are a few edge cases where the $router->generate
approach would lead to undesired results. For example: if an article has a redirection set up, the $router->generate
would still generate the URL of the old article, and the user would then be redirected to the final URL, while wf_cms.content_router
would generate the final URL.
PHP usage
After injecting the wf_cms.content_router
service, you can use it to generate links to different contents of the CMS.
$this->contentRouter->generate($article);
$this->contentRouter->generate($category);
$this->contentRouter->generate($tag);
$this->contentRouter->generate($author);
The generate
method accepts the following arguments:
content
the entity for which the link should be generatedplatform
the platform for which the link should be generated (optional, default isnull
). E.g.amp
,facebook
,instant
.routeParams
an array with extra parameters, if neededreferenceType
same as Symfony's$router->generate
third parameter, it can be used to generate absolute URLs. Besides the constants that Symfony offers, one can useWf\Bundle\CmsBaseBundle\Routing\ContentRouter::PUBLIC_URL
constant to generate public (absolute) URLs from admin pages.
Twig usage
Some functions are exposing wf_cms.content_router
functionality to twig files:
{{ wf_cms_content_path(article) }} {# similar to Symfony's `path` function - generates a relative URL #}
{{ wf_cms_content_url(article) }} {# similar to Symfony's `url` function - generates an absolute
JavaScript usage
The wfed/util/routing
dependency exposes a getContentLink
function that can be used to generate links, similar to the PHP ContentRouter:
define(["wfed/util/routing"], function ({ getContentLink }) {
// ...
const url = getContentLink(contentModel); // contentModel can be an article, category, tag, author
});