>  Q&A  >  본문

Laravel 프레임워크는 어떻게 애플리케이션을 프론트엔드와 백엔드로 나누나요? 라우팅 그룹을 사용합니까?

AilonAilon2633일 전1442

모든 응답(1)나는 대답할 것이다

  • PHP中文网

    PHP中文网2017-08-26 16:25:35

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


    회신하다
    0
  • 취소회신하다