Giter VIP home page Giter VIP logo

tast-router's Introduction

TastRouter License Code Climate Build Status

A Simple PHP Router

  • 支持RESTful风格
  • 支持反向路由
  • 支持动态参数绑定
  • 支持对参数正则检验
  • 支持Yaml格式的路由配置

Requirements

PHP5.4+

composer方式获得

{
    "require":{
        "xujiajun/tast-router":"dev-master"
    }
}

当然也可以直接clone

Usage

step1

sudo composer install

step2:


//web/index.php

require __DIR__.'/../vendor/autoload.php';
use TastRouter\Route;
use TastRouter\Router;
use TastRouter\RouteCollection;

$collection = new RouteCollection();

$controller = 'TastRouter\\App\\Controllers\\UserController';

//普通用法
$collection
->attachRoute(new Route('/user/do',[
    '_controller' => "$controller::doAction",
    'methods' => 'GET',
]));

//使用正则
$collection
->attachRoute(new Route('/user/{user}',[
    '_controller' => "$controller::indexAction",
    'methods' => 'GET',
    'user'=>'\w+',
//    'id'=>'\d+',
]));

//路由名绑定
$collection
->attachRoute(new Route('/hello/{hello}',[
    '_controller' => "$controller::indexAction",
    'methods' => 'GET',
    'hello'=>'\w+',
    'routeName'=>'say_hello',//bind route name
//    'id'=>'\d+',
]));


$router = new Router($collection);
$route = $router->matchCurrentRequest();

//解析路由
echo $router->generate('say_hello',['hello'=>'xujiajun']);// 输出 /hello/xujiajun

以上用法太麻烦?

TastRouter也支持Yaml的配置.方便管理你的路由:

//web/index.php

require __DIR__.'/../vendor/autoload.php';
use TastRouter\Router;
use Symfony\Component\Yaml\Yaml;

$file = __DIR__.'/../src/Config/routes.yml';
$array = Yaml::parse(file_get_contents($file));
$router = Router::parseConfig($array);
$route = $router->matchCurrentRequest();

License

MIT Licensed

tast-router's People

Contributors

xujiajun avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.