lotfio / aven
强大的PHP路由器
0.3.0
2020-06-29 20:04 UTC
Requires
- php: ^7.2
Requires (Dev)
- phpunit/phpunit: ^8.0
This package is auto-updated.
Last update: 2024-09-21 03:01:57 UTC
README
🚜 强大的PHP路由器 🚜
🔥 简介
Aven (ayven) 是一个强大且 灵活 的 PHP 路由器,适用于 PHP7 及更高版本。
📌 要求
- PHP 7.2 或更高版本
- PHPUnit >= 8(用于测试目的)
🚀 安装与使用
composer require lotfio/aven
✏️ 使用方法
1- 快速使用
<?php require_once 'vendor/autoload.php'; // here we are using $_SERVER['REQUEST_URI'] // you can use $_GET['uri'] $router = new Aven\Router($_SERVER['REQUEST_URI']); $router->get('/', function(){ // with a callback return "welcome from aven"; }); $router->get('/', "UsersController@method"); // controller method $router->get('/', "UsersController::method"); // controller static method $router->init(); // initialize router
2- 可用路由
GET
,POST
,ANY
,PUT
,DELETE
,HEAD
<?php $router->get('/', function(){ return " this is get method"; }); $router->post('/', function(){ return " this is post method"; }); $router->any('/', function(){ return " this is any method allows all"; }); $router->put('/', function(){ return " this is put method. you should send $_POST['_method'] = 'put'"; }); $router->delete('/', function(){ return " this is delete method. you should send $_POST['_method'] = 'delete'"; }); $router->head('/', function(){ return " this is head method. you should send $_POST['_method'] = 'head'"; });
3- 命名路由
<?php $router->get('/', function(){ return "this is get named route (default)";})->name('default');
4- 重定向
$router->redirect(string $routeName, array $params = [], int $httpcode = 301)
<?php // route 1 $router->get('/', function() use($router){ // accessing this route will redirect you to route2 means /hola $router->redirect('route2'); // if parametrised route you can pass array of parameters })->name('default'); // route 2 $router->get('/hola', function(){ return " welcome to hola from default route";})->name('route2');
5- 路由参数
- 你可以使用括号或花括号来定义参数
- 预定义参数
:int
,:integer
,:num
,:numeric
,:number
= \d+:str
= \w+:alpha
= [A-z]+
<?php $router->get('/test/(:int)', function(){}); // evaluates to /test/\d+ $router->get('/test/(:str)', function(){}); // evaluates to /test/\w+ // optional parameters (if optional parameter uri should end with /) $router->get('/test/(:id*)', function(){}); // optional id /test/ or /test/1 $router->get('/test/(:id?)', function(){}); // zero or one id /test/ or /test/0-9
6- 自定义路由参数
->regex(array $params)
<?php // override predefined param $router->get('/test/(:str)', function(){})->regex(array(":str"=> '[my-reg-ex]')); // custom param $router->get('/test/(:hola)', function(){})->regex(array(":hola"=> '[my-reg-ex]'));
7- 路由组
$router->group(string $uri,callable $callback, ?string $groupName)
- 你可以有任意数量的嵌套组
<?php $router->group('/mygroup', function($router){ // groups adds prefixes to routes $router->get('/test', function(){ return "from /mygroup/test" }); // evaluates to /mygroup/test }); // multiple groups $router->group('/group1', function($router){ $router->group('/group2', function($router){ $router->get('/test', function(){ return "from /group1/group2/test" }); // evaluates to /group1/group2/test }); });
8- 路由命名空间
$router->namespace(string $uri,callable $callback)
- 你可以有任意数量的嵌套命名空间
<?php $router->namespace('My\\Namespace', function($router){ // you can also use dots (My.Namespace) instead of \\ $router->get('/test', "TestController@test"}); // evaluates to My\\Namespace\\TestController@test }); // multiple groups $router->namespace('ns1', function($router){ $router->namespace('ns2', function($router){ $router->get('/test', "TestController@test"); // evaluates to ns1\\ns2\\TestController@test }); });
9- 其他路由
$router->form(string $uri, $callback|$class, ?array $override, ?string $routeName)
<?php // form route with callback $router->form('/login', function(){ }); // works both with GET and POST // form route with class $router->form('/login', Login::class); // by default class should have showForm & submitForm // override default form methods $router->form('/login', Login::class, ['get','post']); // named form method $router->form('/login', Login::class, ['get','post'], 'login.form');
10- 其他路由
$router->crud(string $uri, string $class, ?array $only, ?string $routeName)
<?php // crud route // this needs a User class with 4 methods create,read,update,delete // create => POST user/create // read => GET with optional pareter user/read/ // update => PUT with optional pareter user/update/ // delete => DELETE with optional pareter user/delete/ $router->crud('/user', User::class); // disable some methods $router->crud('/user', User::class, ['c']); // only create $router->crud('/user', User::class, ['create']); // only create $router->crud('/user', User::class, ['c', 'u']); // create & update $router->crud('/user', User::class, ['create', 'update']); // create & update // named crud $router->crud('/user', User::class, NULL, 'myCrud'); // name can be used for redirections
💻 贡献
- 感谢您考虑为 软件包 贡献。所有贡献指南均在此处说明:here。
📃 更新日志
- 您可以在以下位置找到 ChangeLog。
🍺 支持开发
- 分享 软件包 并让我们获得更多的星标和贡献者。
- 如果这个项目帮助您节省了开发时间,您可以请我喝杯咖啡 :) : Paypal. 💖
📋 许可证
- 软件包 是一个开源软件,许可协议为 MIT。