jinnguyen/puja-route

Puja-Route 是一个路由组件,允许解析请求到模块/控制器/动作

v1.2.1 2017-11-25 04:40 UTC

This package is not auto-updated.

Last update: 2024-09-20 20:05:51 UTC


README

Puja-Route 是一个路由组件,允许解析请求到模块/控制器/动作

安装

composer require jinnguyen/puja-route

用法

include /path/to/vendor/autoload.php;
$config = array(
    'root_namespace' => '\\Puja\\Route\\Demo\\', // You can change to match with controller class name on your app
    'default_controller' => 'Index', // Default controller
    'default_action' => 'index', // Default action
    'controller_dir' => __DIR__ . '/Controller/', // Folder that includes controllers, controller must have prefix is Controller and file type is .php (Ex:  UserController.php/IndexController.php, ...)
    'module_dir' => __DIR__ . '/Module/', // Folder that includes app modules
    'cache_dir' => __DIR__ . '/cache/', // Cache folder will store cached routers
);

$router = new Puja\Route\Route($config); // when you build, it will scan all file *Controller.php in $config[controller_dir] and /Controller/*Controller.php in $config[module_dir]

添加更多路由

如果您的控制器不在 $config[controller_dir] 或 $config[module_dir]//Controller/ 中,您也可以使用此命令将其添加到路由

$route->addRoute('/testaddroute', '\\Test\\Add\\Route\\Namespace');

构建

扫描所有控制器文件,并从 $config[controller_dir]、$config[module_dir] 以及通过 $router->addRoute() 添加的控制器构建路由

$router->build();

获取路由

获取路由信息:模块、控制器、动作等

$router->getRoute('testaddroute') // get route at  of testaddroute

注意

  • 必须先运行 $router->build() 后再运行 $router->getRoute(),否则会抛出异常
  • 应在所有 $router->addRoute() 调用之后调用 $router->build(),否则将丢失所有添加的路由。