plugin icon indicating copy to clipboard operation
plugin copied to clipboard

Route names completion doesn't list all routes

Open nuernbergerA opened this issue 5 years ago • 7 comments

I have the following structure: routes/web.php

foreach (File::allFiles(__DIR__.'/web') as $file) {
    require $file->getPathname();
}

And some routing files separated per module:

routes/web/moduleA.php
routes/web/moduleB.php
...

Some module files have multiple Route::group() defined.

Before I open those files I don't have route names completion, after I opened them once it shows me the routes.

Somehow this wasn't an issue this plugin Are there any known issues if I run both plugins together?

nuernbergerA avatar Sep 09 '20 16:09 nuernbergerA

Are there any known issues if I run both plugins together?

There are no compatibility issues with the old plugin.

Laravel Idea tries to find the information about each route file. For example if RouteServiceProvider contains:

        Route::prefix('api')
            ->as('api.')
            ->middleware('api')
            ->namespace($this->namespace)
            ->group(base_path('routes/api.php'));

Plugin will know that route names in the api.php file has "api." prefix. However, it doesn't work for dynamic

foreach (File::allFiles(__DIR__.'/web') as $file) {
    require $file->getPathname();
}

So, for this case, the simple way as used in the old Laravel plugin(just use empty prefix) is better :) There is a workaround by adding phpDocs to the beginning of the route files:

/**
 * @routePrefix("api.")
 * @routeNamespace("App\Http\Controllers")
 */

But it's a bit ugly. I'll try to find a better solution. At least I can add an option: "smart" or "simple" route files processing.

adelf avatar Sep 09 '20 17:09 adelf

Hey, Adrian. Why? I was going to support this kind of route registration by description in the ide.json file

adelf avatar Jun 20 '21 10:06 adelf

@adelf was going through my open issues and found this one. I don't face that issue anymore. So I thought I can close it😉

nuernbergerA avatar Jun 20 '21 15:06 nuernbergerA

Ok. This issue is mine now! )

adelf avatar Jun 20 '21 16:06 adelf

FYI I still need to add the comment to support this on my code. @adelf

/**
 * PhpStorm Laravel-idea plugin helper comment
 * @routePrefix("admin")
 * @routeNamespace("App\Http\Controllers")
 */
Route::prefix('admin')
    ->namespace('Admin')
    ->middleware(['auth', 'enabled', 'admin'])
    ->group(function () {
        Route::match(['get', 'post'], 'profile', 'HomeController@profile')
            ->name('admin-profile');
    });

delmicio avatar Oct 13 '22 18:10 delmicio

@delmicio how do you register the admin.php file in the RouteServiceProvider?

adelf avatar Oct 17 '22 15:10 adelf

RouteServiceProvider

It is not the default Laravel 5.5 setup. We need a custom logic to better organize routes.

    public function map()
    {
        $this->mapWebRoutes();
    }

    /**
     * Define the "web" routes for the application.
     *
     * These routes all receive session state, CSRF protection, etc.
     *
     * @return void
     */
    protected function mapWebRoutes()
    {
        Route::middleware('web')
            ->namespace($this->namespace)
            ->group(function () {
                $this->requireRoutes('routes/web');
            });
    }
    /**
     * @param $path
     *
     * @return Collection
     */
    public function requireRoutes($path)
    {
        return collect(
            Finder::create()
            ->in(base_path($path))
            ->name('*.php')
        )->each(function ($file) {
            /** @var SplFileInfo $file */
            include_once $file->getRealPath();
        });
    }

image

delmicio avatar Oct 17 '22 16:10 delmicio