Skip to content
Simple php router
PHP
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
.gitignore
LICENSE
README.md
composer.json

README.md

SimpleRouter

Latest Version on Packagist Software License Total Downloads

Simple router for php7.1+ projects like API.

Install

Using composer:

$ composer require zhukmax/simple-router

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 SimpleRouter without any Template engine.

<?php

require_once 'vendor/autoload.php';

use ProjectName\API\Controllers\IndexController;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
use Zhukmax\Router\Router;

/** Add Twig Template engine **/
$loader = new FilesystemLoader(__DIR__ . '/src/views');
$twig = new Environment($loader);

$router = new Router([
    'tplEngine' => $twig
]);
$router
    ->get('/api/users', IndexController::class, 'actionGetAll', 'json')
    ->output();

You can use Request static methods if you need $_GET/$_POST data in your action-method. The methods have basic data-filters for intiger, email.

<?php

namespace ProjectName\API\Controllers;

use Zhukmax\Router\Request;

class IndexController
{
    public static function actionGetAll()
    {
        $date = Request::get('date');
        $page = Request::getInt('page', 0);
        
        return [
            'date' => $date,
            'page'=> $page
        ];
    }
}

License

The Apache License Version 2.0. You can find text of License in the License File.

You can’t perform that action at this time.