PHP: Generate Laravel Paginator Secure (HTTPS) Links
Asked Answered
P

3

7

I'm developing an app using Laravel 4.2 over HTTPS with secure routes and redirects. I'm using Paginator to paginate results, but the links rendered in the view points to the http pages, how can we force Paginator to generate https links?

Pecten answered 1/3, 2015 at 18:50 Comment(0)
P
30

I had this issue today and found this global solution.

In your AppServiceProvider::boot method you can add the following to force https on pagination links

$this->app['request']->server->set('HTTPS','on');
Peasant answered 20/11, 2015 at 16:38 Comment(1)
This also works for any other link, so I was able exchange it for \URL::forceScheme('https') which seem to ignore pagination. Thanks.Costive
V
4

If your current page is served over HTTPS, then the pagination URLs generated should use that schema.

However if you're using a proxy that does not pass the correct headers, the Request class responsible for determining if the connection is secure, might not report it as such. To determine if the request is detected as secure use Request::secure(). If that returns false, try using Laravel Trusted Proxies.

If that does not work you can force the pagination URLs with setBaseUrl as follows:

$results->paginate();
$results->setBaseUrl('https://' . Request::getHttpHost() . '/' . Request::path());
Voltameter answered 1/3, 2015 at 19:56 Comment(1)
For Laravel 5.1, setPath can be used instead of setBaseUrlRingmaster
F
0

Add a custom presenter ZurbPresenter.php in app/helpers/ (you can place it inside other directory provided its path is included in to ClassLoader::addDirectories()):

<?php
class ZurbPresenter extends Illuminate\Pagination\Presenter {

    /**
     * Get HTML wrapper for a page link.
     *
     * @param  string  $url
     * @param  int  $page
     * @param  string  $rel
     * @return string
     */
    public function getPageLinkWrapper($url, $page, $rel = null)
    {
        $rel = is_null($rel) ? '' : ' rel="'.$rel.'"';

        if (strpos($url, "http://") === 0) {
            $url = "https://" . ltrim($url, "http://");
        }

        return '<li><a href="'.$url.'"'.$rel.'>'.$page.'</a></li>';
    }

    /**
     * Get HTML wrapper for disabled text.
     *
     * @param  string  $text
     * @return string
     */
    public function getDisabledTextWrapper($text)
    {
        return '<li class="disabled"><span>'.$text.'</span></li>';
    }

    /**
     * Get HTML wrapper for active text.
     *
     * @param  string  $text
     * @return string
     */
    public function getActivePageWrapper($text)
    {
        return '<li class="active"><span>'.$text.'</span></li>';
    }

}

Notice the getPageLinkWrapper() has a logic to replace http by https.

Create a view file to use the presenter. Inside app/views create a file zurb_pagination.php with following content:

<?php
    $presenter = new ZurbPresenter($paginator);

    $trans = $environment->getTranslator();
?>

<?php if ($paginator->getLastPage() > 1): ?>
    <ul class="pager">
        <?php
            echo $presenter->getPrevious($trans->trans('pagination.previous'));

            echo $presenter->getNext($trans->trans('pagination.next'));
        ?>
    </ul>
<?php endif; ?>

Finally change your app config to use the new presenter in app\config/view.php for pagination:

'pagination' => '_zurb_pagination_simple',

I use a similar approach for my website and you can verify it's working here.

Footwear answered 3/9, 2018 at 9:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.