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

前端 未结 9 1107
广开言路
广开言路 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:03

    You can use Request::is() so your main routes.php file will look like this:

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

    You can read more here.

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

    You can load custom route files within a Service Provider. AsgardCMS is doing it the same way, see this method in the Core RoutingServiceProvider that loads the backend routes:

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

    The Laravel docs provide a simple example in the package development section:

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

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

    If you just want to create a custom route file you can easily add your custom route file and register by using the web middleware name. The code is pretty simple like this.

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

    Note: This method also works even the file is inside the folder just trace where your route file located.

    Happy Coding.

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

    My simple and fast solution for multiple route files. It works!

    In web.php file, add:

    foreach (glob(__DIR__. '/*') as $router_files){
        (basename($router_files =='web.php')) ? : (require_once $router_files);
    }
    
    0 讨论(0)
  • 2020-12-15 13:12

    in case someone still after that

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

    1- open routes/web/api.php and add

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

    2- now create a new folder at the same level and call it ‘Routes‘

    3- create files according to each route ex.user, post, stuff, etc… and add ur route logic as normal

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

    The group() method on Laravel's Route, can accept filename, so we can something like this:

    // web.php
    
    Route::prefix('admin')
        ->group(base_path('routes/admin.php'));
    
    // admin.php
    Route::get('/', 'AdminController@index');
    
    0 讨论(0)
提交回复
热议问题