Laravel 5.4前楽屋分離、異なる二級ドメイン名で訪問する.

8499 ワード

第一歩:app\http\Controllersフォルダを追加して、フロントエンドとバックエンドまたはインターフェースを保存するフォルダを作成します.
Home(  )  Admin(  )   App(  )     
这里写图片描述
ステップ2:app\http\providers\RouteServiceProvider.phpを修正します.
/**
     * 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';
    protected $homeNamespace = 'App\Http\Controllers\Home';//PC 
    protected $adminNamespace = 'App\Http\Controllers\Admin';//    

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

        parent::boot();
    }

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

        //$this->mapWebRoutes();
        $sld_prefix = explode('.',$_SERVER['HTTP_HOST'])[0];
        if(config('route.admin_url') == $sld_prefix){
            $this->mapAdminRoutes();
        }elseif(config('route.home_url') == $sld_prefix){
            $this->mapHomeRoutes();
        }elseif(config('route.api_url') == $sld_prefix){
            $this->mapApiRoutes();
        }
    }

    /**
     * Define the "web" routes for the application.
     *
     * These routes all receive session state, CSRF protection, etc.
     *
     * @return void
     */
    protected function mapWebRoutes()
    {
        Route::middleware('web')
             ->namespace($this->namespace)
             ->group(base_path('routes/web.php'));
    }

    /**
     * Define the "api" routes for the application.
     *
     * These routes are typically stateless.
     *
     * @return void
     */
    protected function mapApiRoutes()
    {
        Route::prefix('api')
             ->middleware('api')
             ->namespace($this->namespace)
             ->group(base_path('routes/api.php'));
    }

    /**
     *     
     */
    protected function mapAdminRoutes()
    {
        Route::middleware('web')
            ->namespace($this->adminNamespace)
            ->group(base_path('routes/admin.php'));
    }

    /**
     * PC 
     */
    protected function mapHomeRoutes()
    {
        Route::middleware('web')
            ->namespace($this->homeNamespace)
            ->group(base_path('routes/home.php'));
    }
}
ステップ3:routesディレクトリの下でadmin.phpとhome.phpルーティング这里写图片描述を作成する.
ステップ4:それぞれap\Http\Controller\Adminとap\Http\Controller\Home

namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;

class AdminController extends Controller
{
    public function index()
    {
        echo "this is admin";
    }
}



namespace App\Http\Controllers\Home;
use App\Http\Controllers\Controller;

class HomeController extends Controller
{
    public function index()
    {
        echo "this is home";
    }
}
第五ステップ:それぞれadmin.phpとhome.phpでルートを新規作成する.
Route::get('/', 'AdminController@index');
Route::get('/','HomeController@index');
第六ステップ:試験这里写图片描述
这里写图片描述
第七ステップ:運行エラー1:lavel Class‘App’Http/Controller’not found
エラーの二:Class App\Http\Controlles\IndexController does not exist
解決方法:
 PHPstorm Terminal     “composer dump-autoload”

  laravel  composer    ,           autoload。
PHPstormエディタを使用していない場合は、ローカルにcompserをインストールし、cmdを管理者として実行し、プロジェクトのルートディレクトリに入り、「compser dump-atoroad」を実行します.