Skip to content

Latest commit

 

History

History
83 lines (62 loc) · 1.49 KB

README.md

File metadata and controls

83 lines (62 loc) · 1.49 KB

Simple PHP Router Class v1.0

This is a simple PHP Router Class. It requires no dependencies. Fork it and have fun with it.

It's a standalone single file PHP class to use on your projects. It requires no dependencies or no framework.

How to use

You only need two files:

  • 1 Router.php
  • 2 .htaccess

The .htaccess redirects all requests to index.php.

You can run everything from the index.php file, see the file for usage.

Step 1 - Initialize

Create a router instance

require "Router.php"

$router = new Router();

Step 2 - Add route URIs

Add routes with add method:

$router->add('GET', '/', function(){
    // Code goes here
});

You can directly call specific methods:

Only supports get, post, put, patch, delete right now

$router->get('/', function(){
    // Code goes here
});

$router->post('/create', function(){
    // Code goes here
});

Step 3 - Finish

Finish routing with listen() method

$router->listen();

Set URI params

You can set URI params of any type just by using ":" before URI name

$router->get('/user/:id', function($params){
    // Code goes here
    echo $params['id'];
});

Example Code

require 'Router.php';

$router->get('/', function(){
    // Code goes here
});

$router->post('/create', function(){
    // Code goes here
});

$router->get('/user/:id', function($params){
    // Code goes here
    echo $params['id'];
});

$router->listen();

LICENSE

MIT License