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?
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');
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());
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.
© 2022 - 2024 — McMap. All rights reserved.
\URL::forceScheme('https')
which seem to ignore pagination. Thanks. – Costive