PHPackages                             sebastiansulinski/laravel-routes - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. sebastiansulinski/laravel-routes

AbandonedArchivedLibrary[API Development](/categories/api)

sebastiansulinski/laravel-routes
================================

Route Collections and Route Model Binding wrappers for Laravel

1.0.0(10y ago)01.1kMITPHPPHP &gt;=5.4.0

Since Jun 21Pushed 6y ago1 watchersCompare

[ Source](https://github.com/sebastiansulinski/laravel-routes)[ Packagist](https://packagist.org/packages/sebastiansulinski/laravel-routes)[ RSS](/packages/sebastiansulinski-laravel-routes/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (2)Used By (0)

Route Collections and Route Model Binding wrappers for Laravel
==============================================================

[](#route-collections-and-route-model-binding-wrappers-for-laravel)

[![No longer maintained](https://camo.githubusercontent.com/0c114e5e6d30334c2a18de17be5d422ec26f12367cd9994f1502e24ddc1e9dc4/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4d61696e74656e616e63652d4f46462d7265642e737667)](https://camo.githubusercontent.com/0c114e5e6d30334c2a18de17be5d422ec26f12367cd9994f1502e24ddc1e9dc4/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4d61696e74656e616e63652d4f46462d7265642e737667)

### \[DEPRECATED\] This repository is no longer maintained

[](#deprecated-this-repository-is-no-longer-maintained)

> While this project is functional with older versions of Laravel, the dependencies are no longer up to date. You are still welcome to explore, learn, and use the code provided here.

Route collections
-----------------

[](#route-collections)

Crate a new directory that will store all route collections for your application - example would be directory called `Routes/Collections` under `app/Http`.

```
app/Http/Routes/Collections

```

Inside the directory create a new class that will be used with the given section of your system - say for the Front end of your application, we use `FrontCollection.php` and we put it into another directory - to distinguish between sections - because this one belongs to the front end, we call it `Front`.

```
app/Http/Routes/Collections/Front/FrontCollection.php

```

The new `FrontCollection` class will extend the `SSD\LaravelRoutes\RouteCollectionFactory` and needs to have the method `getNameSpace` that returns the current namespace, which will be used to create a fully qualifying name of each class within the `app/Http/Routes/Collections/Front` directory.

```
// app/Http/Routes/Collections/Front/FrontCollection.php

namespace App\Http\Routes\Collections\Front;

use SSD\LaravelRoutes\RouteCollectionFactory;

class FrontCollection extends RouteCollectionFactory
{

    protected static function getNameSpace()
    {
        return __NAMESPACE__;
    }

}

```

Inside the same directory create a new file for each collection of routes - say for the `Blog` module of your Front section you could use:

```
// app/Http/Routes/Collections/Front/Blog.php

namespace App\Http\Routes\Collections\Front;

use SSD\LaravelRoutes\RouteCollectionContract;

class Blog implements RouteCollectionContract
{

    public function routes()
    {

        app('router')->group(['prefix' => 'blog'], function() {

            app('router')->get('/', 'BlogController@index');

            app('router')->get('latest', 'BlogController@latest');

            app('router')->post('comment', 'BlogController@addComment');

            app('router')->get('comment/{id}', 'BlogController@showComment');

        });

    }

}

```

and perhaps another one for the `Contact` Controller with form submission method:

```
// app/Http/Routes/Collections/Front/Contact.php

namespace App\Http\Routes\Collections\Front;

use SSD\LaravelRoutes\RouteCollectionContract;

class Contact implements RouteCollectionContract
{

    public function routes()
    {

        app('router')->group(['prefix' => 'contact'], function() {

            app('router')->get('/', 'ContactController@index');

            app('router')->post('/', 'ContactController@submit');

        });

    }

}

```

Now all you need to do in your `app/Http/routes.php` file is:

```
use App\Http\Routes\Collections\Front\FrontCollection;

FrontCollection::blog();
FrontCollection::contact();

```

The magically called static methods on the `FrontCollection` are names of the collection classes in `camelCase` - say for instance collection with name `FoodRecepies` would be called as `FrontCollection::foodRecepies()` and so on.

If you want to keep your `routes.php` file even cleaner, you could create a master collection for each section and then enclose all separate route collections inside of it

```
// app/Http/Routes/Collections/Front/Master.php

namespace App\Http\Routes\Collections\Front;

use SSD\LaravelRoutes\RouteCollectionContract;

class Master implements RouteCollectionContract
{

    public function routes()
    {

        FrontCollection::blog();
        FrontCollection::contact();

    }

}

```

and for the `Admin` section (make sure you first create `AdminCollection`)

```
// app/Http/Routes/Collections/Admin/Master.php

namespace App\Http\Routes\Collections\Admin;

use SSD\LaravelRoutes\RouteCollectionContract;

class Master implements RouteCollectionContract
{

    public function routes()
    {

        app('router')->group(
            [
                'prefix' => 'admin',
                'namespace' => 'Admin'
            ],
            function() {

                AdminCollection::auth();

                app('router')->group(
                    [
                        'middleware' => ['admin']
                    ],
                    function() {

                        AdminCollection::blog();
                        AdminCollection::pages();

                    }
                );

            }
        );

    }

}

```

Then simply call it from within the routes.php

```
use App\Http\Routes\Collections\Front\FrontCollection;
use App\Http\Routes\Collections\Admin\AdminCollection;

FrontCollection::master();
AdminCollection::master();

```

### Custom exceptions

[](#custom-exceptions)

The abstract `SSD\LaravelRoutes\RouteCollectionFactory` class can throw either `SSD\LaravelRoutes\Exceptions\InvalidClassName` when the static method name does not correspond to the existing class or `SSD\LaravelRoutes\Exceptions\MissingNamespace` when you forget to declare the `getNameSpace()` method on the class extending `SSD\LaravelRoutes\RouteCollectionFactory`.

Route model binder
------------------

[](#route-model-binder)

Route model binder allows you to group model bindings.

To start, create a new directory under `app/Http/Routes` called `ModelBindings`

```
app/Http/Routes/ModelBindings

```

Inside this directory create a class corresponding to the model you are trying to define bindings for - for instance, if you had a `Blog` model on which you'd like to define two bindings - one for `blog_id` and the other for the `blog_slug`

```
app('router')->get('blog/{blog_id}', 'BlogController@edit');
app('router')->get('blog/{blog_slug}', 'BlogController@show');

```

your model would look like so

```
// app/Http/Routes/ModelBindings/BlogBinder.php

namespace App\Http\Routes\ModelBindings;

use Illuminate\Routing\Router;
use SSD\LaravelRoutes\RouteModelBinderContract;

use App\Blog;

class BlogBinder implements RouteModelBinderContract
{

    public function bind(Router $router)
    {

        $router->model('blog_id', Blog::class);
        // for version of PHP lower than 5.6 use:
        // $router->model('blog_id', 'App\Blog');

        $router->bind('blog_slug', function($slug) {

            return $this->recordBySlug($slug);

        });

    }

    protected function recordBySlug($slug)
    {

        return Blog::whereSlug($slug)->firstOrFail();

    }

}

```

Add the `scopeWhereSlug()` method to your `Blog` model (or, if you're using slugs on more than one model you could extract it to a Trait)

```
// app/Blog.php

namespace App;

class Blog extends Model
{

    protected $table = 'blog';

    public function scopeWhereSlug($query, $slug)
    {

        return $query->where('slug', '=', $slug);

    }

}

```

Now, with the `BlogBinder` ready, we can add it to the `app/Providers/RouteServiceProvider.php'

```
// app/Providers/RouteServiceProvider.php

namespace App\Providers;

use Illuminate\Routing\Router;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;

use SSD\LaravelRoutes\RouteModelBinderFactory;
use App\Http\Routes\ModelBindings\BlogBinder;

class RouteServiceProvider extends ServiceProvider
{

    protected $namespace = 'App\Http\Controllers';

    public function boot(Router $router)
    {

        parent::boot($router);

        RouteModelBinderFactory::bind(new BlogBinder, $router);

    }

    public function map(Router $router)
    {
        $router->group(['namespace' => $this->namespace], function ($router) {
            require app_path('Http/routes.php');
        });
    }
}

```

### Model binding tip

[](#model-binding-tip)

You are more likely to use `slug` with the front end of your application - so just to boost the performance a bit, let's cache the model for the `slug` binding. To do this - create a new, `BaseBinder` class under `App\Http\Routes\ModelBindings` and to make it easier - let's use `nespot/carbon` package. First add `Carbon` dependency with the composer.

```
composer require nesbot/carbon

```

Now create `BaseBinder` class with the `cache` method. I'm caching for just one day, but feel free to make it as long as you need

```
// app/Http/Routes/ModelBindings/BaseBinder.php

namespace App\Http\Routes\ModelBindings;

use Carbon\Carbon;

abstract class BaseBinder {

    protected function cache($key, callable $default)
    {

        $value = app('cache.store')->get($key);

        if (is_null($value)) {

            $arguments = func_get_args();

            $value = call_user_func_array($default, array_splice($arguments, 2));

            app('cache.store')->put($key, $value, Carbon::now()->addDay(1));

        }

        return $value;

    }

}

```

Finally modify the `BlogBinder` class

```
// app/Http/Routes/ModelBindings/BlogBinder.php

namespace App\Http\Routes\ModelBindings;

use Illuminate\Routing\Router;
use SSD\LaravelRoutes\RouteModelBinderContract;

use App\Blog;

class BlogBinder extends BaseBinder implements RouteModelBinderContract
{

    public function bind(Router $router)
    {

        $router->model('blog_id', Blog::class);

        $router->bind('blog_slug', function($slug) {

            return $this->cache(
                'blog.slug.'.$slug,
                [
                    $this,
                    'recordBySlug'
                ],
                $slug
            );

        });

    }

    protected function recordBySlug($slug)
    {

        return Blog::whereSlug($slug)->firstOrFail();

    }

}

```

And now your model binding will first be served from the database, then, every sub-sequent call will be read from cache for a length of one day. Make sure that when you update record - you also update cached version - or simply remove cache for a corresponding key.

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

3985d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/2211203?v=4)[Sebastian Sulinski](/maintainers/sebastiansulinski)[@sebastiansulinski](https://github.com/sebastiansulinski)

---

Top Contributors

[![sebastiansulinski](https://avatars.githubusercontent.com/u/2211203?v=4)](https://github.com/sebastiansulinski "sebastiansulinski (1 commits)")

### Embed Badge

![Health badge](/badges/sebastiansulinski-laravel-routes/health.svg)

```
[![Health](https://phpackages.com/badges/sebastiansulinski-laravel-routes/health.svg)](https://phpackages.com/packages/sebastiansulinski-laravel-routes)
```

###  Alternatives

[nuwave/lighthouse

A framework for serving GraphQL from Laravel

3.5k10.7M93](/packages/nuwave-lighthouse)[mpociot/laravel-apidoc-generator

Generate beautiful API documentation from your Laravel application

3.5k3.1M12](/packages/mpociot-laravel-apidoc-generator)[andreaselia/laravel-api-to-postman

Generate a Postman collection automatically from your Laravel API

1.0k586.2k3](/packages/andreaselia-laravel-api-to-postman)[api-ecosystem-for-laravel/dingo-api

A RESTful API package for the Laravel and Lumen frameworks.

3121.5M10](/packages/api-ecosystem-for-laravel-dingo-api)[reindert-vetter/api-version-control

A Laravel package to manage versions of endpoints in an elegant way

1671.0M](/packages/reindert-vetter-api-version-control)[flat3/lodata

OData v4.01 Producer for Laravel

96320.9k](/packages/flat3-lodata)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
