2017 © Pedro Peláez
 

library php-router

The Laravel router, for use outside of the Laravel framework

image

seytar/php-router

The Laravel router, for use outside of the Laravel framework

  • Friday, April 7, 2017
  • by seytar
  • Repository
  • 1 Watchers
  • 6 Stars
  • 188 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 13 Forks
  • 0 Open issues
  • 2 Versions
  • 5 % Grown

The README.md

PHP Router

The Laravel router, for use outside of the Laravel framework., (*1)

Installation

Add the package to your composer.json and run composer update., (*2)

{
    "require": {
        "seytar/php-router": "*"
    }
}

Usage

To start using the router you will need to bootstrap it like this:, (*3)

require 'vendor/autoload.php';

use Seytar\Routing\Router;

Router::bootstrap(function($ex) {
        header('Content-Type: text/html; charset=utf-8');
        echo '404 - Page Not Found';
    });

Once this has been done, you can define any route like you would in Laravel:, (*4)

Route::get('/', function()
{
    echo 'Hello world.';
});

The bootstrap process will check if there is a routes.php file in your application, and will automatically load it for you. It will also register a shutdown function that dispatches the current request. If you want to dispatch the current request manually, you can call Router::dispatch()., (*5)

The Request, Response, Input and URL facades are also available., (*6)

Basic GET Route

Route::get('/', function()
{
    return 'Hello World';
});

Basic POST Route

Route::post('foo/bar', function()
{
    return 'Hello World';
});

Registering A Route For Multiple Verbs

Route::match(array('GET', 'POST'), '/', function()
{
    return 'Hello World';
});

Registering A Route Responding To Any HTTP Verb

Route::any('foo', function()
{
    return 'Hello World';
});

Forcing A Route To Be Served Over HTTPS

Route::get('foo', array('https', function()
{
    return 'Must be over HTTPS';
}));

Often, you will need to generate URLs to your routes, you may do so using the URL::to method:, (*7)

$url = URL::to('foo');

, (*8)

Route Parameters

Route::get('user/{id}', function($id)
{
    return 'User '.$id;
});

Optional Route Parameters

Route::get('user/{name?}', function($name = null)
{
    return $name;
});

Optional Route Parameters With Defaults

Route::get('user/{name?}', function($name = 'John')
{
    return $name;
});

Regular Expression Route Constraints

Route::get('user/{name}', function($name)
{
    //
})
->where('name', '[A-Za-z]+');

Route::get('user/{id}', function($id)
{
    //
})
->where('id', '[0-9]+');

Passing An Array Of Wheres

Of course, you may pass an array of constraints when necessary:, (*9)

Route::get('user/{id}/{name}', function($id, $name)
{
    //
})
->where(array('id' => '[0-9]+', 'name' => '[a-z]+'))

Defining Global Patterns

If you would like a route parameter to always be constrained by a given regular expression, you may use the pattern method:, (*10)

Route::pattern('id', '[0-9]+');

Route::get('user/{id}', function($id)
{
    // Only called if {id} is numeric.
});

Accessing A Route Parameter Value

If you need to access a route parameter value outside of a route, you may use the Route::input method:, (*11)

Route::filter('foo', function()
{
    if (Route::input('id') == 1)
    {
        //
    }
});

, (*12)

Route Filters

Route filters provide a convenient way of limiting access to a given route, which is useful for creating areas of your site which require authentication. There are several filters included in the Laravel framework, including an auth filter, an auth.basic filter, a guest filter, and a csrf filter. These are located in the app/filters.php file., (*13)

Defining A Route Filter

Route::filter('old', function()
{
    if (Input::get('age') < 200)
    {
        return Redirect::to('home');
    }
});

If the filter returns a response, that response is considered the response to the request and the route will not execute. Any after filters on the route are also cancelled., (*14)

Attaching A Filter To A Route

Route::get('user', array('before' => 'old', function()
{
    return 'You are over 200 years old!';
}));

Attaching A Filter To A Controller Action

Route::get('user', array('before' => 'old', 'uses' => 'UserController@showProfile'));

Attaching Multiple Filters To A Route

Route::get('user', array('before' => 'auth|old', function()
{
    return 'You are authenticated and over 200 years old!';
}));

Attaching Multiple Filters Via Array

Route::get('user', array('before' => array('auth', 'old'), function()
{
    return 'You are authenticated and over 200 years old!';
}));

Specifying Filter Parameters

Route::filter('age', function($route, $request, $value)
{
    //
});

Route::get('user', array('before' => 'age:200', function()
{
    return 'Hello World';
}));

After filters receive a $response as the third argument passed to the filter:, (*15)

Route::filter('log', function($route, $request, $response)
{
    //
});

Pattern Based Filters

You may also specify that a filter applies to an entire set of routes based on their URI., (*16)

Route::filter('admin', function()
{
    //
});

Route::when('admin/*', 'admin');

In the example above, the admin filter would be applied to all routes beginning with admin/. The asterisk is used as a wildcard, and will match any combination of characters., (*17)

You may also constrain pattern filters by HTTP verbs:, (*18)

Route::when('admin/*', 'admin', array('post'));

Filter Classes

For advanced filtering, you may wish to use a class instead of a Closure. Since filter classes are resolved out of the application IoC Container, you will be able to utilize dependency injection in these filters for greater testability., (*19)

Registering A Class Based Filter

Route::filter('foo', 'FooFilter');

By default, the filter method on the FooFilter class will be called:, (*20)

class FooFilter {

    public function filter()
    {
        // Filter logic...
    }

}

If you do not wish to use the filter method, just specify another method:, (*21)

Route::filter('foo', 'FooFilter@foo');

, (*22)

Named Routes

Named routes make referring to routes when generating redirects or URLs more convenient. You may specify a name for a route like so:, (*23)

Route::get('user/profile', array('as' => 'profile', function()
{
    //
}));

You may also specify route names for controller actions:, (*24)

Route::get('user/profile', array('as' => 'profile', 'uses' => 'UserController@showProfile'));

Now, you may use the route's name when generating URLs or redirects:, (*25)

$url = URL::route('profile');

$redirect = Redirect::route('profile');

You may access the name of a route that is running via the currentRouteName method:, (*26)

$name = Route::currentRouteName();

, (*27)

Route Groups

Sometimes you may need to apply filters to a group of routes. Instead of specifying the filter on each route, you may use a route group:, (*28)

Route::group(array('before' => 'auth'), function()
{
    Route::get('/', function()
    {
        // Has Auth Filter
    });

    Route::get('user/profile', function()
    {
        // Has Auth Filter
    });
});

You may also use the namespace parameter within your group array to specify all controllers within that group as being in a given namespace:, (*29)

Route::group(array('namespace' => 'Admin'), function()
{
    //
});

, (*30)

Sub-Domain Routing

Laravel routes are also able to handle wildcard sub-domains, and pass you wildcard parameters from the domain:, (*31)

Registering Sub-Domain Routes

Route::group(array('domain' => '{account}.myapp.com'), function()
{

    Route::get('user/{id}', function($account, $id)
    {
        //
    });

});

, (*32)

Route Prefixing

A group of routes may be prefixed by using the prefix option in the attributes array of a group:, (*33)

Route::group(array('prefix' => 'admin'), function()
{

    Route::get('user', function()
    {
        //
    });

});

The Versions

07/04 2017

dev-master

9999999-dev https://github.com/seytar/php-router

The Laravel router, for use outside of the Laravel framework

  Sources   Download

MIT

The Requires

 

laravel router

07/04 2017

dev-1.0.x-stable

dev-1.0.x-stable https://github.com/seytar/php-router

The Laravel router, for use outside of the Laravel framework

  Sources   Download

MIT

The Requires

 

laravel router