MikeChip/router

此包已被废弃,不再维护。未建议替代包。

适用于任何项目的简单非MVC路由器

1.0.0 2019-09-08 17:21 UTC

This package is auto-updated.

Last update: 2024-07-21 14:10:25 UTC


README

用于小型项目(无MVC)的小型路由器。将 http://example.com/test 转换为 ./controllers/test.php

已弃用

该库制作时间已久,可能已过时。如果您想使用它,请进行分支。

安装

您可以使用composer进行安装

composer require mikechip/router

或者直接包含 /src 目录下的类

使用方法

<?php
    require_once('vendor/autoload.php');

    /*
     * Directory where your controllers are located
     */
    $controller_dir = __DIR__ . '/test_controllers';

    /*
     * Optional. URI that client requested.
     * $_SERVER['REQUEST_URI'] is used by default.
     * Use only if it is highly needed (for example, in ReactPHP)
     */
    $request_uri = $_SERVER['REQUEST_URI'];

    /*
     * Create new Router object and get result
     */
    $router = new Mike4ip\Router\Router( $controller_dir, $request_uri );
    $result = $router->getResult();

    try {
        /*
         * Run controller.
         * For example: if you requested /test,
         * it runs $controller_dir/test.php
         */
        require(
            $result->getController()
        );
    } catch(Mike4ip\Router\NotFoundException $e) {
        /*
         * If controller you need is not found
         */
        http_response_code(404);
        print('Page not found');
    }

或使用简单快捷的方式

<?php
    require_once('vendor/autoload.php');
    $controller_dir = __DIR__ . '/test_controllers';
    \Mike4ip\Router\Router::autorun();

反馈

欢迎在问题https://github.com/mikechip/php7-router/issues中提出