Laravel Controller Subfolder routing

后端 未结 14 1419
一生所求
一生所求 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.

    0 讨论(0)
  • 2020-11-29 17:19

    I had this problem recently with laravel 5.8 but i underestand I should define controller in a right way like this below:

    php artisan make:controller SubFolder\MyController  // true
    

    Not like this:

    php artisan make:controller SubFolder/MyController  // false
    

    Then you can access the controller in routes/web.php like this:

    Route::get('/my', 'SubFolder\MyController@index');
    
    0 讨论(0)
  • 2020-11-29 17:21
    php artisan make:controller admin/CategoryController
    

    Here admin is sub directory under app/Http/Controllers and CategoryController is controller you want to create inside directory

    0 讨论(0)
  • 2020-11-29 17:26

    In my case I had a prefix that had to be added for each route in the group, otherwise response would be that the UserController class was not found.

    Route::prefix('/user')->group(function() {
        Route::post('/login', [UserController::class, 'login'])->prefix('/user');
        Route::post('/register', [UserController::class, 'register'])->prefix('/user');
    });
    
    0 讨论(0)
  • 2020-11-29 17:29

    For those using Laravel 5 you need to set the namespace for the controller within the sub-directory (Laravel 5 is still in development and changes are happening daily)

    To get a folder structure like:

    Http
    ----Controllers
        ----Admin
                PostsController.php
        PostsController.php
    

    namespace Admin\PostsController.php file like so:

    <?php namespace App\Http\Controller\Admin;
    
    use App\Http\Controllers\Controller;
    
    class PostsController extends Controller {
    
        //business logic here
    }
    

    Then your route for this is:

    $router->get('/', 'Admin\PostsController@index');
    

    And lastly, don't for get to do either composer or artisan dump

    composer dump-autoload
    

    or

    php artisan dump
    
    0 讨论(0)
  • 2020-11-29 17:31

    For ** Laravel 5 or Laravel 5.1 LTS both **, if you have multiple Controllers in Admin folder, Route::group will be really helpful for you. For example:

    Update: Works with Laravel 5.4

    My folder Structure:

    Http
    ----Controllers
        ----Api
              ----V1
                     PostsApiController.php
                     CommentsApiController.php
        PostsController.php
    

    PostAPIController:

    <?php namespace App\Http\Controllers\Api\V1;
    
    use App\Http\Requests;
    use App\Http\Controllers\Controller;   
    use Illuminate\Http\Request;
    
    class PostApiController extends Controller {  
    ...
    

    In My Route.php, I set namespace group to Api\V1 and overall it looks like:

    Route::group(
            [           
                'namespace' => 'Api\V1',
                'prefix' => 'v1',
            ], function(){
    
                Route::get('posts', ['uses'=>'PostsApiController@index']);
                Route::get('posts/{id}', ['uses'=>'PostssAPIController@show']);
    
        });
    

    For move details to create sub-folder visit this link.

    0 讨论(0)
提交回复
热议问题