Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve redirect routing #348

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open

Conversation

johanjanssens
Copy link
Member

@johanjanssens johanjanssens commented May 18, 2020

This PR improves the redirect router. It adds support for callback based routing alongside pattern based routing which was already possible.

Redirect routing is available globally, redirects can be setup outside of the context of Pages for any url on your Joomla site

Global redirects

The changes implemented as part of #344 make it possible to implement redirects in Joomla outside of the context of Pages.

Global configuration

You do not need to setup a /joomlatools-pages folder to be able to define redirects. If you just want to use this feature add a configuration-pages.php to your Joomla root, and defines your redirects.

<?php
return array('redirects' => [
    '/articles/[:alias]' => '/blog/posts/[:alias],
    '/login'  => ['resolve' => function($route)
    {
	if(isset($route->query['view']) && $route->query['view'] == 'registration')
	{
            $route->path = '/register';
            unset($route->query['view']);

            return true;
	 }
     }],
  ]
);

Configure caching

You can configure the redirect cache lifetime through the cache query parameter in the route target. It will set the max-age value for the Cache-Control header when the response is send.

The max-age parameter indicates that the file is to be considered stale after its age is greater than the specified number of seconds.

By setting a cache liftetime you instruct CDN's and browser cache the redirect for a specific time. This helps to increase the performance of your site and is recommended practice.

The cache parameters offers support for English textual relative datetime formats as supported by strtotime, for example:

  • 1day
  • 2hours
  • 15min

See also: joomlatools/joomlatools-framework#373

For example, set the cache lifetime to 1day

>> '/articles/[:alias]' => '/blog/posts/[:alias]/?cache=1day,

This allows pages to be to redirect any url, anywhere, even when not
in a page context.
identifiers by a custom scheme and rename 'pages' scheme to 'page'
The callback is defined is as follows: 'function($route, $generate = false)'

- $route: a ComPagesDispatcherRouteRouteInterface object
- $generate: are we generating a url or resolving (default false)

Callbacks are both supported for static and dynamic routes, in case of a
dynamic route the callback is called only if the route could be succesfully
resolved.
Example:

'/path/to/page' => [
	'generate' => function($route)
	{
		return true;
	},
	'resolve' => function($route)
	{
		return true;
	}
],
@johanjanssens johanjanssens requested a review from ercanozkaya May 18, 2020 23:08
@johanjanssens johanjanssens linked an issue Jun 24, 2020 that may be closed by this pull request
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve redirect routing
1 participant