定制 richardandrade/php_router 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

richardandrade/php_router

Composer 安装命令:

composer require richardandrade/php_router

包简介

A simple router for managing your app routes

README 文档

README

Simple php router library for managing your APIs/Apps routes.

This library is only meant for study purposes or small projects.


How to install it

composer require richardandrade/php_router

How to use it


1. In your Front Controller, you'll need to instanciate the Router Class with your source (or app) namespace as a param:

use RichardAndrade\PhpRouter;

$router = new Router('app\example\\');

2. After initializing the class you'll then need to define your application routes, I recommend creating a new file like 'routes.php' and then including it in your Front Controller:

include_once('routes.php');
//in 'routes.php' file

$router->setRoute([
      'path' => '/home', //Route path
      'methods' => ['GET'], //Here you'll need to define the allowed HTTP Methods as an array (e.g ['GET','POST','PUT'])
      'controller' => 'MyController' //The name of the controller responsible for this route
]);

3. Finally, after defining your routes and including the file in the Front Controller, call the following method to start the router:

$router->run();

The final result (index file) should be something like:

use RichardAndrade\PhpRouter;

$router = new Router('app\example\\');

include_once('routes.php');

$router->run();

Defining dynamic routes


With this library you'll also be able to define dynamic routes. With this type of route your application will receive any params that you defined in the request URI.

//routes such as 'myapp.com/article/1', where '1' could be the article ID, can be configured as shown bellow

$router->setRoute([
      'path' => '/article/{id}', //in this case I used 'id', this will be the key name for the 'params' associative array
      'methods' => ['GET'],
      'controller' => 'ArticlesController'
]);

//after that, your controller will be able to receive the defined params

class ArticlesController {
  public funtion __construct(array $params)
  {
  }
}

//the 'params' variable will provide, in this example, ['id' => '1']



extra: setting headers


This library also provides a simple way to define the response headers inside your controllers:

use RichardAndrade\PhpRouter;

class ArticlesController {
    public function example()
    {
        Router::setHeaders([
              'Content-type' => 'application/xml',
              'Access-Control-Allow-Origin' => 'https://myapp.com'
        ]);
    }
}

统计信息

  • 总下载量: 7
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2023-06-22

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固