Skip to content
This repository has been archived by the owner on Jun 21, 2024. It is now read-only.
/ php7-router Public archive

Simple non-MVC router for any project

License

Notifications You must be signed in to change notification settings

bssth/php7-router

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Overview

Tiny router for tiny projects without MVC. Converts http://example.com/test to ./controllers/test.php

Deprecated

The library was made quite a while ago and may be out of date. Please make a fork if you want to use it.

Installation

You can install in using composer:

composer require mikechip/router

Or just include classes from /src

Usage

<?php
    require_once('vendor/autoload.php');

    /*
     * Directory where your controllers are located
     */
    $controller_dir = __DIR__ . '/test_controllers';

    /*
     * Optional. URI that client requested.
     * $_SERVER['REQUEST_URI'] is used by default.
     * Use only if it is highly needed (for example, in ReactPHP)
     */
    $request_uri = $_SERVER['REQUEST_URI'];

    /*
     * Create new Router object and get result
     */
    $router = new Mike4ip\Router\Router( $controller_dir, $request_uri );
    $result = $router->getResult();

    try {
        /*
         * Run controller.
         * For example: if you requested /test,
         * it runs $controller_dir/test.php
         */
        require(
            $result->getController()
        );
    } catch(Mike4ip\Router\NotFoundException $e) {
        /*
         * If controller you need is not found
         */
        http_response_code(404);
        print('Page not found');
    }

Or use simple shortcut:

<?php
    require_once('vendor/autoload.php');
    $controller_dir = __DIR__ . '/test_controllers';
    \Mike4ip\Router\Router::autorun();

Feedback

You are welcome at Issues: https://github.com/mikechip/php7-router/issues