puja-route
Puja-Route is a router component, that allow to parse request to module/controller/action, (*1)
Install, (*2)
composer require jinnguyen/puja-route
Usage, (*3)
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]
Add more route, (*4)
If your controller is not in $config[controller_dir] and $config[module_dir]//Controller/, you can also add it to route by this command:, (*5)
$route->addRoute('/testaddroute', '\\Test\\Add\\Route\\Namespace');
Build, (*6)
Scan all controller files and build routes from $config[controller_dir] and $config[module_dir] and controllers that was added by $router->addRoute(), (*7)
$router->build();
Get route, (*8)
Get router information: module, controller, action, .., (*9)
$router->getRoute('testaddroute') // get route at of testaddroute
Note:
- $router->getRoute() must run after $router->build() otherwise it will get exception
- Should call $router->build() after all $router->addRoute(), otherwise all added routes will be missed., (*10)