1
votes

In October CMS, you can easily create a link to a CMS page in a Twig template with reverse routing. For example:

<a href="{{ 'help'|page }}">Go to help page</a>

will link to the CMS page with the filename help.htm, substituting the URL with the URL defined in that page.

However, for the life of me, I can't figure out how to get the URL to a CMS page in a component to create a redirect to it.

I tried:

return Redirect::to('help');

which simply redirects to /help URL, which isn't the correct URL, so I get a 404.

I also tried:

return Redirect::to(\Url::route('help'));

However this produces an error because it is not registered as a route.

The reason I can't just hard-code the URL is because I am using the Translate plugin, which means that I can have a different URL depending on the language. So for example, in English it would be /en/help, but in Spanish it would be /es/ayuda. Putting:

{{ 'help'|page }}

in my Twig files will automatically get the correct URL based on the current language.

Is this possible to do in a component or controller?

1

1 Answers

3
votes

Within your component you can use controller's pageUrl method.

$this->controller->pageUrl(<file_name>, <params>);

according to your need, this should do the trick

return Redirect::to($this->controller->pageUrl('help'));

if any doubt please comment.