Other Parts:
Having covered the basics of routing, views, and controllers, we can now focus on refining our router to address its current limitations. There are two primary issues with the existing router:
-
It cannot handle methods other than
GET
, such asPOST
. -
It does not support routing URIs with parameters, like
/posts/{post-slug}
.
We will now work on enhancing our router to overcome these challenges and make it more robust and versatile.
Handle Request Methods:
We will call a specified method name instead of add. So, let's add them to the Router class.
Let's update web.php
:
Add send
method to ContactController
:
I added a contact form from TailwindUI.
The important part is input names and form method:
Action is by default the current URI, which is /contact
:
After submitting the form, we will get:
![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687650277995/35a7c307-bb02-456e-88ab-4d863d32f6bc.png align="center")
We have successfully handled the POST method also.
HTML Form Method problem:
HTML forms natively support only GET
and POST
methods. However, there are workarounds to handle DELETE
, PUT
, and PATCH
methods within HTML forms. One common approach is to include a hidden input field in the form and set its value to the desired method name. This way, when the form is submitted, you can access the method value on the server side and handle the request accordingly.
Let's add a small change to the Router class:
Handle routes with parameters
We will have to update the matchRoute method. Let's do that, and I will explain it after.
These parts haven't changed:
In the updated router, it attempts to match /{param}
if it exists as a defined route. If the /{param}
route is not found, it proceeds to match the server URI with the route URI defined by the user in the web.php
file. This approach allows for more flexible routing, accommodating both fixed routes and routes with parameters dynamically.
This line, find matches with Regex and stores them in an array:
It returns this:
So, if $paramMatches[0]
is empty, there is no param, and it continues then as a simple URI.
Else, we have another array named: $paramKey
, to store keys.
Next, we explode the route URI /post/{slug}
to get the param index later:
To get the index, we loop through $uri
. If a match is found, it will be added to $indexNum
.
Note: This function can match multiple params, that's why we have the arrays instead of
int
andstring
. e.g./team/{team_id}/{user_id}
.
Now that we have obtained the index of parameters, our next step is to match them with the server URI. To accomplish this, we can utilize regular expressions (regex) to establish the necessary pattern matching. Regex enables us to define and compare patterns to identify and extract the desired parameters from the server URI.
Example 1:
Example 2:
And, to do that:
For the first example, the $params
will be:
And Server URI will be now /post/{.*}
.
Finally, let's match the URIs:
Final Test:
I will add a new route to web.php
routes:
And, add the post method to HomeController
:
If we visit this route, we will get:
Conclusion:
Great news! With the implementation of multiple request methods handling and URI parameter routing, our router has reached a fully functional state at this level. Now, we can proceed to incorporate a database and make our application dynamic.
See you at the next one!