• This repository has been archived on 08/Mar/2022
  • Stars
    star
    129
  • Rank 278,629 (Top 6 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 8 years ago
  • Updated almost 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

THIS PACKAGE HAS BEEN DEPRECATED — An organized approach to handling routes in Laravel.

Laravel Router

Latest stable release Software license Build status Total downloads Total stars

Read my blog View my other packages and projects Follow @sebastiaanluca on Twitter Share this package on Twitter

An organized approach to handling routes in Laravel.

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.

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.

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.

How to install

Just add the package to your project using Composer and Laravel will auto-discover it:

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):

<?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.

<?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.

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:

/**
 * 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.

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

Especially useful in packages!

Optional features

To use the following optional features, register the RegisterRoutePatterns class:

<?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:

  • 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:

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:

$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.

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:

$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.

Change log

Please see CHANGELOG for more information what has changed recently.

Testing

composer install
composer test

Contributing

Please see CONTRIBUTING and CONDUCT for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

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.

Have a project that could use some guidance? Send me an e-mail at [email protected]!

More Repositories

1

laravel-helpers

THIS PACKAGE HAS BEEN DEPRECATED — An extensive set of Laravel framework helper functions and collection macros.
PHP
418
star
2

php-pipe-operator

Method chaining for any value using any method.
PHP
280
star
3

laravel-module-loader

THIS PACKAGE HAS BEEN DEPRECATED — A lightweight package to organize your code into contextual modules.
PHP
77
star
4

laravel-auto-morph-map

THIS PACKAGE HAS BEEN DEPRECATED — Automatically alias and map the polymorphic types of Eloquent models.
PHP
54
star
5

laravel-boolean-dates

Automatically convert Eloquent model boolean attributes to dates (and back).
PHP
35
star
6

php-helpers

THIS PACKAGE HAS BEEN DEPRECATED — An extensive set of PHP helper functions and classes.
PHP
27
star
7

laravel-4-eventcron

THIS PACKAGE HAS BEEN DEPRECATED — Laravel 4.x database event queue
PHP
27
star
8

laravel-conditional-providers

THIS PACKAGE HAS BEEN DEPRECATED — Load Laravel service providers and facades based on the current environment.
PHP
26
star
9

laravel-skeleton-deprecated-2

THIS PACKAGE HAS BEEN DEPRECATED — A ready-to-go Laravel skeleton application.
PHP
21
star
10

laravel-route-model-autobinding

THIS PACKAGE HAS BEEN DEPRECATED — Automatically bind Eloquent models as route segment variables.
PHP
14
star
11

php-stub-generator

THIS PACKAGE HAS BEEN DEPRECATED — Easily generate files and classes from stubs.
PHP
10
star
12

laravel-unbreakable-migrations

THIS PACKAGE HAS BEEN DEPRECATED — Prevent your Laravel database migrations from failing by wrapping them in transactions.
PHP
9
star
13

laravel-blog

THIS PACKAGE HAS BEEN DEPRECATED — A simple-to-use blog system you can pull into any existing Laravel project and immediately get writing.
PHP
5
star
14

php-codesniffer-ruleset

THIS PACKAGE HAS BEEN DEPRECATED — An opinionated custom coding standard
4
star
15

laravel-changelog

THIS PACKAGE HAS BEEN DEPRECATED — Show your project's changelog in your application.
PHP
4
star
16

laravel-goodies

THIS PACKAGE HAS BEEN DEPRECATED — A curated list of Laravel packages and other goodies.
3
star
17

laravel-validator

THIS PACKAGE HAS BEEN DEPRECATED — Properly handle user input by using your validation rules as source of truth
PHP
3
star
18

laravel-presets

THIS PACKAGE HAS BEEN DEPRECATED — Various plug-and-play presets to scaffold your existing project.
PHP
3
star
19

Gmail-Tracker-Extension

THIS PACKAGE HAS BEEN DEPRECATED — Chrome extension for Gmail to enable tracking emails.
JavaScript
2
star
20

alarmclock

NodeJS alarm clock for the Raspberry Pi.
JavaScript
2
star
21

laravel-skeleton

An opinionated fresh Laravel project to help you get started.
PHP
1
star
22

laravel-resource-flow

THIS PACKAGE HAS BEEN DEPRECATED — Quickly scaffold access to a resource.
PHP
1
star
23

P5RuntimeFullScreen

THIS PACKAGE HAS BEEN DEPRECATED — Fullscreen mode for Processing that actually lets you switch (on the fly) between a windowed and a fullscreen sketch. With support for Processing 2 (beta), customisable hotkeys, OpenGL, Proscene, Eclipse, …
Java
1
star