2017 © Pedro Peláez
 

library laravel-router

An organized approach to handling Laravel routes.

image

sebastiaanluca/laravel-router

An organized approach to handling Laravel routes.

  • Monday, July 30, 2018
  • by sebastiaanluca
  • Repository
  • 7 Watchers
  • 106 Stars
  • 1,601 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 5 Forks
  • 0 Open issues
  • 23 Versions
  • 9 % Grown

The README.md

Laravel Router

Latest stable release Software license Build status ![Total downloads][downloads-badge] Total stars, (*1)

Read my blog View my other packages and projects ![Follow @sebastiaanluca on Twitter][twitter-profile-badge] [Share this package on Twitter][link-twitter-share], (*2)

An organized approach to handling routes in Laravel., (*3)

This package provides you with an easy-to-use system to separate route logic into routers based on functionality while also providing additional functionality. A replacement for those bulky web.php and api.php route files that are often lacking any structure and break Laravel structure conventions of separating everything in classes instead of regular PHP files., (*4)

Do note that it changes nothing to the way you define your routes. It's just a way of organizing them. Optionally you can use the additional functionality it provides, but that's not a requirement., (*5)

Table of contents

Requirements

  • PHP 7.3 or higher
  • Laravel 7.0 or higher

Looking for support for earlier versions? Try out any of the previous package versions., (*6)

How to install

Just add the package to your project using Composer and Laravel will auto-discover it:, (*7)

composer require sebastiaanluca/laravel-router

Further optional setup

If you want to be able to register your routers in a single place, add the RegistersRouters trait to your HTTP kernel (found at App\Http\Kernel):, (*8)

<?php

namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;
use SebastiaanLuca\Router\Kernel\RegistersRouters;

class Kernel extends HttpKernel
{
    use RegistersRouters;
}

How to use

Creating a router

The following is an example of a router. It can be placed anywhere you like, though I'd suggest grouping them in the App\Http\Routers directory., (*9)

<?php

namespace App\Http\Routers;

use SebastiaanLuca\Router\Routers\Router;

class UserRouter extends Router
{
    /**
     * Map the routes.
     */
    public function map()
    {
        $this->router->group(['middleware' => ['web', 'guest']], function () {

            $this->router->get('/users', function () {

                return view('users.index');

            });

        });
    }
}

The map method is where you should define your routes and is the only requirement when using a router. The Laravel routing instance is automatically resolved from the IoC container, so you can use any standard routing functionality you want. Of course you can also use the Route facade., (*10)

Registering the router

To automatically have the framework load your router and map its routes, add the trait and add the router to the $routers array in your application's HTTP kernel class:, (*11)

/**
 * The application routers to automatically boot.
 *
 * @var array
 */
protected $routers = [
    \App\Http\Routers\UserRouter::class,
];

Manually registering the router

If you don't want to or can't add the trait to the kernel, you can also register the router manually by just instantiating it (in a service provider for instance). The parent base router will automatically resolve all dependencies and call the map method on your router., (*12)

app(\App\Http\Routers\UserRouter::class);

Especially useful in packages!, (*13)

Optional features

To use the following optional features, register the RegisterRoutePatterns class:, (*14)

<?php

namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;
use SebastiaanLuca\Router\Kernel\RegistersRouters;

class Kernel extends HttpKernel
{
    use RegistersRouters;

    /**
     * The application routers to automatically boot.
     *
     * @var array
     */
    protected $routers = [
        \SebastiaanLuca\Router\Routers\RegisterRoutePatterns::class,
    ];
}

Common route parameter patterns

Laravel provides a convenient way to validate URL parameters using patterns in routes. This package provides a predefined set of such patterns so you don't have to repeatedly add them to each route or define them yourself. The following parameter patterns are currently included:, (*15)

  • id (\d+)
  • hash ([a-z0-9]+)
  • uuid ([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})
  • slug ([a-z0-9-]+)
  • token ([a-zA-Z0-9]{64})

So forget about writing:, (*16)

Route::get('user/activations/{uuid}', function ($uuid) {
    return view('users.activations.show');
})->where('uuid', '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}');

Just use the {uuid} or any other pattern in your route:, (*17)

$this->router->get('user/activations/{uuid}', function ($uuid) {
    return view('users.activations.show');
});

Full-domain routing

Another great feature of Laravel is sub-domain routing which allows you to handle multiple subdomains within a single Laravel project. The only caveat there is that it only does that and doesn't handle full domains., (*18)

Laravel Router fixes that for you so you can direct multiple domains to a single Laravel project and handle them all at once. Simply define a route group with the {domain} pattern and use it in your callback or controller:, (*19)

$this->router->group(['domain' => '{domain}'], function () {

    $this->router->get('user/{id}', function ($domain, $id) {
        return 'You\'re visiting from ' . $domain;
    });

});

License

This package operates under the MIT License (MIT). Please see LICENSE for more information., (*20)

Change log

Please see CHANGELOG for more information what has changed recently., (*21)

Testing

bash composer install composer test, (*22)

Contributing

Please see CONTRIBUTING and CONDUCT for details., (*23)

Security

If you discover any security related issues, please email hello@sebastiaanluca.com instead of using the issue tracker., (*24)

Credits

About

My name is Sebastiaan and I'm a freelance Laravel developer specializing in building custom Laravel applications. Check out my portfolio for more information, my blog for the latest tips and tricks, and my other packages to kick-start your next project., (*25)

Have a project that could use some guidance? Send me an e-mail at hello@sebastiaanluca.com!, (*26)

The Versions

30/07 2018

dev-develop

dev-develop https://github.com/sebastiaanluca/laravel-router

An organized approach to handling Laravel routes.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel http route routing router laravel-router

30/07 2018

dev-master

9999999-dev https://github.com/sebastiaanluca/laravel-router

An organized approach to handling Laravel routes.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel http route routing router laravel-router

30/07 2018

5.0.1

5.0.1.0 https://github.com/sebastiaanluca/laravel-router

An organized approach to handling Laravel routes.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel route routing router laravel-router

30/07 2018

5.0.0

5.0.0.0 https://github.com/sebastiaanluca/laravel-router

An organized approach to handling Laravel routes.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel route routing router laravel-router

06/03 2018

4.0.0

4.0.0.0 https://github.com/sebastiaanluca/laravel-router

An organized approach to handling Laravel routes.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel route routing router laravel-router

27/10 2017

3.1.1

3.1.1.0 https://github.com/sebastiaanluca/laravel-router

An organized approach to handling Laravel routes.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel route routing router laravel-router

10/07 2017

3.1.0

3.1.0.0 https://github.com/sebastiaanluca/laravel-router

An organized approach to handling Laravel routes.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel route routing router laravel-router

24/06 2017

3.0.0

3.0.0.0 https://github.com/sebastiaanluca/laravel-router

An organized approach to handling Laravel routes.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel route routing router laravel-router

18/02 2017

dev-feature/testing/refactor-tests

dev-feature/testing/refactor-tests https://github.com/sebastiaanluca/laravel-router

An organized approach to handling Laravel routes. Provides additional functionality on top of the default HTTP router.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel http router laravel-router

24/01 2017

2.1.2

2.1.2.0 https://github.com/sebastiaanluca/laravel-router

An organized approach to handling Laravel routes. Provides additional functionality on top of the default HTTP router.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel http router laravel-router

24/01 2017

2.1.1

2.1.1.0 https://github.com/sebastiaanluca/laravel-router

An organized approach to handling Laravel routes. Provides additional functionality on top of the default HTTP router.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel http router laravel-router

18/08 2016

v2.0.4

2.0.4.0 https://github.com/sebastiaanluca/laravel-router

An organized approach to handling Laravel routes. Provides additional functionality on top of the default HTTP router.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel http router laravel-router

18/08 2016

v2.0.3

2.0.3.0 https://github.com/sebastiaanluca/laravel-router

An organized approach to handling Laravel routes. Provides additional functionality on top of the default HTTP router.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel http router laravel-router

18/08 2016

v1.x-dev

1.9999999.9999999.9999999-dev https://github.com/sebastiaanluca/laravel-router

An organized approach to handling Laravel routes. Provides additional functionality on top of the default HTTP router.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel http router laravel-router

18/08 2016

v1.0.0

1.0.0.0 https://github.com/sebastiaanluca/laravel-router

An organized approach to handling Laravel routes. Provides additional functionality on top of the default HTTP router.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel http router laravel-router

18/08 2016

v2.0.0

2.0.0.0 https://github.com/sebastiaanluca/laravel-router

An organized approach to handling Laravel routes. Provides additional functionality on top of the default HTTP router.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel http router laravel-router

18/08 2016

v2.0.1

2.0.1.0 https://github.com/sebastiaanluca/laravel-router

An organized approach to handling Laravel routes. Provides additional functionality on top of the default HTTP router.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel http router laravel-router

04/05 2016

0.1.4

0.1.4.0 https://github.com/sebastiaanluca/laravel-router

An organized approach to handling Laravel routes. Provides additional functionality on top of the default HTTP router.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel http router laravel-router

27/04 2016

0.1.3

0.1.3.0 https://github.com/sebastiaanluca/laravel-router

An organized approach to handling Laravel routes. Provides additional functionality on top of the default HTTP router.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel http router laravel-router

08/04 2016

0.1.2

0.1.2.0 https://github.com/sebastiaanluca/laravel-router

An organized approach to handling Laravel routes. Provides additional functionality on top of the default HTTP router.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel router laravel-router

08/04 2016

0.1.1

0.1.1.0 https://github.com/sebastiaanluca/laravel-router

An organized approach to handling Laravel routes. Provides additional functionality on top of the default HTTP router.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel router laravel-router

08/04 2016

0.1

0.1.0.0 https://github.com/sebastiaanluca/laravel-router

An organized approach to handling Laravel routes. Provides additional functionality on top of the default HTTP router.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel router laravel-router