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

Route callbacks #344

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

Route callbacks #344

wants to merge 5 commits into from

Conversation

johanjanssens
Copy link
Member

@johanjanssens johanjanssens commented May 15, 2020

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;
     }],		
]

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 changed the title Add support for route callbacks Route callbacks May 16, 2020
@johanjanssens johanjanssens mentioned this pull request Jun 17, 2020
@johanjanssens johanjanssens linked an issue Jun 28, 2020 that may be closed by this pull request
1 task
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.

Route callbacks
2 participants