创建多个文件以区分laravel 5.4中的路由

时间:2017-07-27 11:07:26

标签: laravel-5.4

这是我为创建多个路径文件而更改的RouteServiceProvider

namespace App\Providers;

use Illuminate\Routing\Router;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;

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

    /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @return void
     */
    public function boot(Router $router) {
        //

        parent::boot($router);
    }

    /**
     * Define the routes for the application.
     *
     * @return void
     */
    public function map(Router $router) {
        $this->mapApiRoutes($router);

        $this->mapWebRoutes($router);

        //
    }

    /**
     * Define the "web" routes for the application.
     *
     * These routes all receive session state, CSRF protection, etc.
     *
     * @return void
     */
    protected function mapWebRoutes($router) {
        $router->group(['namespace' => $this->namespace, 'middleware' => 'web'], function ($router) {
            foreach (glob(app_path('Http/Routes/Web/*.php')) as $eachRoute) {
                require $eachRoute;
            }
        });
    }

    /**
     * Define the "api" routes for the application.
     *
     * These routes are typically stateless.
     *
     * @return void
     */
    protected function mapApiRoutes($router) {
        $router->group(['prefix' => 'api', 'namespace' => $this->namespace, 'middleware' => 'api'], function ($router) {
            foreach (glob(app_path('Http/Routes/Api/*.php')) as $eachRoute) {
                require $eachRoute;
            }
        });
    }
}

1 个答案:

答案 0 :(得分:1)

打开您的RouteServiceProvider

    use Illuminate\Routing\Router; statement top of the file.

    /**
     * Define the routes for the application.
     *
     * @return void
     */
    public function map(Router $router) {
        $this->mapApiRoutes();

        $this->mapWebRoutes($router);

        //used Router object above in map function
    }  

这仅适用于网络路由,您也可以创建api,需要创建目录来区分它。

最后:

protected function mapWebRoutes($router) {

    $router->group(['namespace' => $this->namespace], function ($router) {

        foreach (glob(base_path('routes/web/*.php')) as $eachRoute) {
            require $eachRoute;
        }
    });
}