• laravel前后台路由分离


    在laravel中创建文件放置前台和后台控制器

    找到app/providers/RouteServiceProvider.PHP文件

    在内配置

    例:

    <?php  
      
    namespace AppProviders;  
      
    use IlluminateRoutingRouter;  
    use IlluminateFoundationSupportProvidersRouteServiceProvider 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 = 'AppHttpControllers';  
        protected $frontendNamespace;  
        /**  
         * Define your route model bindings, pattern filters, etc.  
         *  
         * @param  IlluminateRoutingRouter  $router  
         * @return void  
         */  
        public function boot(Router $router)  
        {  
            //  
            $this->frontnamespace = 'AppHttpControllersFront';  
            parent::boot($router);  
        }  
        /**  
         * Define the routes for the application.  
         *  
         * @param  IlluminateRoutingRouter  $router  
         * @return void  
         */  
        public function map(Router $router)  
        {  
            //配置路由所在文件  
            // $backendUrl = config('route.backend_url');  
            // $frontendUrl = config('route.frontend_url');  
            // $apiUrl = config('route.api_url');  
                    //  
                    $router->group(['namespace' => $this->namespace], function ($router) {  
                        require app_path('Http/routes.php');  
                    });  
                    //前台  
                    $router->group(['namespace' => $this->frontnamespace], function ($router)   
                    {  
                        // 'domain' => $backendUrl,  
                        require app_path('Http/routes_front.php');  
                    });  
        }  
    }  

    <?php  
      
    namespace AppProviders;  
      
    use IlluminateRoutingRouter;  
    use IlluminateFoundationSupportProvidersRouteServiceProvider 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 = 'AppHttpControllers';  
        protected $backendNamespace;  
        protected $frontendNamespace;  
        protected $apiNamespace;  
        protected $currentDomain;  
      
        /**  
         * Define your route model bindings, pattern filters, etc.  
         *  
         * @param  IlluminateRoutingRouter $router  
         * @return void  
         */  
        public function boot(Router $router)  
        {  
            //  
            $this->backendNamespace = 'AppHttpControllersBackend';  
            $this->frontendNamespace = 'AppHttpControllersFrontend';  
            $this->apiNamespace = 'AppHttpControllersAPI';  
    //        $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  IlluminateRoutingRouter $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' => 'UserPasswordController@getReset'  
        ]);  
    ]);  

    转自woshihaiyong168的博客

  • 相关阅读:
    dajngo控制台添加数据报错Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured.
    windows cmd编辑文本
    Centos pip 安装uwsgi 报错“fatal error: Python.h: No such file or directory”
    linux 配置启动supervisor详细
    安装supervisor 失败 :/usr/bin/python: bad interpreter: No such file
    yum 安装,可以list,但是无法安装Error downloading packages: 。。。。 No such file or directory
    linux 所有命令无法使用
    mysql库、表、记录的基本操作
    2数据库的三大范式
    1初识数据库mysql
  • 原文地址:https://www.cnblogs.com/zmdComeOn/p/10250898.html
Copyright © 2020-2023  润新知