Sometimes we (web-developers) need to create a custom framework. Whenever I strongly recommend you choose this approach only when you can't convince other people in the team that it is a horrible idea. You don't have to build your own bicycle. But, If you want to... ok, let's 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 a 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 the controller's method according to request method and request string
I know, this code does not look so simple as you expected. But it does only comparing request string with a route pattern. It's extracting parameters from the pattern and set them to a variable (property of our class). Also, we're setting action that we need to perform when the pattern matches.
Therefore now we can implement request methods like
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 the next closure.
Finally implementing the 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 the parameter if it is callable.
Here is a full example of
And how to call it:
Comments are welcome :)