Giter Club home page Giter Club logo

txiki-router's Introduction

Txiki Router

Simple router for PHP

Author Latest Version Packagist Version

Software License Build Status Scrutinizer Code Quality Code Coverage

Install

Via Composer

$ composer require txiki/router

Requirements

The following versions of PHP are supported by this version.

  • PHP 5.4
  • PHP 5.5
  • PHP 5.6
  • HHVM

Documentation

Simple example:

// load composer autoload
require '../vendor/autoload.php';

use Txiki\Router\Route;

$r = new Route();

// add GET route
$r->get('/home', function(){
    return "GET Hello world!";
});

//tell router what you want to process, the route and the http method
$route = $r->exec( '/home', 'get');

if($route!==false){
	// example process response
	echo $route->response;
}else{
	// example response 404
	echo '404';
}

Add more routes:

// add POST route
$r->post('/home', function(){
    return "POST Hello world!";
});

// add DELETE route
$r->delete('/home', function(){
    return "DELETE Hello world!";
});

// add PUT route
$r->put('/home', function(){
    return "PUT Hello world!";
});

Wildcard and custom routes:

// add route to any http method
$r->any('/home', function(){
    return "Hello world! respond to any http method";
});

// custom http methods for one route
$r->add('/home', function(){
    return "Hello world! respond to custom http methods";
}, 'get|post');

Manage custom url params:

$r->any('/test-{id}/{name}/{n}', function($id, $name , $n){
    return 'Test: ' . $id .' '.$name.' '.$n;
})->params([
		// add your own regular expression to param or
		// 'use Txiki\Router\RouteRegex'
		'id' => RouteRegex::INT,
		'name' => RouteRegex::ALPHA,
		'n' => RouteRegex::INT
	]
);

Use class/method as callback:

class myClass{
    public function method1( $id, $name){
        return 'Hello world ' .$id . ' '. $name;
    }
}

$r->get('/user/{id}/{name}', 'myClass::method1');

Helper methods:

// get all established routes
$table = $r->table();

// get all routes for '/home'
$map = $r->getRouteMap('/home');

// get only POST route for '/home'
$map = $r->getRouteMap('/home', 'post');

Testing

$ vendor/bin/phpunit

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.

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.