Lumen-like routes

Prelude

Sometimes we (web-developers) need to create custom framework. Whenever I strongly recommend you to choose this approach only when you can't convince other people in the team that it is horrible idea. You don't have build your own bicycle. But, If you want to... ok, lets begin with building user-friendly routes :)

How it's look like

Very simple ha? So how to achieve that?

First of all we have to create PHP class and name it Route. This class will be obviously our router. So you have to put routing-logic here

Let's implement method-processing logic. I mean that we have to call controller's method according to request method and request string

I know, this code is not looks so simple as you expected. But it do only comparing request string with route pattern. It's extracting parameters from pattern and set them to variable (property of our class). Also we're setting action that we need to perform when pattern match.

Therefore now we can implement request methods like GET, POST, etc. For example:

The next step is to implement grouping routes. In this example we'll use only url prefix (without any middleware).

Grouping method accumulating prefixes and executing next closure.

Finally implementing route processor. As we want to use Laravel / Lumen style - we'll use @ symbol to tell what Controller and what Method we should call. Also we'll check parameter if it is callable.

Here is full example of Route.php

And how to call it:

Comments are welcome :)