承接 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
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

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