此软件包的最新版本(v0.0.1)没有可用的许可证信息。

Route - 为PHP提供快速、灵活的路由功能,让您能够快速轻松地构建RESTful Web应用程序。

v0.0.1 2019-06-28 02:58 UTC

This package is auto-updated.

Last update: 2024-09-28 14:52:35 UTC


README

Route - 为PHP提供快速、灵活的路由功能,让您能够快速轻松地构建RESTful Web应用程序。

安装

您可以直接下载并使用它,无需任何更改。

或者使用Composer。

建议您使用Composer来安装Route。

$ composer require nezamy/route

Route需要PHP 5.4.0或更高版本。

用法

只有当使用composer时,请在根目录中创建index.php。

创建一个包含以下内容的index.php文件

<?php
define('DS', DIRECTORY_SEPARATOR);
define('BASE_PATH', __DIR__ . DS);
//Show errors
//===================================
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
//===================================

require BASE_PATH.'vendor/autoload.php';

$app            = System\App::instance();
$app->request   = System\Request::instance();
$app->route     = System\Route::instance($app->request);

$route          = $app->route;

$route->any('/', function() {
    echo 'Hello World';
});

$route->end();

如果使用Apache,请确保在index.php旁边存在.htaccess文件

它的工作原理

通过将URL模式与回调函数进行匹配来完成路由。

index.php

$route->any('/', function() {
    echo 'Hello World';
});

$route->any('/about', function() {
    echo 'About';
});

回调可以是任何可调用的对象。因此,您可以使用常规函数

function pages() {
    echo 'Page Content';
}
$route->get('/', 'pages');

或者类方法

class home
{
    function pages() {
        echo 'Home page Content';
    }
}
$route->get('/', ['home', 'pages']);
// OR
$home = new home;
$route->get('/', [$home, 'pages']);
// OR
$route->get('/', 'home@pages');

方法路由

$route->any('/', function() {
    // Any method requests
});

$route->get('/', function() {
    // Only GET requests
});

$route->post('/', function() {
    // Only POST requests
});

$route->put('/', function() {
    // Only PUT requests
});

$route->patch('/', function() {
    // Only PATCH requests
});

$route->delete('/', function() {
    // Only DELETE requests
});

// You can use multiple methods. Just add _ between method names
$route->get_post('/', function() {
    // Only GET and POST requests
});

多路由(全部在一个中)

$route->get(['/', 'index', 'home'], function() {
    // Will match 3 page in one
});

参数

// This example will match any page name
$route->get('/?', function($page) {
    echo "you are in $page";
});

// This example will match anything after post/ - limited to 1 argument
$route->get('/post/?', function($id) {
    // Will match anything like post/hello or post/5 ...
    // But not match /post/5/title
    echo "post id $id";
});

// more than parameters
$route->get('/post/?/?', function($id, $title) {
    echo "post id $id and title $title";
});

对于“无限”可选参数,您可以这样做

// This example will match anything after blog/ - unlimited arguments
$route->get('/blog/*', function() {
    // [$this] instanceof ArrayObject so you can get all args by getArrayCopy()
    pre($this->getArrayCopy());
    pre($this[1]);
    pre($this[2]);
});

命名参数

您可以在路由中指定命名参数,这些参数将传递到回调函数中。

$route->get('/{username}/{page}', function($username, $page) {
    echo "Username $username and Page $page <br>";
    // OR
    echo "Username {$this['username']} and Page {$this['page']}";
});

正则表达式

您可以通过正则表达式验证参数。

// Validate args by regular expressions uses :(your pattern here)
$route->get('/{username}:([0-9a-z_.-]+)/post/{id}:([0-9]+)',
function($username, $id)
{
    echo "author $username post id $id";
});

// You can add named regex pattern in routes
$route->addPattern([
    'username' => '/([0-9a-z_.-]+)',
    'id' => '/([0-9]+)'
]);

// Now you can use named regex
$route->get('/{username}:username/post/{id}:id', function($username, $id) {
    echo "author $username post id $id";
});

一些已注册在路由中的命名正则表达式模式

[
    'int'               => '/([0-9]+)',
    'multiInt'          => '/([0-9,]+)',
    'title'             => '/([a-z_-]+)',
    'key'               => '/([a-z0-9_]+)',
    'multiKey'          => '/([a-z0-9_,]+)',
    'isoCode2'          => '/([a-z]{2})',
    'isoCode3'          => '/([a-z]{3})',
    'multiIsoCode2'     => '/([a-z,]{2,})',
    'multiIsoCode3'     => '/([a-z,]{3,})'
]

可选参数

您可以通过添加(?)来指定可选的命名参数,以供匹配使用

$route->get('/post/{title}?:title/{date}?',
function($title, $date) {
    if ($title) {
        echo "<h1>$title</h1>";
    }else{
        echo "<h1>Posts List</h1>";
    }

    if ($date) {
        echo "<small>Published $date</small>";
    }
});

$route->group('/admin', function()
{
    // /admin/
    $this->get('/', function() {
        echo 'welcome to admin panel';
    });

    // /admin/settings
    $this->get('/settings', function() {
        echo 'list of settings';
    });

    // nested group
    $this->group('/users', function()
    {
        // /admin/users
        $this->get('/', function() {
            echo 'list of users';
        });

        // /admin/users/add
        $this->get('/add', function() {
            echo 'add new user';
        });
    });

    // Anything else
    $this->any('/*', function() {
        pre("Page ( {$this->app->request->path} ) Not Found", 6);
    });
});

带参数的组

$route->group('/{lang}?:isoCode2', function($lang)
{
    $default = $lang;

    if (!in_array($lang, ['ar', 'en'])) {
        $default = 'en';
    }

    $this->get('/', function($lang) use($default) {
        echo "lang in request is $lang<br>";
        echo "include page_{$default}.php";
    });

    $this->get('/page/{name}/', function($lang, $name)
    {
        pre(func_get_args());
        pre($this->app->request->args);
    });
});

中间件

$route->use(function (){
    $req = app('request');
    pre('Do something before all routes', 3);
});

$route->before('/', function (){
    pre('Do something before all routes', 4);
});

$route->before('/*!admin', function (){
    pre('Do something before all routes except admin', 4);
});

$route->before('/admin|home', function (){
    pre('Do something before admin and home only ', 4);
});

$route->after('/admin|home', function (){
    pre('Do something after admin and home only ', 4);
});

完整示例在这里

支持我

https://www.paypal.me/nezamy