ozziest / patika
简单的PHP路由库。
1.1.1
2015-12-31 15:45 UTC
Requires (Dev)
- phpunit/phpunit: 4.8.*
This package is not auto-updated.
Last update: 2024-09-14 18:52:48 UTC
README
Patika 是一个简单的路由包,您可以在项目中轻松使用。这是一个小巧且实用的包,因为您不需要定义所有路由。您应该编写控制器代码而不是路由定义。
安装
要通过 composer 安装,只需将以下内容放入您的 composer.json
文件中
{ "require": { "ozziest/patika": "dev-master" } }
$ composer update
使用方法
首先,您应该定义 .htaccess
文件,以便处理所有请求并将其发送到 index.php
文件。
.htaccess
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
index.php
文件必须定义如下:
// Including composer autoload file include 'vendor/autoload.php'; // First of all, you should use try-catch block for handling routing errors try { // You must create a new instance of Manager Class with the app argument. $patika = new Ozziest\Patika\Manager(['app' => 'App\Controllers']); // And calling the route! $patika->call(); } catch (Ozziest\Patika\Exceptions\PatikaException $e) { // If the controller or method aren't found, you can handle the error. echo $e->getMessage(); }
这就是全部!现在 Patika 路由器 已经激活。现在,您可以根据需要定义控制器。
Users.php
namespace App\Controllers; class Users { /** * All * * @return null */ public function all() { echo 'App\Controllers\Users@all()'; } }
检查
$ php -S localhost:8000 index.php
$ curl -X GET localhost:8000/users/all
完整文档
您可以在 完整文档 中阅读!