GithubHelp home page GithubHelp logo

pkdevboxy / tez Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 74 KB

Framework agnostic, lightweight regex-based router implementation in PHP with support for reverse URL generation.

Home Page: http://vaibhavpandeyvpz.github.io/tez

License: MIT License

PHP 100.00%

tez's Introduction

vaibhavpandeyvpz/tez

Framework agnostic, lightweight regex-based router implementation in PHP with support for reverse URL generation.

Build Status

Install

composer require vaibhavpandeyvpz/tez

Routing

$router = new Vaibhav\Tez\Router();

$router->get('/', function ()
{
    return 'Home';
}, 'home');

$router->get('/hello/{name}', function ($name)
{
    return sprintf('Hello %s!', $name);
}, 'hello');

$router->get('/hi/{name}/{num:[0-9]+}', function ($name, $no)
{
    return sprintf('Hi %s. You are no. %d!', $name, $no);
}, 'hi');

$router->group('/group', function ()
{
    /**
     * Group
     *
     * @var $this Vaibhav\Tez\Router
     */
    $this->get('/', function () {}, 'g-home');

    $this->get('/one/{name}', function ($name) { ... }, 'g-one');

    $this->get('/two/{name}', function ($name) { ... }, 'g-two');

    $this->group('/sub', function ()
    {
        /**
         * Nested group
         *
         * @var $this Vaibhav\Tez\Router
         */
        $this->get('/', function () { ... }, 'sg-home');

        $this->get('/one/{name}', function ($name) { ... }, 'sg-one');

        $this->get('/two/{name}', function ($name) { ... }, 'sg-two');
    });
});

Generation

$router = new Vaibhav\Tez\Router();

// Setup routes...

$path = $router->generate('hi', [
    'name' => 'me',
    'no' => 1
]);

Dispatching

$router = new Vaibhav\Tez\Router();

// Setup routes...

$path = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '/';

$route = $router->match($path);

if ($route !== null) {
    if ($route->allows($_SERVER['REQUEST_METHOD'])) {
        $dispatcher = new Vaibhav\Tez\Dispatcher();
        echo $dispatcher->dispatch($route);
    } else {
        header('HTTP/1.0 405 Method Not Allowed');
    }
} else {
    header('HTTP/1.0 404 Not Found');
}

Dispatching (Controller > Action)

$router = new Vaibhav\Tez\Router();

$router->get('/', 'HomeCtrl#index');

// Setup more routes ...

$path = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '/';

$route = $router->match($path);

if ($route !== null) {
    if ($route->allows($_SERVER['REQUEST_METHOD'])) {
        $dispatcher = new Vaibhav\Tez\Dispatcher(function ($handler)
        {
            if (is_string($handler) && (strpos($handler, '#') > 0)) {
                list ($controller, $action) = explode('#', $handler);
                return [new $controller(), $action];
            }
        });
        echo $dispatcher->dispatch($route);
    } else {
        header('HTTP/1.0 405 Method Not Allowed');
    }
} else {
    header('HTTP/1.0 404 Not Found');
}

License

See LICENSE.md file.

tez's People

Contributors

vaibhavpandeyvpz avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.