bulveyz/router-php
Composer 安装命令:
composer require bulveyz/router-php
包简介
Simple router for light projects PHP
README 文档
README
Very basic but functional router. It will be useful for simple projects, will help to implement the routing system quickly, as it is very easy and fast, you can use it both separately and in conjunction with other classes or patterns.
Install via composer
composer require bulveyz/router-php
Documentation
Example of use
use BulveyzRouter\Route; use BulveyzRouter\Router; Route::get('/home', function() { echo "Home"; }); Route::any('/user/{id}', function($param) { echo "User" . $param->id; }); Route::post('/create/post', 'PostController@store'); Router::routeVoid();
Don't forget to enable redirecting all requests to index.php in .htaccess. Here is one example:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L,QSA]
Usage
Connect the necessary classes for the router BulveyzRouter\Route and BulveyzRouter\Router can be connected separately (if you use singleton), but BulveyzRouter\Route must be called and routes must be defined before BulveyzRouter\Router::routeVoid(); And don't forget to call BulveyzRouter\Router::routeVoid(); before defined routes.
Example
index.php
use BulveyzRouter\Router; // Defained routes require_once '../routes.php'; // Run router Router::routeVoid();
routes.php
use BulveyzRouter\Route; Route::get('/home', function() { echo "Home"; });
Patterns for route
The parameters for the route are specified as {pattern}. The route should not have the same parameters, they should have different names.
Examples
Route::get('/user/{id}/{second_id}', function($params) { echo $params->id . $params->second_id; });
With controller
Route::get('/user/{id}/{second_id}', 'HomeController@index'); public function index($params) { echo $params->id . $params->second_id; }
Methods
This version supports only 3 methods:
- GET
- POST
- ANY
Set name for route
You can specify the name of the router and return it anywhere.
Example
Route::get('/home', 'HomeController@index')->name('home.index'); echo \route('home.index'); // return '/home'
Change nampespace for controllers
Of course, you can change the namespace for controllers, by default it is App\ .
Example
Route::setNamespace('Your namespace'); "Route::setNamespace('Classes');"
Output all defained routes and other data
var_dump(Route::routeList()); // Return namespace echo Route::getNamespace() // Return path for route echo \route('routeName'); Route::get('/user', function (){ // Return method this route echo Router::$method; // Return path this route echo Router::$route; // Return params this route (Array) var_dump(Router::$params); // Return handler this route var_dump(Router::$callback); });
bulveyz/router-php 适用场景与选型建议
bulveyz/router-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 203 次下载、GitHub Stars 达 9, 最近一次更新时间为 2018 年 06 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 bulveyz/router-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bulveyz/router-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 203
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 9
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-06-12