PHP: Generate Laravel Paginator Secure (HTTPS) Links

廉价感情. 提交于 2020-07-15 02:07:38

问题


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?


回答1:


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



回答2:


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());



回答3:


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.



来源:https://stackoverflow.com/questions/28797590/php-generate-laravel-paginator-secure-https-links

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!