【问题标题】:multiple route file instead of one main route file in laravel 5laravel 5中的多个路由文件而不是一个主路由文件
【发布时间】:2016-03-14 23:05:18
【问题描述】:

我是一个使用 Laravel 5 进行 Web 开发的新手。我安装了 asGgardCMS,在查看 asgardCms 代码后,我发现 app/Http/route.php 文件中没有任何代码,并且路由所需的代码放在 Modules 代码中。例如,路由菜单管理器模块所需的代码放在 Modules/Media/apiRoutes.php 和 Modules/Media/backendRoutes.php 文件中。可以帮助我并告诉我如何管理我的路线吗?

【问题讨论】:

  • @loghman 请张贴一个你想做的事情的具体场景以及你遇到的问题。

标签: php laravel


【解决方案1】:

您可以在服务提供者中加载自定义路由文件。 AsgardCMS 也是这样做的,在加载后端路由的 Core RoutingServiceProvider 中查看这个方法:

https://github.com/AsgardCms/Core/blob/master/Providers/RoutingServiceProvider.php#L77

Laravel 文档在包开发部分提供了一个简单的示例:

http://laravel.com/docs/5.1/packages#routing

【讨论】:

    【解决方案2】:

    您可以在任何地方创建任意数量的路由文件,然后只需在主路由文件中要求它们,例如:

    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"
    

    您将在其中以与 main 中相同的方式定义路由

    【讨论】:

    • 简单、快速、简单✔
    【解决方案3】:

    您可以使用Request::is(),这样您的主要routes.php 文件将如下所示:

    if(Request::is('frontend/*))
    {
        require __DIR__.'/frontend_routes.php;
    }
    
    if(Request::is('admin/*))
    {
        require __DIR__.'/admin_routes.php;
    }
    

    您可以阅读更多here

    【讨论】:

    • 我认为根据条件定义路由不是一个好主意。您将无法从仪表板生成公共前端资源网址(如帖子网址),反之亦然。
    【解决方案4】:
    1. 创建 2 个路由文件 routes.web.phproutes.api.php

    2. 编辑RouteServiceProvider.php 文件,如下例所示:


    <?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');
            });
    
        }
    }
    

    注意:您可以添加任意数量的路由文件...

    【讨论】:

      【解决方案5】:

      如果您只想创建自定义路由文件,您可以轻松添加自定义路由文件并使用 Web 中间件名称进行注册。像这样的代码很简单。

      编辑 App\Provider\RouteServiceProvider.php

         public function map()
         {
          /** Insert this Method Name **/
              $this->methodicalness();
         }
      
         protected function methodicalness()
         {
             Route::middleware('web')
             ->namespace($this->namespace)
             ->group(base_path('routes/yourRouteName.php'));
         }
      

      注意:即使文件在文件夹中,此方法也有效,只需跟踪您的路由文件所在的位置。

      快乐编码。

      【讨论】:

        【解决方案6】:

        以防有人在那之后

        https://ctf0.wordpress.com/2016/10/01/split-routes-into-categories-in-laravel/

        1- 打开 routes/web/api.php 并添加

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

        2- 现在在同一级别创建一个新文件夹并将其命名为“Routes”

        3- 根据每个路由 ex.user、post、stuff 等创建文件并正常添加您的路由逻辑

        【讨论】:

          【解决方案7】:

          Laravel Route 上的 group() 方法,可以接受文件名,所以我们可以这样:

          // web.php
          
          Route::prefix('admin')
              ->group(base_path('routes/admin.php'));
          
          // admin.php
          Route::get('/', 'AdminController@index');
          

          【讨论】:

            【解决方案8】:

            我对多个路由文件的简单快速的解决方案。有效!

            在 web.php 文件中,添加:

            foreach (glob(__DIR__. '/*') as $router_files){
                (basename($router_files =='web.php')) ? : (require_once $router_files);
            }
            

            【讨论】:

              【解决方案9】:

              laravel ^6

              在 cmets 中解释

              <?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
                  }
              }
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2015-05-14
                • 2015-04-05
                • 1970-01-01
                • 1970-01-01
                • 2015-08-15
                相关资源
                最近更新 更多