joomlatools-pages icon indicating copy to clipboard operation
joomlatools-pages copied to clipboard

Route callbacks

Open johanjanssens opened this issue 4 years ago • 0 comments

This PR adds support for route callbacks and improves the flexibility of the routing API.

Callback

The callback is defined is as follows:

'/path/to/page' => [
    'generate' => function($route)
     {
	 return true;
     },
    'resolve' => function($route)
     {	
	 return true;
     }
],

Where $route is a ComPagesDispatcherRouteRouteInterface object.

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 successfully resolved.

Examples

The changes are specifically added to improve flexibility for redirect routing.

1. Redirect based on url query parameter

Redirect /login?view=registration to /register

'redirects' => [
    '/login'  => ['resolve' => function($route)
     {
	if(isset($route->query['view']) && $route->query['view'] == 'registration')
	{
		$route->path = '/register';
		unset($route->query['view']);

		return true;
	}
     }],
]

2. Redirect based on dynamic route to other site

'redirects' => [
    '/articles/[:alias]' => ['resolve' => function($route)
     {
 	$route->path = 'http://example.com/blog/posts/'.$route->query['alias'];
	unset($route->query['alias']);
	return true;
     }],		
]

johanjanssens avatar May 15 '20 02:05 johanjanssens