Code Monkey home page Code Monkey logo

simple-php-router's Introduction

Simple PHP Router

Latest Stable Version Total Downloads Build Status

Updating from version 0.x or 1.x will break your code! read the documentation before upgrading!

Getting started

Step 1 - .htaccess file create an .htaccess file in the root of your project and fill it with the code below:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

Step 2 - require szenis/routing
In your terminal execute: composer require szenis/routing

Step 3 - create index.php
Create the file index.php in the root of your project

Step 4 - require autoload.php and use the Router

The following snippet shows how the router can be used.

<?php

require './vendor/autoload.php';

use Szenis\Routing\Router;

$router = new Router();
$router->get('/{n:date}-{w:item}', function($date, $item) {
    return 'hello world';
});

$response = $router->resolve($_SERVER['REQUEST_URI'], $_SERVER['REQUEST_METHOD']);

switch ($response['code']) {
    case \Szenis\Routing\Route::STATUS_NOT_FOUND:
        // render your 404 page here...
        break;
    
    case \Szenis\Routing\Route::STATUS_FOUND:
        // the router only resolves the route, here is an example how to execute the route.
        if ($response['handler'] instanceof \Closure) {
            echo call_user_func_array($response['handler'], $response['arguments']);
        }
        
        break;
}

to your index.php

Optional
For debuging purpose add the following to your index.php

error_reporting(E_ALL);
ini_set('display_errors', 1);

Usage

/**
 * initialize the router class
 */
$router = new Router();

/**
 * Route matches the url '/' for the GET method
 */
$router->add('/', 'GET', function() {
    // the closure will be executed when route is triggerd and will return 'hello world'
    return 'hello world!'; 
});

/**
 * It is posible to add one or multiple wildcards in one route
 */
$router->add('/user/{id}', 'GET', function($id) {
    return $id;
});

/**
 * It is also posible to allow mulitple methods for one route (methods should be separated with a '|')
 */
$router->add('/user/{id}/edit', 'GET|POST', function($id) {
    return 'user id: '.$id;
});

/**
 * Or when u are using controllers in a namespace you could give the full path to a controller (controller::action)
 *
 * Since version 2.0 executing the handler is up to you.
 */
$router->add('/user/{id}/delete', 'DELETE', 'App\Controllers\UserController::delete');

/**
 * Since version 1.1 there are shortcut methods for get, post, put, patch, delete and any.
 * You can use them as follow
 */
$router->get('/example/get', function() {});        // Will match GET requests
$router->post('/example/post', function() {});      // Will match POST requests
$router->put('/example/put', function() {});        // Will match PUT requests
$router->patch('/example/patch', function() {});    // Will match PATCH requests
$router->delete('/example/delete', function() {});  // Will match DELETE requests
$router->any('/example/any', function() {});        // Will match GET, POST, PUT, PATCH, DELETE requests

/**
 * resolve the route and receive the response
 *
 * The response is an array with the following keys
 * - code (contains 200 if the route is found, else 404)
 * - handler (contains the handler, often a \Closure or full path to your controller action)
 * - arguments (contains the route arguments if any)
 */
$response = $router->resolve($_SERVER['REQUEST_URI'], $_SERVER['REQUEST_METHOD']);

/**
 * To execute your route you have to do the following
 */

switch ($response['code']) {
    case \Szenis\Routing\Route::STATUS_NOT_FOUND:
        // render your 404 page here...
        break;
    
    case \Szenis\Routing\Route::STATUS_FOUND:
        // the router only resolves the route, here is an example how to execute the route.
        if ($response['handler'] instanceof \Closure) {
            echo call_user_func_array($response['handler'], $response['arguments']);
        }
        
        // if your handler is a full path to your function you could execute it with this:
        $className = substr($response['handler'], 0, strpos($response['handler'], '::'));
        $functionName = substr($response['handler'], strpos($response['handler'], '::') + 2);

        echo call_user_func_array(array((new $className), $functionName), $response['arguments']);

        break;
}

Wildcard options

The following options exist
  • a: (alfabetic chars only)
  • n: (numbers only)
  • an: (alfanumeric chars only)
  • w: (alfanumeric, dash and underscore only)
  • ?: (optional parameters) - must be last part of the url
  • *: (lazy loading) - must be last part of the url

How to use

// The id must be a number
$router->add('/user/{n:id}', 'GET', 'App\Controllers\UserController::show');

// The id must contain either alfabetic chars or numbers
$router->add('/user/{an:id}', 'GET', 'App\Controllers\UserController::show');

// Now we want everything behind docs/ in the page variable
// For example when we go to the url /docs/user/edit we will receive user/edit in the page variable
$router->add('/docs/{*:page}', 'GET', function($page) {
    // do something with $page
});

// Optional parameter example
$router->add('/hello/{a:name}/{?:lastname}', 'GET', function($name, $lastname = null) {
    // check if lastname is provided
    // if ($lastname) {...}
})

Upgrading from v0.x/v1.x to v2.x

  • Namespace has changed from \Szenis to \Szenis\Routing
  • $router->setNamespace() has been removed!
  • RouteResolver shouldnt be used to resolve routes, use the following instead $router->resolve($uri, $method);
  • Router does not execute the route anymore. From now one it is your responsibility to execute the handler. At the bottom of the section 'Usage' there is an example how to execute the handler.

Changelog

v2.0.0

  • Removed 'default' namespace
  • Changed namespace from \Szenis to \Szenis\Routing
  • Router does not execute the callable itself, this gives you more control over parameter injection
  • RouteResolver is callable trough the router
  • Bugfix: it is now possible to have more then one parameter in one segment (/{parameter1}-{parameter2}/)

v1.1.0

  • Shortcut functions for get, post, put, patch, delete and any

v1.0.0

  • Updated readme
  • Posible to add default namespace

v0.9.0

  • 100% test coverage
  • minimum php version reduced from 5.4 to 5.3

v0.8.0

  • Added optional parameter
  • Added lazy url loading
  • Improved code

v0.7.0

  • Improved code

v0.6.0

  • Changed usages of router check out the Usages section for more detail
  • Posible to add closure to a route
  • Routes with query string will be found now (bugfix: v0.6.1)

v0.5.0

  • Removed unnecessary code

v0.4.0

  • Added interfaces and created an url factory

v0.3.0

  • Its now posible to add options to url wildcards for more information see wildcard options

v0.2.0

  • RouteResolver uses regex to match routes quicker

simple-php-router's People

Contributors

stein189 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

simple-php-router's Issues

RouteResolver argument counts not equals

$router->get('/admin_crm/{w:controller}/{a:action}/{?:id}', function ($controller, $action, $id = null) {

Warning: array_combine(): Both parameters should have an equal number of elements in /var/www/..../web/vendor/szenis/routing/src/RouteResolver.php on line 44

so array_combine return false. You do not control ?:{...} optional arguments ?
this worked in 1.x previous version.

try

$count = min(count($route->getArguments()), count($arguments));
                $arguments = array_combine(array_slice($route->getArguments(), 0, $count), array_slice($arguments, 0, $count));

or compare optional arguments before please.

RewriteBase

First, thank you for a great class.

Do you happen to know I get this to work from within a subdirectory? Currently, I am having to prepend my directory name to every route.

For example:

$router->add( '/subdir/login', 'GET', function () {
return 'login page here';
} );

Thanks again.

Support for Controllers in subfolders?

Does this support Controllers being paced in sub folders?
Like:

App/Controller/Common/HomeController.php
App/Controller/Common/HeaderController.php
App/Controller/Information/AboutUsController.php
App/Controller/Information/ContactUsController.php
App/Controller/Account/LogInController.php
App/Controller/Account/LogOutController.php

Page auto-change is not functioning properly

Hello.

How do I report that if I connect to the sub-folder in the root folder, I don't have to print the error ' Automatically change pages ' and print them with other errors?

$response = $router->resolve($_SERVER['REQUEST_URI'], $_SERVER['REQUEST_METHOD']);
switch ($response['code']) {
    case \Szenis\Routing\Route::STATUS_MOVED:
    $temp = new Controller\templates();
    $temp->templates('error/403');

But it didn't work.

Using papago, the English language is not smooth. I'm sorry.

.htaccess example RewriteRule has unnecessary leading slash

This tripped me up a bit. In your sample htaccess ruleset from README you suggest the following to remove trailing slashes:

# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]

However, the leading slash on the substitution portion is unnecessary and causes all replacements to be absolute paths. This gives undesired results if you are running the router from a base directory other than the URL root. This should be sufficient:

# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ $1 [L,R=301]

And it works with a non-root URI base path as well as using a RewriteBase directive.

how to get URL.

Hello. I'm Korean PHP Beginner.

I want get URL and variables

in index.php

require_once(__DIR__. '/vendor/autoload.php');
require_once(__DIR__. '/route/routing.php');

if ($id == 1) {
  require_once('main.php');
}

in routing.php

use Szenis\Routing\Router;

$router = new Router();

$router->add('/', 'GET', function() {
    $id = 1;
    return $id;
});


$response = $router->resolve($_SERVER['REQUEST_URI'], $_SERVER['REQUEST_METHOD']);

switch ($response['code']) {
    case \Szenis\Routing\Route::STATUS_NOT_FOUND:
        // render your 404 page here...
        break;

    case \Szenis\Routing\Route::STATUS_FOUND:
        // the router only resolves the route, here is an example how to execute the route.
        if ($response['handler'] instanceof \Closure) {
            echo call_user_func_array($response['handler'], $response['arguments']);
        }

        break;
}

thanks.
i can't english..

action_type vs actionType in URL

Hi,
look at this problem i have (v2.0.1):

$router->add('/admin_crm/{w:controller}/{a:action_type}/ajax/{a:action}', 'GET', function ($controller, $action_type, $action) { ... }
arguments in url, controller, action_type, action.

After i dump this route in RouteResolver i got this:

Szenis\Routing\Route Object
(
    [method:Szenis\Routing\Route:private] => Array
        (
            [0] => GET
        )

    [url:Szenis\Routing\Route:private] => admin_crm\/([0-9a-zA-Z-_]++)\/([a-zA-Z]++)\/ajax\/([a-zA-Z]++)
    [action:Szenis\Routing\Route:private] => Closure Object
        (
            [parameter] => Array
                (
                    [$controller] => 
                    [$action_type] => 
                    [$action] => 
                )

        )

    [arguments:Szenis\Routing\Route:private] => Array
        (
            [0] => controller
            [1] => action
        )

)

with no action_type argument in Arguments but if i change argument name from
action_type TO actionType, it works. There may be some problem with underscore variable.

Optional parameter not patch url

Hi,
in previous version, was able to write this route:
$router->add('/ajax/{a:action}{?:params}', 'GET', function ($action) ..
and Router match

/ajax/getModalContent/?ajax=true&action=getModalContent ...
/ajax/getModalContent?ajax=true&action=getModalContent ...
//ajax/getModalContent/?ajax=true&action=getModalContent ...

There was no problem in slashes, in v2 i have
Page not found

Is it bug or its not allowed in new version?
Do i have to change urls in router to '/ajax/{a:action}/{?:params}' so every arguments will be separated by / ?

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.