Laravel Controller Subfolder routing

后端 未结 14 1530
一生所求
一生所求 2020-11-29 16:44

I\'m new to Laravel. To try and keep my app organized I would like to put my controllers into subfolders of the controller folder.

controllers\\
---- folder1         


        
14条回答
  •  春和景丽
    2020-11-29 17:17

    1) That is how you can make your app organized:

    Every route file (web.php, api.php ...) is declared in a map() method, in a file

    \app\Providers\RouteServiceProvider.php
    

    When you mapping a route file you can set a ->namespace($this->namespace) for it, you will see it there among examples.

    It means that you can create more files to make your project more structured!

    And set different namespaces for each of them.

    But I prefer set empty string for the namespace ""

    2) You can set your controllers to rout in a native php way, see the example:

    Route::resource('/users', UserController::class);
    Route::get('/agents', [AgentController::class, 'list'])->name('agents.list');
    

    Now you can double click your controller names in your IDE to get there quickly and conveniently.

提交回复
热议问题