Giter Site home page Giter Site logo

spatie / laravel-route-discovery Goto Github PK

View Code? Open in Web Editor NEW
222.0 3.0 26.0 198 KB

Automatically discover routes in a Laravel app

Home Page: https://spatie.be/docs/laravel-route-discovery

License: MIT License

PHP 99.97% Blade 0.03%
laravel route discovery php

laravel-route-discovery's Introduction

Automatically discover routes in a Laravel app

Latest Version on Packagist Tests Total Downloads

This package can automatically discover routes for controllers and views in your Laravel application. This isn't an all-in approach. While using use auto discovery, you can still register routes like you're used to.

// typically in a routes file

Discover::controllers()->in($whateverDirectoryYouPrefer);
Discover::views()->in($whateverDirectoryYouPrefer);

// other routes

Using PHP attributes you can manipulate discovered routes: you can set a route name, add some middleware, or ...

Here's how you would add middleware to a controller whose's route will be auto discovered.

namespace App\Http\Controllers;

use Illuminate\Routing\Middleware\ValidateSignature;
use Spatie\RouteDiscovery\Attributes\Route;

class MyController
{
    #[Route(middleware: ValidateSignature::class)]
    public function myMethod() { /* ... */ }
}

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Documentation

You'll find full documentation at the Spatie website.

A note on performance

Discovering routes during each application request may have a small impact on performance. For increased performance, we highly recommend caching your routes as part of your deployment process.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

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

laravel-route-discovery's People

Contributors

27pchrisl avatar adrianmrn avatar freekmurze avatar ijpatricio avatar laravel-shift avatar leonvanderhaas avatar nunomaduro avatar owenvoke avatar sebastiandedeyne avatar szepeviktor avatar vanyamil avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

laravel-route-discovery's Issues

First Issue

I installed this on new project.

I guess, there should be option to exclude files/directories, so they will not be discovered. (I saw many routes from default laravel breeze also added.)

See this:
image

Also when I used this on one route. Added # annotation to ProfileController and loaded profile form page and I saw request()->user() was null. (I saw I am currently logged in and on top right it shows logged in user name). It redirected me to dashboard.

[help] Missing uri index method

Controller

<?php

namespace App\Http\Controllers\Legacy;

use App\Http\Controllers\Controller;

class ExampleController extends Controller
{
    public function index()
    {
    }
}

Output

 GET|HEAD   example ............. example › Legacy\ExampleController@index

Can we still added the uri index like this?

 GET|HEAD   example ............. example › Legacy\ExampleController@index
 GET|HEAD   example/index ............. example › Legacy\ExampleController@index

[feature request] resource route discovery

This package work great but required to set attributes to almost all my action because I use a lot of nested resource.

I suggest that we can add a resource route discovery.

<?php

Discover::resources()->in($irectory);
$ artisan route:list --except-vendor
GET|HEAD   users ............................................. users.index > UserController@index
GET|HEAD   users/dummy ....................................... users.dummy > UserController@dummy
POST       users/{user} ...................................... users.store > UserController@store
GET|HEAD   users/{user}/friends ............... users.friends.index > User\FriendController@index
POST       users/{user}/friends/request ... users.friends.request > User\FriendController@request
GET|HEAD   users/{user}/friends/{friend} ........ users.friends.show > User\FriendController@show

Prefix Controller with Attribute

This package made my day. No controversy here :)

I see that prefixes can be used to group during discovery if they're in a different directory, but how about on the controller via an attribute?

#[Route(prefix: 'anotherRoute/{someValue}')]
class MyController extends Controller
{
    public function foo(string $someValue) ...
}

anotherRoute/1234/foo

PendingRouteFactory assumes all controllers are in App\.. namespace

We use a different directory strucutre in our apps. Instead of an app directory we put our files in src\MyNewApp and set up laravel to work with this.

In composer.json

"autoload": {
        "psr-4": {
            "MyNewApp\\": "src/MyNewApp"
        }
    },

and in bootstrap/app.php

$app->useAppPath(dirname(__DIR__) . '/src/MyNewApp');

Laravel allows this and it works perfectly fine.

app_path() // returns ../my-folder/src/MyNewApp
base_path() // returns ../my-folder
app()->getNamespace() // returns 'MyNewApp\'

Unfortunately, the PendingRouteFactory assumes all controllers are in App\... namespace

So,

  • Is there a way around this?
  • Am I missing something?
  • Can we fix this?

Base traits' methods being discovered

Hello!

Really excited to see this package come to life.

I'm so sorry if this is something I overlooked, but I didn't find anything about this.

I went ahead and made the first controller, WelcomeController, through the artisan CLI.
The generated controller naturally extends the App\Http\Controllers\Controller, which in turn, extends Illuminate\Routing\Controller and uses traits.

This is resulting in having a controller with one method, and registering 13 routes. Picture attached.

Is this somewhat expected and let it be?

Would you accept a PR to try and fix this?

image

Route naming wrongly assigned

Hi, i'm having trouble with a situation with my UserController, when using Discovery in mi api.php file everything works fine, but when i use php artisan route:cache an error occurs:

Unable to prepare route [api/user/{user}] for serialization. Another route has already been assigned name [user].

I know this is a framework warning, but when looking for answers I found that methods on my UserController where not named correctly, instead all methods used the name "user" as the image show below:

image

As you can see index, show, update and delete methods use the same name.

To solve this I MUST specify "name" attribute on ALL methods to avoid this naming collisions

image

So artisan route:list -v show this

image

And artisan route:cache result is fine then

image

THE THING IS...
In your docs you said that names will be assigned by default, so a won't need to set them one by one on every method.

So, why all my user routes have the same name by default?

Thanks in advance!

Question

Hey Freek, think that you did a great job with this package (again)! Very good idea.

I've just wanted to try it out in a new project, so that I could start using the package for the whole project right away, before the package is officially released.

However, I just can't get it to work. I tried several things, like publishing the config file, adding routes in the web.php..
It's just weird.

The project is using a DDD structure, just like Laravel Beyond CRUD.

I have no idea what's going wrong here. So I'm just wondering if the latest version is supposed to work (which I assume)?

Thanks!

Multiple Where statements

Hi,

first I want to say, I really like this package, never thought routing could be this easy!

I just have one small issue: I want to make a resource route like locations/{location_id}/questions/{question_id} but because I can make just one Where per Route, i can only check one parameter per route, not both. Is there any fix for this?

Thanks, I really appreciate the work for all your nice packages!

Base path is prefixed?

Just downloaded this package. Testing it out, it seems like all route URLs and names are prefixed with the whole directory path. Example:
GET|HEAD c:/repos/project/app/http/controllers/users c:.repos.project.app.http.…

Is this because I am on WIndows and the drive paths aren't recognized?

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.