2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Laravelでサブドメイン対応(さくらレンタルサーバ)

Last updated at Posted at 2020-09-13

やりたいこと

main.hoge.hoge → routes/main.phpでルーティング
api.hoge.hoge → routes/api.phpでルーティング
test.hoge.hoge → routes/test.phpでルーティング

##①テスト環境と本番環境でドメインを分けるためにconfig設定

configフォルダ内にmyapp.php作成
\Config::get('myapp.domain.main')でアクセスできる

config/myapp.php
<?php
return [
    'domain' => [
        'main' => 'main.hoge.hoge',
        'api' => 'api.hoge.hoge',
        'test' => 'test.hoge.hoge',
    ]
];

コンフィグファイルのキャッシュクリアをしないと反映されない。

$ php artisan config:cache

##②ルーティング設定

/app/Providers/RouteServiceProvider.php

class RouteServiceProvider extends ServiceProvider
{

    public function map()
    {
        $this->mapApiRoutes();

        //$this->mapWebRoutes();
        $this->mapMainRoutes();
        $this->mapTestRoutes();
    }

    protected function mapMainRoutes()
    {
        Route::domain(\Config::get('myapp.domain.main'))
             ->middleware('web')
             ->namespace($this->namespace)
             ->group(base_path('routes/main.php'));
    }
    protected function mapTestRoutes()
    {
        Route::domain(\Config::get('myapp.domain.test'))
             ->middleware('web')
             ->namespace($this->namespace)
             ->group(base_path('routes/test.php'));
    }
    protected function mapApiRoutes()
    {
        Route::domain(\Config::get('api.domain.test'))
             ->middleware('api')
             ->namespace($this->namespace)
             ->group(base_path('routes/api.php'));
    }

routes/main.php,routes/test.phpを作ってルーティングさせる。

おわり。

#CROS対応
サブドメインで分けるとaxiosからの通信がクロスドメインで引っかかるので設定
※バージョン指定しないとエラーになります。

$ composer require barryvdh/laravel-cors:^0.11

続きは下記参照
Laravelでクロスオリジン(CORS)に対応する為のメモです

※crosの設定を上に持ってこないと一部ヘッダが付与されない場合がある?

    protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            // \Illuminate\Session\Middleware\AuthenticateSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],

        'api' => [
            'cros',
            'throttle:60,1',
            'bindings',
            
        ],
    ];

    /**
     * The application's route middleware.
     *
     * These middleware may be assigned to groups or used individually.
     *
     * @var array
     */
    protected $routeMiddleware = [
        'cros' => \Barryvdh\Cors\HandleCors::class,
        'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
        'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
        'can' => \Illuminate\Auth\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
    ];

#セッション共有
.envにSESSION_DOMAIN設定すればサブドメイン間でセッション共有可能

'''.enb
SESSION_DRIVER=file
SESSION_LIFETIME=120
SESSION_DOMAIN='.hoge.hoge'
'''

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?