承接 alexya-framework/router 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

alexya-framework/router

Composer 安装命令:

composer require alexya-framework/router

包简介

Alexya's Router utilities

README 文档

README

Alexya's routing components

Contents

Instantiating Router objects

The router translates the HTTP requests and routes them through different specified callbacks until one can handle it.

First you'll need to instance a Router object that will route the requests.

The constructor accepts as parameter a string being the base path.

Example:

<?php
$Router = new \Alexya\Router\Router();
// will route all requests of `/`
// /forum
// /forum/thread/1
// /blog
// /blog/post/1

$Router = new \Alexya\Router\Router("blog");
// will route all requests of `/blog` (if `/forum` is requested, it will be ignored).
// /blog
// /blog/post/1

Adding routes

Once the router has been instantiated you will have to add the routes, you can do this using the method add. The first parameter is the regular expression to match, the second parameter is the callback to execute if the regular expression is matched, the third parameter is an array containing the methods where the regular expression should be tested (if it's empty it will be tested globaly).

There are 2 ways for calling this method:

  • Adding a single route.
  • Adding multiple routes.

Adding a single route

To add a single route you need to provide at least the first 2 arguments:

  • The regular expression that the router should match.
  • The callback to call when the expression is matched.

Optionally you can send a 3rd parameter that can be either an array or a string, this parameter will contain the method(s) on which the expression should me matched.

Example:

<?php

$Router->add("/blog/post/([0-9]*)", function($id) {
    echo "Requested post: {$id}";
});

$Router->add("/forum/thread/([0-9]*)", function($id) {
    echo "Requested thread: {$id}";
}, ["GET", "POST"]);

Adding multiple routes

To add multiple routes you need to provide a single parameter that is an array which contains all the routes to match, each index of the array is an array that contains at least 2 index:

  • The regular expression that the router should match.
  • The callback to call when the expression is matched.

Optionally you can add a 3rd index that can be either an array or a string, this index will contain the method(s) on which the expression should be matched.

Example:

<?php

$Router->add([
    [
        "/blog/post/([0-9]*)",
        function($id) {
            echo "Requested post: {$id}";
        }
    ],
    [
        "/forum/thread/([0-9]*)",
        function($id) {
            echo "Requested thread: {$id}";
        },
        ["GET", "POST"]
    ]
])

Matching routes

Once all routes have been added you will need to call the route method which will parse the request through all routes, if the request matches a route, its callback will be executed, if not, it will throw an exception of type \Alexya\Router\Exceptions\NoRouteMatch.

The default route

To avoid a exception to be thrown, you can use the method setDefault which accepts as parameter the \Alexya\Router\Route object to be executed if the request doesn't match any route. Alternatively, you can use the method add and send {DEFAULT} as the regular expression.

Example:

<?php
$Router = new \Alexya\Router\Router("blog");

$Router->add("/post/([0-9]*)", function($id) {
    echo "Requested post: {$id}";
}, "GET");

$Router->add("{DEFAULT}", function() {
    echo "The page doesn't exist!";
});

//  |           Request          |         Response       |
//  |----------------------------|------------------------|
//  | GET  /blog/post/           | Requested post:        |
//  | GET  /blog/post/1          | Requested post: 1      |
//  | POST /blog/post/3416321341 | The page doesn't exist |
//  | GET  /blog/post/a          | The page doesn't exist |
//  | POST /post/                | The page doesn't exist |
//  | GET  /post/1               | The page doesn't exist |

Chainable routes

Sometimes it's a good idea to have multiple routes work in a single request. For example, you could check that the page exists in a route, then check if the user is logged in another, then load the page in another route...

The function \Alexya\Router\Router::route can accept as parameter a boolean that tells if the router will support chainable routes or not.

When the router supports chainable routes it will loop through ALL routes and execute all the routes that are matched, to stop the router you can let the route callback return false or finish the script execution with die or exit.

Example:

<?php

$router = new \Alexya\Router\Router();
$router->add("(.*)", function() {
    echo "Page requested";
});
$router->add("/User/([a-zA-Z]*)", function($user) {
    echo "User {$user}";

    return false; // Exit chain
});
$router->add("/User/([a-zA-Z]*)/(.*)", function($user, $action) {
    echo "User {$user}, action {$action}";
});

$router->route(true);

// Request URI: /User/test/asdf
// Output:
//  Page requested
//  User test

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: GNU
  • 更新时间: 2016-08-24

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固