定制 slabphp/router 二次开发

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

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

slabphp/router

Composer 安装命令:

composer require slabphp/router

包简介

SlabPHP Router

README 文档

README

This router library will take specially crafted XML files, build a routing table, and determine if a route can be matched to the current REQUEST_URI parameters.

This router probably pales in comparison to many other modern open source routers. We're well aware XML is in many cases frowned upon. This library was written many years ago. Please see the main SlabPHP documentation for more information about this and all SlabPHP libraries.

Router Setup and Usage

First import the library

composer require slabphp/router

Then configure and instantiate your router.

$configuration = new \Slab\Router\Configuration();

$configuration
    ->setConfigurationPaths('/framework/configs','/site/configs'])
    ->addRouteFile('default.xml');

$router = new \Slab\Router($configuration);

Next write your route files and resolve some routes!

$router->determineSelectedRoute();
$route = $router->getSelectedRoute();

Based on the values of $_SERVER['REQUEST_URI'] and the routes in /framework/configs/default.xml and /site/configs/default.xml, you will either get false (a 404 condition) or a \Slab\Router\Route object.

Route Creation

Static Routes

Depending on how you configure the router, a route file may be anywhere. But here is an example of a route file:

<?xml version="1.0" encoding="UTF-8" ?>
<routes>
    <route>
        <path>/</path>
        <name>Homepage</name>
        <class>\Namespace\Path\To\Your\Controller</class>
        <parameters>
            <someRouteParameter><![CDATA[This could be anything really. Maybe a page title?]]></testValue>
        </parameters>
    </route>
</routes>

Dynamic Routes (Pattern Validation)

You can also have dynamic routes that use pattern validators. For example:

<route>
    <path>/something</path>
    <name>A Dynamic URL Path</name>
    <class>\Some\Controller</class>
    <pattern>/value/{string:someVar}/thing/{numeric:intVar}</pattern>
    <parameters>
        <testValue>1</testValue>
        <testString>string</testString>
    </parameters>
</route>

This would match a URL that comes in looking like /something/value/my-first-string/thing/32 Notice the {string:someVar} and {numeric:intVar}. These are mechanisms that tell the SlabPHP router to use a specific validator class and the variable name to store it in. You can create your own custom validators and simply specify their entire classname. For example, a blog post may have something like this:

<route>
    <name>Blog URL</name>
    <class>\Some\Controller</class>
    <path>/</path>
    <pattern>/{numeric:year}}/{numeric:month}/{\My\Blog\Router\Validators\PostSlug:postSlug}</pattern>
    <parameters>
        <testValue>1</testValue>
        <testString>string</testString>
    </parameters>
</route>

As long as \My\Blog\Router\Validators\PostSlug implements the correct interface, and returns true/false, you can fail this route if the post slug is wrong.

There are some built-in validators but many are application specific.

  • string - will match a string with a set of characters; a-z, 0-9, underscore, plus +, and a space.
  • date - will match any date that will work in the constructor to new \DateTime()
  • numeric - will match a number
  • value - matches an exact value, this is used internally for specific url segment values in pattern fields
  • any - matches anything

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2018-03-02

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固