loghman
loghman

Reputation: 175

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

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 required codes for routing be placed in Modules codes. For example required code for routing menu manager module be placed in Modules/Media/apiRoutes.php and Modules/Media/backendRoutes.php files. May help me and tell me how I can manage my routes like that?

Upvotes: 14

Views: 50522

Answers (9)

Mortada Jafar
Mortada Jafar

Reputation: 3679

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 directory of your controllers  
            ->group(base_path('routes/custom.php')); // change custom.php to your custom routers file name
    }
}

Upvotes: 3

Jones Pereira
Jones Pereira

Reputation: 21

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

Upvotes: 2

Benjamin Beganović
Benjamin Beganović

Reputation: 1158

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

Upvotes: 16

ctf0
ctf0

Reputation: 7579

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

Upvotes: 4

Mahmoud Zalt
Mahmoud Zalt

Reputation: 31130

  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...

Upvotes: 13

Jayson
Jayson

Reputation: 21

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.

Upvotes: 2

Alex
Alex

Reputation: 4774

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.

Upvotes: 8

Silwerclaw
Silwerclaw

Reputation: 695

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

Upvotes: 10

tommy
tommy

Reputation: 3615

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

Upvotes: 3

Related Questions