This PHP router system allows you to define and manage your application's routes in a simple and organized manner, inspired by Laravel's routing system.
To install the package, add it to your composer.json
file:
{
"require": {
"ilias/rhetoric": "1.0.0"
}
}
Or simply run the terminal command
composer require ilias/rhetoric
Then, run the following command to install the package:
composer install
Create a file to define your routes, for example, in your project root folder, routes.php
:
<?php
use Ilias\Rhetoric\Router\Router;
Router::get("/", IndexController::class . "@handleApiIndex");
Router::get("/favicon.ico", IndexController::class . "@favicon");
Router::get("/asset", AssetController::class . "@instruction");
Router::group(['prefix' => '/asset'], function ($router) {
$router->group(['prefix' => '/type/{type}'], function ($router) {
$router->get("/name/{name}", AssetController::class . "@getAssetByName");
$router->get("/id/{id}", AssetController::class . "@getAssetById");
});
});
Router::get("/debug", DebugController::class . "@showEnvironment");
In your application's entry point, typically index.php
, set up the router to handle incoming requests:
<?php
require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/routes.php';
use Ilias\Rhetoric\Router\Router;
Router::setup();
Create your controller classes to handle the requests. For example, create IndexController.php
:
<?php
namespace Ilias\Rhetoric\Controller;
class IndexController
{
public function handleApiIndex()
{
echo "Welcome to the API!";
}
public function favicon()
{
// Handle favicon request
}
}
Similarly, create other controller classes like AssetController.php
and DebugController.php
as needed.
If you want to use middleware, create a middleware class implementing Ilias\Rhetoric\Middleware\Middleware
:
<?php
namespace Ilias\Rhetoric\Middleware;
use Ilias\Rhetoric\Middleware\Middleware;
class ExampleMiddleware implements Middleware
{
public static function handle()
{
// Middleware logic here
}
}
Then, apply middleware to your routes or route groups:
Router::get("/protected", IndexController::class . "@protectedMethod", [ExampleMiddleware::class]);
Router::group(['prefix' => '/admin', 'middleware' => [ExampleMiddleware::class]], function ($router) {
$router->get("/dashboard", AdminController::class . "@dashboard");
});
Using the Request
static method, dispatch()
, you can handle the current route:
<?php
Request::dispatch($requestMethod, $requestUri);
Using the Request
static attribute, $params
, you can access an associative array:
<?php
Router::get("/user/{username}/config", Authenticate::class . "@userConfigurations");
When you access the route http://your.dev.api.com/user/iloElias/config
, the params will be stored in Request::$params
as:
echo Request::$params["username"] //"iloElias"
-
::class
Is recommended to use the static reference to your class, so te code does know exactly which class to use