Building Your First Route - chsxf/mfx GitHub Wiki
What is a Route?
Basically, the route path is the part of the URL that MFX will parse to know what function in your code to call as entry point.
For example, if using the default router with the following URL:
https://myfancywebsite.com/contact/form
https
is the protocolmyfancywebsite.com
is the domain namecontact/form
is the route path
MFX's default router will then parse contact/form
into the route provider contact
and the route form
. The route provider is then mapped to the Contact
class and the route to the form
public function of this class.
For more detailed information on routes or routeurs, go to the Framework Reference.
Updating the Configuration
Now we know routes represent a function in a class, we have to tell MFX where to find those classes. To do that, we have to modify the composer.json
file.
For more information on PHP and Composer class autoloading, go to the official PHP documentation and to the Composer documentation.
Open the composer.json
file and add the autoload
directive as follows:
"autoload": {
"psr-4": {
"": "routes"
}
}
Those lines add the routes
folder to the autoload process, informing MFX to look into the routes
folder for classes.
Run the following command to update your website configuration:
composer update
Creating the Route's Class
Let's say we want to create a route called testroute/hello
.
Create a folder named routes
at the root level and a file named TestRoute.php
in it.
[!TIP] Your class name does not need to match the same case as the route name as PHP class resolver is case-insensitive.
At this point, your repository should look like this:
📁 .git
📁 application
📄 .htaccess
📄 entrypoint.php
📁 config
📄 config.php
📁 routes
📄 TestRoute.php
📁 vendor
📄 autoload.php
📁 chsxf
📁 composer
... and other things
Then, open TestRoute.php
and paste these lines in it:
<?php
use chsxf\MFX\Attributes\AnonymousRoute;
use chsxf\MFX\Attributes\Route;
use chsxf\MFX\Routers\IRouteProvider;
use chsxf\MFX\RequestResult;
class TestRoute implements IRouteProvider
{
#[Route, AnonymousRoute]
public static function hello(): RequestResult {
echo 'Hello, world!';
exit();
}
}
This code includes three mandatory steps:
- The
TestRoute
class implements theIRouteProvider
interface. This is mandatory for classes to be eligible as route providers. - As stated earlier, routes must be public functions. So is
hello
. - But all public functions are not suitable for routing. You have to opt-in functions as routes through the
#[Route]
attribute. - The route is also marked with the
#[AnonymousRoute]
attribute to indicate it can be reached even if no user is currently signed in.
[!NOTE] It is possible to disable user authentication completely if all routes can be reached anonymously. However, as it most likely won't be the case for many websites or APIs you will write, we recommend using the
#[AnonymousRoute]
attribute instead.
Final Test
Now go to http://your.complete.website.url/testroute/hello
through your web browser and it should display Hello, world!
.
Congratulations! You have just completed your very first route.