本篇文章为大家展示了如何在laravel项目中实现前后台路由分离,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
实现步骤:
1、首先在app/Https/Controlles/文件下建立 Frontend(前端) Backend(后端) API(接口) 文件
2、在app/Https/建立对应的路由文件
3、打开app/Providers/RouteServiceProvider.php 定义各个功能对应的路由文件
代码如下:
<?php namespace App\Providers; use Illuminate\Routing\Router; use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider; 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 $namespace = 'App\Http\Controllers'; protected $backendNamespace; protected $frontendNamespace; protected $apiNamespace; protected $currentDomain; /** * Define your route model bindings, pattern filters, etc. * * @param \Illuminate\Routing\Router $router * @return void */ public function boot(Router $router) { // $this->backendNamespace = 'App\Http\Controllers\Backend'; $this->frontendNamespace = 'App\Http\Controllers\Frontend'; $this->apiNamespace = 'App\Http\Controllers\API'; // $this->currentDomain = $this->app->request->server->get('HTTP_HOST'); $this->currentDomain = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : ""; parent::boot($router); } /** * Define the routes for the application. * * @param \Illuminate\Routing\Router $router * @return void */ public function map(Router $router) { // $router->group(['namespace' => $this->namespace], function ($router) { // require app_path('Http/routes.php'); // }); $backendUrl = config('route.backend_url'); $frontendUrl = config('route.frontend_url'); $apiUrl = config('route.api_url'); switch ($this->currentDomain) { case $apiUrl: // API路由 $router->group([ 'domain' => $apiUrl, 'namespace' => $this->apiNamespace], function ($router) { require app_path('Http/routes-api.php'); } ); break; case $backendUrl: // 后端路由 $router->group([ 'domain' => $backendUrl, 'namespace' => $this->backendNamespace], function ($router) { require app_path('Http/routes-backend.php'); } ); break; default: // 前端路由 $router->group([ 'domain' => $frontendUrl, 'namespace' => $this->frontendNamespace], function ($router) { require app_path('Http/routes-frontend.php'); } ); break; } } }
上述内容就是如何在laravel项目中实现前后台路由分离,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。