Routers

A PHP router is a component or script in a web application that directs incoming HTTP requests to the appropriate handler or controller based on predefined rules. Read more about it here.

MaplePHP is built upon nikic/FastRoute, but MaplePHP has added additional functionality. FastRoute works exceptionally well and employs regular expressions for advanced users. You can add your routers in "app/Http/Routes/web.php" or create your own like "api.php" and so on.

Bind controller to request

Like most other frameworks and routers, you can bind HTTP requests to different methods:

HTTP GET Request

$routes->get("/{page:about}", ['Http\Controllers\Pages', "about"]);

HTTP POST Request

$routes->post("/{page:about}", ['Http\Controllers\Pages', "about"]);

HTTP PUT Request

$routes->put("/{page:about}", ['Http\Controllers\Pages', "about"]);

HTTP DELETE Request

$routes->delete("/{page:about}", ['Http\Controllers\Pages', "about"]);

HTTP Custom Request

$routes->map("GET", "/{page:about}", ['Http\Controllers\Pages', "about"]);

HTTP Multiple Requests

Grouping

What really makes MaplePHP unique is its group function, where you can group on both or just one of the URI pattern and middlewares:

Group with URI Path Pattern

The URI Path Pattern example shown above is a basic example but will likely be the one you use the most. However, there are more advanced patterns that I will show below.

Group with middlewares

Why would you want to group with middlewares? This design is user-friendly and pedagogical, providing an efficient way to add functionality and improve performance by loading only the necessary extras. The above example starts a session and adds navigation to the pages. This way, you can for example incorporate multiple different navigations.

Group with both Pattern and Middleware

A before and after method will be called in each middleware, meaning before and after the grouped routes controller has been executed. However, you can also bind a specific method to the before call, as demonstrated above with PublicZoneand the Document class.

You can probably see now what makes MaplePHP Routing so user-friendly.

Nesting group

What makes MaplePHP truly great is that you can also group already grouped routes endlessly.

Patterns

It is possible to use Regular Expression (Regex). Form more information you can also click here.

It is, however, recommended to at least bind a URI path to a pattern key. This is because you can later very easily extract the path with MaplePHP Url (UrlInterface) instance.

Advanced patterns

Find all, strings and numbers (counts as one parameter)

Allow only numbers

Find everything for dynamic parameters

IF match or else

It is also highly recommended to attach a KEY to a pattern. With the example above you can write more complete routes like bellow.

Add a new router file

You might want to add a new router file to organize your routes. You do this by editing the "config/routers.php" file:

Last updated

Was this helpful?