GithubHelp home page GithubHelp logo

mathewroy12 / livewire-auto-routes Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tinahammar/livewire-auto-routes

0.0 0.0 0.0 41 KB

Auto generate routes for Laravel Livewire components

License: MIT License

PHP 100.00%

livewire-auto-routes's Introduction

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

Use Spatie pkg

I recommend that you try Spatie Laravel Route Attributes before installing this package.

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 $guestRouteGenerated route name
App/Foo/Users/Create.phpusers/createusers.create
App/Foo/Users/CustomStuff.phpusers/custom-stuff/{id}users.custom-stuff.id
App/Foo/Users/Delete.phpusers/delete/{id}users.delete.id
App/Foo/Users/Edit.phpusers/edit/{id}users.edit.id
App/Foo/Users/Index.phpusersusers
App/Foo/Users/Show.phpusers/{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.

livewire-auto-routes's People

Contributors

tanthammar avatar mathewroy12 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.