zhukmax / simple-router
This package is abandoned and no longer maintained.
The author suggests using the zhukmax/waymark package instead.
Router for php7.1+ projects
3.0.0
2020-07-26 13:20 UTC
Requires
- php: >=7.1.0
- ext-json: *
Requires (Dev)
- phpunit/phpunit: ^9.2
README
Waymark is a router for php7.1+ projects like API.
Install
Using composer:
$ composer require zhukmax/waymark
Using
If you need Template engine in your project you can use your favorite like I use Twig in example, but if you need only json/csv responces then just use Waymark without any Template engine.
<?php require_once './vendor/autoload.php'; use ProjectName\API\Controllers\IndexController; use Twig\Environment; use Twig\Loader\FilesystemLoader; use Zhukmax\Waymark\Router; /** Add Twig Template engine **/ $loader = new FilesystemLoader(__DIR__ . '/src/views'); $twig = new Environment($loader); (new Router([ 'tplEngine' => $twig, 'namespace' => '\\ProjectName\\API\\Controllers', 'routes' => dirname(__FILE__).'/routes.json' ])) ->get('/api/users', IndexController::class, 'actionGetAll', 'json') ->output();
Json-file with routes example:
{ "get": { "/users": [ "NameOfControllerWithoutControllerSuffix", "NameOfAction", "html" ], "/users/{id:int}": [ "NameOfControllerWithoutControllerSuffix", "NameOfAction", "html" ] } }
You can use Request static methods if you need $_GET/$_POST/$_FILES data in your action-method. The methods have basic data-filters for intiger, email, boolean, array, files, images.
Parameter in route can be only string ({name:str}
) or integer ({id:int}
).
<?php namespace ProjectName\API\Controllers; use Zhukmax\Waymark\AbstractController; use Zhukmax\Waymark\Request; class IndexController extends AbstractController { public static function actionGetAll() { $date = Request::get('date'); $page = Request::getInt('page', 0); return [ 'date' => $date, 'page'=> $page ]; } public function tst(string $date, int $page) { return $this->tpl->render('index.twig', [ 'date' => $date, 'page' => $page ]); } }
License
The Apache License Version 2.0. You can find text of License in the License File.