The route component defines the system route based on the php attribute
To install PHP Router, You can run the following command directly at your project path in your console:
composer require hm-hajjaji/route
In your project's index.php file, create an instance of the HttpRoute
class and pass the parameter controller_dir
path to your project controllers
<?php
//index.php
use Route\Http\HttpRoute;
//get autoload of composer
require_once dirname(__DIR__)."/vendor/autoload.php";
//create object from class HttpRoute
(new HttpRoute("path controllers"))->resolve();
Create a controller and add Route
attribute to the action index
<?php
namespace App\Controller;
use Route\Route;
class HomeController
{
#[Route("/home","app_home")]
public function index()
{
echo "welcome home";
}
}
Run your own localhost server Example :
php -S localhost:8000
And finally go to url http://localhost:8000/home .
Prefix action route in one controller
<?php
namespace App\Controller;
use Route\Route;
#[Route("/user")]
class HomeController
{
#[Route("/home","app_home")]
public function index()
{
echo "welcome home";
}
#[Route("/show","app_show")]
public function show()
{
echo "welcome show";
}
}
You can pass multiple parameters for the routing
<?php
namespace App\Controller;
use Route\Route;
class HomeController
{
#[Route("/home/{name}","app_home")]
public function index($name)
{
echo "welcome $name";
}
}
Now to access to action do the following:
go to url http://localhost:8000/home/name_value .
Output the : welcome name_value
You can recuperate the path
from the route name
using the path function
:
<a href="<?=HttpRoute::path("app_home",['name' => 'Home'])?>">Home</a>
The path will be: http://localhost:8000/home/Home
.