2017 © Pedro Peláez
 

library php-autoroute

Automatically maps a provided route to a controller and method

image

lesichkovm/php-autoroute

Automatically maps a provided route to a controller and method

  • Sunday, April 1, 2018
  • by lesichkovm
  • Repository
  • 1 Watchers
  • 0 Stars
  • 4 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

PHP Autoroute

The "set-and-forget" smart router script., (*1)

Installation

A. Use composer (preferred)

composer require lesichkovm/php-autoroute

B. Manually via composer

Add the following to your composer file:, (*2)

   "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., (*3)

Requirements

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

See http://php.net/manual/en/language.oop5.autoload.php for how to register and autoload classes., (*5)

Example routes

Route: /admin/user-management/view-users, (*6)

Executes: Admin\UserManagementController@viewUsers, (*7)

Route: user/admin/test/home, (*8)

Executes: User\Admin\TestController@home, (*9)

Example routes using request method

Route: /admin/user-management/view-users, (*10)

Executes: Admin\UserManagementController@getViewUsers, (*11)

Route: user/admin/test/home, (*12)

Executes: User\Admin\TestController@getHome, (*13)

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');
}

The Versions

01/04 2018

dev-master

9999999-dev http://github.com/lesichkovm/php-autoroute

Automatically maps a provided route to a controller and method

  Sources   Download

proprietary

php route auto lesichkov

01/04 2018

v2.0.0

2.0.0.0 http://github.com/lesichkovm/php-autoroute

Automatically maps a provided route to a controller and method

  Sources   Download

proprietary

php route auto lesichkov

01/04 2018

1.0.0

1.0.0.0 http://github.com/lesichkovm/php-autoroute

Automatic route for PHP

  Sources   Download

proprietary

php route auto lesichkov