amirhossein-fzl / router

simple php router

Home Page:http://tarhche.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

dependencies none PHP 5.6+ WebSite tarhche.com GPLv3 license

router

A simple php Router

how to use

We have 3 important files here

  1. htaccess: Redirects all requests to home.php
  2. home.php: You can manage all requests in this file
  3. ROUTE.php: This is ROUTE lib you need to manage requests in home.php

Add ROUTE.php in home.php and then use it to manage requests:

<?php 
    //add router class
    include_once 'ROUTE.php';
?>

we have 4 functions in order to manage requests:

  • get: manages GET (method) requests.
  • post: manages POST (method) requests for example forms and etc.
  • go: manages both GET and POST requests
  • addHookFunction:this method runs per each request.

here we have some examples:

<?php
    //example 1 : using html or php
    ROUTE::get('/login',function(){
        //your page
        echo '<h1>my login page</h1>';
    });

   //example 2 : include files
    ROUTE::go('get','/register',function(){
        include 'pages/register.php';
    });

    //example 3 : for both get and post methods
    ROUTE::go('get|post','/logout',function(){
        echo '<h1>logout page</h1>';
    });

    //example 4 : using regex
    ROUTE::go('get','/show/{id:^\d*$}',function($id){
        echo '<h1>your numeric id is : '.$id.'</h1>';
    });
    ROUTE::post('/show/{id:^\d*$}',function($id){
        echo '<h1>your numeric id is : '.$id.'</h1>';
    });

    //example 5 : change to asp page
    ROUTE::get('/login.aspx',function(){
        //your page
        echo '<h1>my login page</h1>';
    });
?>

URLs can be matched by REGEX.

addHookFunction can be used to trace or takeing some actions before routing.
here is an example of addHookFuncion :

<?php
//using add addHookFunction
    ROUTE::addHookFunction(function($data){
        print_r($data);//show data
    });
?>

you can use $data variable in addHookFunction to access requests.
note: addHookFuncion must be written before any routes an home.php to work correctly.

Iranian people can visit here for persian tutorials about this router.

License

This project is licensed under the GPL-3.0 License - see the LICENSE.md file for details

contribute

in order to developement or debug, you can create pull requests.

About

simple php router

http://tarhche.com

License:GNU General Public License v3.0


Languages

Language:PHP 100.0%