Giter VIP home page Giter VIP logo

php-autoroute's Introduction

PHP Autoroute

The "set-and-forget" smart router script.

Installation

A. Use composer (preferred)

composer require lesichkovm/php-autoroute

B. Manually via composer

Add the following to your composer file:

   "repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/lesichkovm/php-autoroute.git"
        }
    ],
    "require": {
        "lesichkovm/php-autoroute": "dev-master"
    },

How does it work?

Just from the route (URI) it will find and execute the appropriate controller and method. Pass the current route and autoroute will call the associated controller and method.

Requirements

Your controller classes must be either included (i.e. using require_once), or you must have an autoload function registered (recommended).

See http://php.net/manual/en/language.oop5.autoload.php for how to register and autoload classes.

Example routes

Route: /admin/user-management/view-users

Executes: Admin\UserManagementController@viewUsers

Route: user/admin/test/home

Executes: User\Admin\TestController@home

Example routes using request method

Route: /admin/user-management/view-users

Executes: Admin\UserManagementController@getViewUsers

Route: user/admin/test/home

Executes: User\Admin\TestController@getHome

How to use

Simple One Line Example

autoroute(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));

Advanced Options with Exception Catching

// 1. Get the current route
$route = isset($_REQUEST['route']) ? $_REQUEST['route'] : '';

// 2. Autoroute
try {
    autoroute($route, [
        'default_method' => 'index',
        'default_controller' => 'Guest',
        'default_namespace' => 'App\\Controllers',
        'use_request_method' => true,
    ]);
} catch (ReflectionException $re) {
    // Page not found
    die('Not found:' . $re->controller . '@' . $re->method);
} catch (Exception $e) {
    // Other non routing related exception
    // Deal with exception (i.e. send yourself a mail)
    die('Exception occurred');
}

php-autoroute's People

Contributors

lesichkovm avatar

Stargazers

 avatar

Watchers

 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.