All Projects → sebastiaanluca → Laravel Router

sebastiaanluca / Laravel Router

Licence: mit
An organized approach to handling routes in Laravel.

Projects that are alternatives of or similar to Laravel Router

Ziggy
Use your Laravel named routes in JavaScript
Stars: ✭ 2,619 (+1914.62%)
Mutual labels:  laravel, routes
router
Bidirectional Ring router. REST oriented. Rails inspired.
Stars: ✭ 78 (-40%)
Mutual labels:  router, routes
Vue Routisan
Elegant, fluent route definitions for Vue Router, inspired by Laravel. v3 is currently in beta. [email protected]
Stars: ✭ 193 (+48.46%)
Mutual labels:  laravel, router
Next Routes
Universal dynamic routes for Next.js
Stars: ✭ 2,354 (+1710.77%)
Mutual labels:  router, routes
Laravel
Muito conteúdo sobre o framework Laravel. Controllers, Models, Views, Blade, Migrations, Seeders, Middlewares, Autenticação, Autorização, Providers, pacotes, laravel 8, etc.
Stars: ✭ 43 (-66.92%)
Mutual labels:  laravel, routes
routex.js
🔼 Alternative library to manage dynamic routes in Next.js
Stars: ✭ 38 (-70.77%)
Mutual labels:  router, routes
STCRouter
基于标准URL的iOS路由系统,可实现业务模块组件化,控制器之间零耦合,可实现黑白名单控制,可进行native降级到hybrid。
Stars: ✭ 19 (-85.38%)
Mutual labels:  router, routes
gatsby-plugin-dynamic-routes
Creating dynamic routes based on your environment and/or renaming existing routes
Stars: ✭ 14 (-89.23%)
Mutual labels:  router, routes
Rapid.js
An ORM-like Interface and a Router For Your API Requests
Stars: ✭ 700 (+438.46%)
Mutual labels:  laravel, router
Iris
The fastest HTTP/2 Go Web Framework. AWS Lambda, gRPC, MVC, Unique Router, Websockets, Sessions, Test suite, Dependency Injection and more. A true successor of expressjs and laravel | 谢谢 https://github.com/kataras/iris/issues/1329 |
Stars: ✭ 21,587 (+16505.38%)
Mutual labels:  laravel, router
Routegen
Define your API and SPA routes in one place. Use them anywhere. Only 1.3kb.
Stars: ✭ 86 (-33.85%)
Mutual labels:  router, routes
Nova Route Viewer
Route viewer tool for Laravel Nova
Stars: ✭ 54 (-58.46%)
Mutual labels:  laravel, routes
Universal Router
A simple middleware-style router for isomorphic JavaScript web apps
Stars: ✭ 1,598 (+1129.23%)
Mutual labels:  router, routes
Aimeos
Integrated online shop based on Laravel 8 and the Aimeos e-commerce framework
Stars: ✭ 2,354 (+1710.77%)
Mutual labels:  laravel
Laravel Social Email Authentication
Laravel 5.3 bootstrap app with Multi Auth, Social and Email Authentication. Google re-Captcha, Facebook, Twitter, G+ and much more..
Stars: ✭ 129 (-0.77%)
Mutual labels:  laravel
Laravel Phone
Phone number functionality for Laravel
Stars: ✭ 1,806 (+1289.23%)
Mutual labels:  laravel
Fastroute
Simple, idiomatic and fast 161 loc http router for golang
Stars: ✭ 128 (-1.54%)
Mutual labels:  router
Laravel Hashids
A Hashids bridge for Laravel
Stars: ✭ 1,714 (+1218.46%)
Mutual labels:  laravel
Luthier Ci
Improved routing, middleware support, authentication tools and more for CodeIgniter 3 framework
Stars: ✭ 129 (-0.77%)
Mutual labels:  router
Laravel Package Generator
A laravel package generator
Stars: ✭ 128 (-1.54%)
Mutual labels:  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]!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].