laravel-route-attributes icon indicating copy to clipboard operation
laravel-route-attributes copied to clipboard

Patch handling of multiple directories in config (nested)

Open dreadkopp opened this issue 2 years ago • 1 comments

ISSUE

When using multiple Directories in the config (as seen in readme::installation) routes get registered multiple times.

i.e. having a Structure like this:

App\Http\Controllers\
    SomeController.php
    Api\
        AnotherController.php

and using a config like:

...
'directories' => [
        app_path('Http/Controllers'),
        app_path('Http/Controllers/API') => [
            'prefix' => 'api',
        ],
]
...

it will result in the routes of AnotherController to get registered twice. once without the path prefix, and one time with the correct prefix

POC

sample from project i am currently working on

╰─❯ ./sail artisan route:list --name=location.getList                                                                                                                                                                                       

  GET|HEAD       api/locations .......................................................................................................................................... api.location.getList › API\Location\LocationApiController@locationsApi
  GET|HEAD       locations .............................................................................................................................................. api.location.getList › API\Location\LocationApiController@locationsApi

                                                                                                                                                                                                                              Showing [2] routes



PATCHED


╰─❯ ./sail artisan route:list --name=location.getList                              

  GET|HEAD       api/locations .......................................................................................................................................... api.location.getList › API\Location\LocationApiController@locationsApi

                                                                                                                                                                                                                              Showing [1] routes

config from sample

...
'directories' => [
        app_path('Http/Controllers'),
        app_path('Http/Controllers/API') => [
            'prefix' => 'api',
        ],
    ],
...

LocationsApiController has a route named "api.location.getList"

Purpose

This pull request tries to resolve that issue ( also makes some private methods protected... since everyone should be able to extend a software easily, not by copy pasting i.e. the whole ServiceProvider :) )

TODO:

  • [ ] ServiceProviderWithMultipleDirectoriesInConfigTest should not fail assertions ( maybe i am missing something in the test setup )

dreadkopp avatar Aug 24 '22 19:08 dreadkopp

i will not find time to patch the test in this issue / mr in the next 2 weeks. Is someone able to patch it ?

integrated in a project these patches work as expected. i just made a mess out of the serviceprovidertest it guess

dreadkopp avatar Aug 31 '22 23:08 dreadkopp

Closing due to inactivity.

freekmurze avatar Oct 01 '22 10:10 freekmurze