ilias/rhetoric
最新稳定版本:1.1.1
Composer 安装命令:
composer require ilias/rhetoric
包简介
Package addressed to handling routes with PHP
README 文档
README
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.
Installation
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
Usage
Step 1: Define Your Routes
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");
Step 2: Set Up Your Router
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();
Step 3: Create Controllers
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.
Step 4: Handling Middleware (Optional)
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"); });
Step 5: Dispatching Routes
Using the Request static method, dispatch(), you can handle the current route:
<?php Request::dispatch($requestMethod, $requestUri);
Dynamic params
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"
Explanations
-
::classIs recommended to use the static reference to your class, so te code does know exactly which class to use
统计信息
- 总下载量: 114
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-07-11