16
12

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 5 years have passed since last update.

Laravel pagenationリンクのhttps化

Last updated at Posted at 2020-04-15

Laravelでpagenationを使用したときの2ページ目以降のリンクがhttpからhttpsへ変更する方法について明記します

Laravelでページ内URLをhttps〜で生成するにはAppServiceProvider.phpの boot()を以下のように記せば良いですが、paginateメソッドを使用した場合は不十分です。

app/Providers/AppServiceProvider.php
    public function boot()
    {
        \URL::forceScheme('https');
    }

データを複数ページに分割して表示させる事ができるpaginateメソッドを利用したときに、このままだと1ページ目はhttpsになるが、2ページ目以降はhttpのままになり、エラーとなるケースがあります。

その場合には、
$this->app['request']->server->set('HTTPS','on');
をboot()の末尾に追記すればオッケーです。

app/Providers/AppServiceProvider.php
    public function boot()
    {
        \URL::forceScheme('https');
        $this->app['request']->server->set('HTTPS','on');
    }

私の知る限り、これが一番シンプルな気がします。
他にもっとシンプルな方法が会ったら教えてください。

参考ページ
PHP: Generate Laravel Paginator Secure(HTTPS) Links

16
12
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
16
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?