Coder Social home page Coder Social logo

router's Introduction

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.

router's People

Contributors

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