Coder Social home page Coder Social logo

urouter's Introduction

URouter

A simple trie-based PHP router

Installation

Via composer:

composer require em4nl/urouter

Usage

Assuming you're using autoloading and your composer vendor dir is at ./vendor:

<?php

require_once __DIR__ . '/vendor/autoload.php';

$router = new Em4nl\U\Router();

// set a base path if this app doesn't live at the root path
// (right behind the domain)
$router->base('/my-app');

$router->get('/', function() {
    echo 'the index route';
});

$router->get('/test', function() {
    echo "the /test route";
});

$router->get('/:thing', function($thing) {
    echo "I like $thing!";
});

$router->get('/test/*', function($wildcard_match) {
    // will match paths of arbitrary length behind /test/ ...
});

$router->post('/form', function($context) {
    // ...
});

$router->catchall(function() {
    header('HTTP/1.1 404 Not Found');
    // ...
});

$router->run();

Routes don't have to be defined in any particular order, they will be matched by specificity automatically. E.g. if you visit /test/, the /test route will match and not the /:thing route, even if the latter would be defined earlier in the source code.

Development

Install dependencies

composer install

Run tests

./vendor/bin/phpunit tests

License

The MIT License

urouter's People

Contributors

smotchkkiss avatar

Stargazers

 avatar

Watchers

 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.