Code Monkey home page Code Monkey logo

php_router's Introduction

PhpRouter PhpRouter alt text alt text alt text

PHP router inspired on FatFree, CakePHP and own invention.

It allows to use anonymous functions as callback as well as OOP classes and methods. In addition, it detects if request is asynchronous or not (defined by [ajax] tag in rute definition).

Installation

Just clone that repository or use composer:

composer require plugowski/php_router

Next in your project create .htaccess file which point on your dispatcher file:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteRule .* index.php?url=$1 [QSA,L]
</IfModule>

Usage

Basic usage looks like code below:

<?php
require __DIR__ . '/vendor/autoload.php';

use PhpRouter\Route;
use PhpRouter\RouteCollection;
use PhpRouter\Router;
use PhpRouter\RouteRequest;

$routing = new RouteCollection();
$routing->attach(new Route('GET /', function(){
    echo 'Hello World';
}));

(new Router(new RouteRequest(), $routing))->run();

If you want to trigger OOP classes and methods, just write full namespace and method reference (-> or ::):

new Route('GET /', '\Full\Namespace\Class->method');

For named parameters use @name tag, for example:

new Route('GET /order/@id', '\Full\Namespace\Class->method');

Standard all named parameters will be recognized as (\w-)+, but if you want to define your own pattern, do it in second argument as associative array, where keys match to @ tag:

// $_SERVER['REQUEST_URI'] = '/order/14-XA-43321'
new Route('GET /order/@id', ['id' => '\d{2}\-\w{2}\-\d{5}'], function($params) {
  echo 'Order ID: ' . $params['id']; // Order ID: 14-XA-43321 
});

All named params are passed into called function as associative array in first argument.

Licence

New BSD Licence: https://opensource.org/licenses/BSD-3-Clause

php_router's People

Contributors

plugowski avatar

Stargazers

 avatar  avatar  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.