Code Monkey home page Code Monkey logo

php7-router's Introduction

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

php7-router's People

Contributors

bssth avatar

Stargazers

Eyüp Klasvento avatar Gladyshev Sergey avatar

Watchers

James Cloos 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.