aldrumo/route-loader

Laravel based route loader for use with sluggable items

0.2.1 2022-08-16 14:56 UTC

This package is auto-updated.

Last update: 2024-10-16 19:46:04 UTC


README

Laravel package for loading dynamic sluggable routes.

Setup

Create class that implements \Aldrumo\RouteLoader\Contracts\RouteLoader.

This should return a collection that contains items with a slug and id attribute. This will likely be "Page" models.

<?php

namespace App\Routes;

use Aldrumo\RouteLoader\Contracts\RouteLoader;
use App\Models\Page;
use Illuminate\Support\Collection;

class FilledRouteLoader implements RouteLoader
{
    public function getRoutes(): Collection
    {
        return Page::where('is_active', true)->get();
    }
}

Register this to the container in your app service provider.

use Aldrumo\RouteLoader\Contracts\RouteLoader;
use App\Routes\PageRouteLoader;
$this->app->bind(
    RouteLoader::class,
    PageRouteLoader::class
);

Generator

Place the following lins in the "boot" method of your apps service provider

use Aldrumo\RouteLoader\Generator;
use Illuminate\Support\Facades\Route;

Route::middleware('web')
    ->group(
        function () {
            $this->app[Generator::class]->generateRoutes();
        }
    );

Clearing Routes

resolve(\Aldrumo\RouteLoader\Generator::class)->clearRoutes();
php artisan aldrumo:clear-routes