tdw/routing 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

tdw/routing

Composer 安装命令:

composer require tdw/routing

包简介

Simple router to GET, POST, PUT, PATCH, DELETE requests.

README 文档

README

Simple router to GET, POST, PUT, PATCH, DELETE requests.

Build Status

Requirements

PHP: >=7.0

Install

$ composer require tdw/routing

require 'to/path/vendor/autoload.php';

Usage

Instance

<?php

$router = new \Tdw\Routing\Router();

Closure

<?php

$router->addGET(new \Tdw\Routing\Route('/', function (){
    echo 'Home page';
}, 'home'));

Action and route name

<?php

class ArticleAction
{
    /**
     * Route GET
     */
    public function index()
    {
        //
    }

    /**
     * Route POST
     */
    public function save()
    {
        //
    }
}

$router->addGET(new \Tdw\Routing\Route('/articles', 'ArticleAction@index', 'article.index'));
$router->addPOST(new \Tdw\Routing\Route('/articles/save', 'ArticleAction@save', 'article.save'));

Rule

<?php

$route = (new \Tdw\Routing\Route('/article/{slug}/{id}', function () {
    //
}, 'articles.show'))
    ->addRule('slug', new \Tdw\Routing\Rule\Slug())
    ->addRule('id', new \Tdw\Routing\Rule\Id());
$router->addGET($route);

Match

<?php

$currentRoute = $router->match(\GuzzleHttp\Psr7\ServerRequest::fromGlobals());

var_dump($currentRoute);

Exceptions

\Tdw\Routing\Exception\RouteNotFoundException
\Tdw\Routing\Exception\RouteNameNotFoundException

Suport for methods

- GET
- POST
- PUT
- PATCH
- DELETE

Integration suggestion

<?php

class App
{
    /**
     * @var \Tdw\Routing\Contract\Router
     */
    private $router;

    function __construct(\Tdw\Routing\Contract\Router $router)
    {
        $this->router = $router;
    }

    function run(\Psr\Http\Message\ServerRequestInterface $request)
    {
        if ($route = $this->router->match($request)) {
            if ($route->getCallback() instanceof Closure) {
                return call_user_func_array($route->getCallback(), $route->getParameters());
            }
            list( $action, $method ) = explode('@', $route->getCallback());
            return call_user_func_array([new $action, $method], $route->getParameters());

        }
        return new \GuzzleHttp\Psr7\Response(404, [], 'Page Not Found');
    }
}

(new App($router))->run(\GuzzleHttp\Psr7\ServerRequest::fromGlobals());

Contributing

Add new features.

License

The Tdw Routing is open-sourced software licensed under the MIT license.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-10-31

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固