Route Engine

The Wiggum Framework's router is built on top of the nikic/fastroute component.

Basic Routing

Routing rules are defined in your app/boot/routes.php file. The most basic Wiigum routes simply accept a URI and a handler action, in the format ClassPath@method

// example.com/journals invokes the doDefault method in the controller class Blog 
$app->router->get('/journals', '\app\controllers\blog\Blog');

// example.com/docs invokes the allDocuments method in the controller class Documents 
$app->router->get('/docs', '\app\controllers\documents\Documents@allDocuments');
Available Router Methods

The router allows you to register routes that respond to any HTTP verb:

$app->router->get($uri, $callback);
$app->router->post($uri, $handler);
$app->router->put($uri, $handler);
$app->router->patch($uri, $handler);
$app->router->delete($uri, $handler);
$app->router->options($uri, $handler);
$app->router->any($uri, $handler);
$app->router->map($methods, $uri, $handler);

Route Parameters

Sometimes you will need to capture segments of the URI within your route. For example, you may need to capture a user's ID from the URL. You may do so by defining route parameters:

$this->router->get('user/{id}', '/app/controllers/users/UserController@showProfile');

The id is stored in the Request object as a parameter and can be retreived in your contorller method like this:

$id = $request->getParameter('id');
Another example with multiple parameters:
$this->router->get('posts/{postId}/comments/{commentId}', '/app/controllers/posts/CommentController@showComment');
Optional Parameters

You may define optional route parameters by enclosing part of the route URI definition in [...]. So, for example, /foo[bar] will match both /foo and /foobar:

$this->router->get('user[/{name}]', '/app/controllers/users/UserController@showProfile');
Regular Expression Constraints

You may constrain the format of your route parameters by defining a regular expression in your route definition:

$this->router->get('user/{name:[A-Za-z]+}', '/app/controllers/users/UserController@showProfile');

Route Closure

You can use a Closure as your handler, your Closure must return a array with the route actions.

The route actions that are available to you are:

classPath (required) The string is divided into the class path to your controller and a method to invoke in your controller seperated by a @ sign. If no method is provided doDefault is used as a default method. eg. \app\controllers\documents\Documents@allDocuments.
parameters (optional) Any paramaters you would like to attach to your Request object.
properties (optional) Any properties you would like to attach to your controller.
$app->addRoute('/docs', function($parameters) {
	$properties = ['accountId' => 123];
						
	return ['classPath' => '\app\controllers\documents\Documents@allDocuments', 'parameters' => $parameters, 'properties' => $properties];
});

Route Groups

Route groups allow you to share route attributes, such as prefix, middleware or filters, across a large number of routes without needing to define those attributes on each individual route. Shared attributes are specified in an array format as the first parameter to the $router->group method.

Route Prefix

The prefix group attribute may be used to prefix each route in the group with a given URI. For example, you may want to prefix all route URIs within the group with admin:

$app->router->group(['prefix' => '/admin'], function () use ($app) {
    $app->router->get('/users', function() {
        // Matches The "/admin/users" URL
    });
});
Middleware

To assign middleware to all routes within a group, you may use the middleware key in the group attribute array.

First register your middleware with the router:

$app->router->registerMiddleware('auth', function($request, $response, $next) {
    echo 'BEFORE';
	$response = $next($request, $response);
	echo 'AFTER';

	return $response;
});

Using the name you gave your middleware above, define your route group:

$app->router->group(['middleware' => 'auth'], function () use ($app) {
    // Both the below routes uses auth Middleware registered above
    
    $app->router->get('/', '/app/controllers/dashboard/DashController');

    $app->router->get('user/profile', '/app/controllers/users/UserController@showProfile');
});

Middleware Order

Route middlewares gets added after system middlewares and will be executed in the order you define them.

Filters

Filters allow you to munipulate a routes actions before being processed. For example you can chnage the controller classpath, or modify parameters or add properties.

To assign a filter to all routes within a group, you may use the filters key in the group attribute array.

First register your filter with the router:

$app->router->registerFilter('attachUserId', function($actions) {
    $actions['properties']['userId'] = $actions['paramters']['userId']; // attach userId to your controller as a property

	return $actions;
});

Using the name you gave your filter above, define your route group:

$app->router->group(['filters' => 'attachUserId'], function () use ($app) {
    // Both the below routes uses attachUserId Filter registered above
    
    $app->router->get('/', '/app/controllers/dashboard/DashController');

    $app->router->get('user/profile', '/app/controllers/users/UserController@showProfile');
});