Auto generate routes for Laravel Livewire components

Overview

livewire-auto-routes

Auto generate routes for Laravel Livewire Components.

Latest Stable Version Latest Unstable Version Total Downloads GitHub issues GitHub stars Software License

Requirements

  • Livewire 2
  • Laravel 8
  • php 8

Installation

composer require tanthammar/livewire-auto-routes

Routes in web.php takes precedence!

You can use web.php as normal. Routes declared in your Livewire components are registered after the routes in web.php.

Usage

  • You generate routes via traits or by adding a route() method to your Livewire component.
  • Your Livewire components can exist in any folder inside the app namespace.
  • If you don't add any of the traits or the route() method, the Livewire component is treated just as a normal component, thus ignored by this package.

Guest route Trait

  • Applies the guest middleware.
  • The property is used to generate both the route name and url.
use Tanthammar\LivewireAutoRoutes\HasGuestRoute;

class FooComponent extends \Livewire\Component
{
    use HasGuestRoute;
    
    protected string $guestRoute = '/foo/{id}/edit'; //route name becomes 'foo.id.edit'
}

Auth route Trait

  • Applies the auth middleware.
  • The property is used to generate both the route name and url.
use Tanthammar\LivewireAutoRoutes\HasAuthRoute;

class FooComponent extends \Livewire\Component
{
    use HasAuthRoute;
    
    protected string $authRoute = '/foo/{name?}'; //route name becomes 'foo.name'
}

Custom routes

Option 1

Declare the route just like you would in web.php

use Illuminate\Support\Facades\Route;

class FooComponent extends \Livewire\Component
{
    public function route(): \Illuminate\Routing\Route|array
    {
        return Route::get('foo', static::class)
            ->middleware('auth') //default middleware is 'web'
            ->name('foo');
    }
}

Option 2, use the RouteMaker

The RouteMaker can auto-generate the route name from the route definition, but it's optional.

use Tanthammar\LivewireAutoRoutes\RouteMaker;

class FooComponent extends \Livewire\Component
{
    public function route(): RouteMaker
    {
        return new RouteMaker(
            route: 'users/{id}/edit', //if you omit $name, this route name will become 'users.id.edit'
            middleware: ['auth:sanctum', 'verified'],
            component: static::class,
            name: 'users.edit' //OPTIONAL, else $name will be generated from $route
        );
    }
}

Routes are registered in alphabetical order!

Livewire component FILES are looped in alphabetical order in the app namespace. One way to control the load order is to group your components in subfolders with suitable names like routeGroupA, routeGroupB, where routes in "routeGroupA" would be registered before "routeGroupB".

Example using the Traits

It's recommended to keep a controlled naming structure to avoid route conflicts. Use the RouteMaker if you want better naming.

Directory(asc) = load order $authRoute or $guestRoute Generated route name
App/Foo/Users/Create.php users/create users.create
App/Foo/Users/CustomStuff.php users/custom-stuff/{id} users.custom-stuff.id
App/Foo/Users/Delete.php users/delete/{id} users.delete.id
App/Foo/Users/Edit.php users/edit/{id} users.edit.id
App/Foo/Users/Index.php users users
App/Foo/Users/Show.php users/{id} users.id

💬 Let's connect

Discuss with other tall-form users on the official Livewire Discord channel. You'll find me in the "partners/tall-forms" channel.

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security-related issues, please open an issue.

License

The MIT License (MIT). Please see License File for more information.

You might also like...
Boilerplate code for protecting a form with proof of work. Uses javascript in the browser to generate the hashcash and PHP on the server to generate the puzzle and validate the proof of work.

Boilerplate code for protecting a form with proof of work. Uses javascript in the browser to generate the hashcash and PHP on the server to generate the puzzle and validate the proof of work.

Generate trends for your models. Easily generate charts or reports.
Generate trends for your models. Easily generate charts or reports.

Laravel Trend Generate trends for your models. Easily generate charts or reports. Support us Like our work? You can support us by purchasing one of ou

Auto-generated Interface and Repository file via Repository pattern in Laravel

Auto-generated Repository Pattern in Laravel A repository is a separation between a domain and a persistent layer. The repository provides a collectio

Laravel and Lumen Auto Hard Deleter
Laravel and Lumen Auto Hard Deleter

Laravel Auto Hard Deleter This package deletes soft deleted rows automatically after a time interval that you define. For Laravel and Lumen 6, 7, 8 In

Use auto generated UUID slugs to identify and retrieve your Eloquent models.

Laravel Eloquent UUID slug Summary About Features Requirements Installation Examples Compatibility table Alternatives Tests About By default, when get

PHP Simple Response, XML, JSON,... auto response with accept in request's header

simple-response Simple package to handle response properly in your API. This package does not include any dependency. Install Via Composer $ composer

A system for auto-decorating models with presenters
A system for auto-decorating models with presenters

Laravel Auto Presenter 7 This package automatically decorates objects bound to views during the view render process. Features Automatically decorate o

Cagilo - a set of simple components for use in your views Laravel Blade.

Cagilo - a set of simple components for use in your views Laravel Blade. Official Documentation Documentation for Cagilo can be found on its we

Useful blade components and functionality for most Laravel projects.

laravel-base Note: Package is still in early stages of development, so functionality is subject to change. LaravelBase is a package I've created to pr

Releases(1.1.1)
Owner
Tina Hammar
Laravel packages
Tina Hammar
Laravel-comments-livewire - Livewire components for the laravel-comments package

Associate comments and reactions with Eloquent models This package contains Livewire components to be used with the spatie/laravel-comments package. S

Spatie 15 Jan 18, 2022
Laravel Livewire (TALL-stack) form generator with realtime validation, file uploads, array fields, blade form input components and more.

TALL-stack form generator Laravel Livewire, Tailwind forms with auto-generated views. Support Contributions Features This is not an admin panel genera

TinaH 622 Jan 2, 2023
Auto Generate laravel api Documentation

Laravel Dark Documentation Generator ?? You can create Documentation for your api easily by using this library Installation: Require this package with

HusseinAlaa 6 May 8, 2022
Livewire DataTables components for back-end. Modular, easy to use, with tons of features.

Livewire DataTables Livewire DataTables components for back-end. Modular, easy to use, with tons of features. Inspired by Caleb's Livewire Screencasts

Amir Rami 8 Jul 27, 2022
Livewire UI components with tailwind base style

WireUI ?? Documentation Wire UI is a library of components and resources to empower your Laravel and Livewire application development. Starting a new

WireUi 811 Jan 2, 2023
A package for defining scoped styles in Livewire components

A package for defining scoped styles in Livewire components

Lord Developer 4 Aug 29, 2022
This package allows you to render livewire components like a blade component, giving it attributes, slots etc

X-livewire This package allows you to render livewire components like a blade component, giving it attributes, slots etc. Assuming you wanted to creat

null 7 Nov 15, 2022
Laravel routes from Javascript

Laravel Javascript Routes Why? I love the Laravel 4 routing system and I often use named routes like route('users.show', array('id' => 1)) to generate

Fede Isas 63 Oct 10, 2022
Pretty routes for Laravel

Pretty Routes for Laravel Visualise your routes in pretty format. Installation composer require garygreen/pretty-routes If your using autodiscovery in

Gary Green 644 Dec 15, 2022
View your Laravel routes on the browser.

View your Laravel routes on the browser. This package adds a route to your Laravel application. Once you've installed this package, enter /route-list

Patompong Savaengsuk 23 Oct 28, 2022