Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A bug was detected when using the prefix method without group method #49039

Closed
andrey-helldar opened this issue Nov 18, 2023 · 5 comments
Closed

Comments

@andrey-helldar
Copy link
Contributor

andrey-helldar commented Nov 18, 2023

Laravel Version

10.32.1

PHP Version

8.2.12

Database Driver & Version

No response

Description

When using the prefix method on routes that are not groups, it incorrectly generates the destination URL.

It should be like this:

POST api/orders/some ..... orders.index › OrderController@index
POST api/webhook/some ............. webhook › WebhookController

But this option is displayed instead:

POST api/orders/some ..... orders.index › OrderController@index
POST webhook/api/some ............. webhook › WebhookController

Steps To Reproduce

  1. Create a controllers:
    php artisan make:controller OrderController
    php artisan make:controller WebhookController -i
  2. Create routes:
    use App\Http\Controllers\OrderController;
    use App\Http\Controllers\WebhookController;
    
    app('router')
        ->name('orders.')
        ->prefix('orders')
        ->controller(OrderController::class)
        ->group(static function () {
            app('router')->post('some', 'index')->name('index');
        });
    
    app('router')
        ->name('webhook')
        ->prefix('webhook')
        ->post('some', WebhookController::class);
  3. Execute the console command:
    php artisan route:list
routes.bug.mp4
@crynobone
Copy link
Member

Hey there, thanks for reporting this issue.

We'll need more info and/or code to debug this further. Can you please create a repository with the command below, commit the code that reproduces the issue as one separate commit on the main/master branch and share the repository here?

Please make sure that you have the latest version of the Laravel installer in order to run this command. Please also make sure you have both Git & the GitHub CLI tool properly set up.

laravel new bug-report --github="--public"

Do not amend and create a separate commit with your custom changes. After you've posted the repository, we'll try to reproduce the issue.

Thanks!

@andrey-helldar
Copy link
Contributor Author

Repository: https://github.com/andrey-helldar/laravel-issue-49039
Commit: https://github.com/andrey-helldar/laravel-issue-49039/commit/3f6a81c4063129cf324ae4338e0a03558ca61e49
Result:

Helldar@HellPC MINGW64 /d/domains/bug-report (main)
$ php artisan route:list

  POST       api/orders/some .................................................... orders.index › OrderController@index
  POST       webhook/api/some ............................................................ webhook › WebhookController

@nikopeikrishvili
Copy link
Contributor

Hello, I have tested with the examples provider, and I have the same result as @andrey-helldar
Screenshot 2023-11-20 at 14 47 26

@andrey-helldar
Copy link
Contributor Author

andrey-helldar commented Nov 20, 2023

I've also noticed that it doesn't matter what route and controller names are used. The only important thing is to call the prefix method without using a group:

// incorrect
app('router')->prefix('some')->get('foo', SomeController::class);

// correct
app('router')->prefix('some')->group(
    fn () => app('router')->get('foo', SomeController::class);
);

For example:

app('router')->name('foo')->prefix('foo')->get('some', WebhookController::class);
app('router')->name('foo')->prefix('foo')->post('some', WebhookController::class);
app('router')->name('foo')->prefix('foo')->put('some', WebhookController::class);
app('router')->name('foo')->prefix('foo')->patch('some', WebhookController::class);
app('router')->name('foo')->prefix('foo')->delete('some', WebhookController::class);

app('router')->name('bar')->prefix('bar')->get('some', [WebhookController::class, 'some']);
app('router')->name('bar')->prefix('bar')->post('some', [WebhookController::class, 'some']);
app('router')->name('bar')->prefix('bar')->put('some', [WebhookController::class, 'some']);
app('router')->name('bar')->prefix('bar')->patch('some', [WebhookController::class, 'some']);
app('router')->name('bar')->prefix('bar')->delete('some', [WebhookController::class, 'some']);

Result:

Helldar@HellPC MINGW64 /d/domains/bug-report (main)
$ art r:l

  GET|HEAD  bar/api/some ................................................................ WebhookController@some
  POST      bar/api/some ................................................................ WebhookController@some
  PUT       bar/api/some ................................................................ WebhookController@some
  PATCH     bar/api/some ................................................................ WebhookController@some
  DELETE    bar/api/some ................................................................ WebhookController@some
  GET|HEAD  foo/api/some ............................................................... foo › WebhookController
  POST      foo/api/some ............................................................... foo › WebhookController
  PUT       foo/api/some ............................................................... foo › WebhookController
  PATCH     foo/api/some ............................................................... foo › WebhookController
  DELETE    foo/api/some ............................................................... foo › WebhookController

@crynobone
Copy link
Member

I would feel the current behavior is expected, since prefix is more useful for grouped routes. Feel free to submit PR if you want to improve the behavior

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants