登录  /  注册
Laravel框架如何把应用分为前台和后台?用路由组吗?
Ailon
Ailon 2017-08-26 15:02:07
[PHP讨论组]
Ailon
Ailon

全部回复(1)
PHP中文网

找到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;
        }
    }
}

完成后我们的路由也可以新建了  但要和上面的名称要一样

在路由中可以这样写(当然也可以自定义路由)例:

Route::group(['middleware' => ['web']], function () {   
    Route::controller('/test', 'TestController');  
    // 重置  
    Route::get('user/password/reset/{token?}', [
        'as' => 'user.password.reset@token', 
        'uses' => 'User\PasswordController@getReset'  
    ]);  
]);


热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号