multiple route file instead of one main route file in laravel 5

前端 未结 9 1108
广开言路
广开言路 2020-12-15 12:58

I am a novice in web developing with Laravel 5. I installed asGgardCMS and After seeing asgardCms codes, I found that there is nothing codes in app/Http/route.php file and r

相关标签:
9条回答
  • 2020-12-15 13:21
    1. create 2 route files routes.web.php and routes.api.php.

    2. edit the RouteServiceProvider.php file to look like the example below:


    <?php
    
    namespace App\Providers;
    
    use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
    use Illuminate\Routing\Router;
    
    class RouteServiceProvider extends ServiceProvider
    {
    
        /**
         * This namespace is applied to the controller routes in your routes file.
         *
         * In addition, it is set as the URL generator's root namespace.
         *
         * @var string
         */
        protected $webNamespace = 'App\Http\Controllers\Web';
    
        protected $apiNamespace = 'App\Http\Controllers\Api';
    
        /**
         * Define your route model bindings, pattern filters, etc.
         *
         * @param  \Illuminate\Routing\Router $router
         *
         * @return void
         */
        public function boot(Router $router)
        {
            //
    
            parent::boot($router);
        }
    
        /**
         * Define the routes for the application.
         *
         * @param  \Illuminate\Routing\Router $router
         *
         * @return void
         */
        public function map(Router $router)
        {
    
            /*
            |--------------------------------------------------------------------------
            | Web Router 
            |--------------------------------------------------------------------------
            */
    
            $router->group(['namespace' => $this->webNamespace], function ($router) {
                require app_path('Http/routes.web.php');
            });
    
            /*
            |--------------------------------------------------------------------------
            | Api Router 
            |--------------------------------------------------------------------------
            */
    
            $router->group(['namespace' => $this->apiNamespace], function ($router) {
                require app_path('Http/routes.api.php');
            });
    
        }
    }
    

    Note: you can add as many route files as you want...

    0 讨论(0)
  • 2020-12-15 13:27

    laravel ^6

    explained in comments

    <?php
    
    namespace App\Providers;
    
    use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
    use Illuminate\Support\Facades\Route;
    
    class RouteServiceProvider extends ServiceProvider
    {
        /**
         * This namespace is applied to your controller routes.
         *
         * In addition, it is set as the URL generator's root namespace.
         *
         * @var string
         */
        protected $namespace = 'App\Http\Controllers';
    
        /**
         * This namespace is applied to your Custom controller routes.
         *
         * In addition, it is set as the URL generator's root namespace.
         *
         * @var string
         */
        protected $custom_namespace = 'App\Http\Controllers\Custom';
    
        /**
         * The path to the "home" route for your application.
         *
         * @var string
         */
        public const HOME = '/home';
    
        /**
         * Define your route model bindings, pattern filters, etc.
         *
         * @return void
         */
        public function boot()
        {
            //
    
            parent::boot();
        }
    
        /**
         * Define the routes for the application.
         *
         * @return void
         */
        public function map()
        {
            $this->mapApiRoutes();
    
            $this->mapWebRoutes();
    
        // map new custom routes
            $this->mapCustomRoutes();
        }
    
        /**
         * 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(base_path('routes/web.php'));
        }
    
        /**
         * Define the "api" routes for the application.
         *
         * These routes are typically stateless.
         *
         * @return void
         */
        protected function mapApiRoutes()
        {
            Route::prefix('api')
                ->middleware('api')
                ->namespace($this->namespace)
                ->group(base_path('routes/api.php'));
        }
    
       /**
         * Define the "custom" routes for the application.
         *
         * These routes are typically stateless.
         *
         * @return void
         */
        protected function mapCustomRoutes()
        {
               Route::middleware('web')
                ->namespace($this->custom_namespace)  //or change its custom_namespace to namespace if you don't need change diractory of your controllers  
                ->group(base_path('routes/custom.php')); // change custom.php to your custom routers file name
        }
    }
    
    0 讨论(0)
  • 2020-12-15 13:29

    You can create as many route files as you need anywhere and then just require them in the main route file smth like:

    Route::get('/', function () {
        return 'Hello World';
    });
    
    Route::post('foo/bar', function () {
        return 'Hello World';
    });
    
    require_once "../../myModule1/routes.php";
    require_once "../../myModule2/routes.php"
    require_once "some_other_folder/routes.php"
    

    where you will define routes in the same way as in main

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