Instantly share code, notes, and snippets.

Embed
What would you like to do?
Laravel middleware for working with pjax.
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Symfony\Component\DomCrawler\Crawler;
class PjaxMiddleware
{
/**
* Handle an incoming request.
*
* @param Request $request
* @param Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$response = $next($request);
if (! $request->pjax() || $response->isRedirection()) {
return $response;
}
$this->filterResponse($response, $request->header('X-PJAX-CONTAINER'))
->setUriHeader($response, $request);
return $response;
}
/**
* Prepare the PJAX-specific response content.
*
* @param Response $response
* @param string $container
* @return $this
*/
protected function filterResponse(Response $response, $container)
{
$crawler = new Crawler($response->getContent());
$response->setContent(
$this->makeTitle($crawler) .
$this->fetchContents($crawler, $container)
);
return $this;
}
/**
* Prepare an HTML title tag.
*
* @param Crawler $crawler
* @return string
*/
protected function makeTitle($crawler)
{
$pageTitle = $crawler->filter('head > title')->html();
return "<title>{$pageTitle}</title>";
}
/**
* Fetch the PJAX-specific HTML from the response.
*
* @param Crawler $crawler
* @param string $container
* @return string
*/
protected function fetchContents($crawler, $container)
{
$content = $crawler->filter($container);
if (! $content->count()) {
abort(422);
}
return $content->html();
}
/**
* Set the PJAX-URL header to the current uri.
*
* @param Response $response
* @param Request $request
*/
protected function setUriHeader(Response $response, Request $request)
{
$response->header(
'X-PJAX-URL', $request->getRequestUri()
);
}
}
@ijunaid8989

This comment has been minimized.

Show comment
Hide comment
@ijunaid8989

ijunaid8989 Oct 10, 2015

You are awesome Sir. Thanks a lot

ijunaid8989 commented Oct 10, 2015

You are awesome Sir. Thanks a lot

@mstaack

This comment has been minimized.

Show comment
Hide comment
@mstaack

mstaack commented Oct 10, 2015

Great!

@fabienmwamba

This comment has been minimized.

Show comment
Hide comment
@fabienmwamba

fabienmwamba Oct 12, 2015

Just wonderful

fabienmwamba commented Oct 12, 2015

Just wonderful

@iolson

This comment has been minimized.

Show comment
Hide comment
@iolson

iolson Oct 12, 2015

Thank you for this!

iolson commented Oct 12, 2015

Thank you for this!

@helmerdavila

This comment has been minimized.

Show comment
Hide comment
@helmerdavila

helmerdavila Oct 13, 2015

print $very_nice;

helmerdavila commented Oct 13, 2015

print $very_nice;
@JesseLeite

This comment has been minimized.

Show comment
Hide comment
@JesseLeite

JesseLeite Oct 14, 2015

@JeffreyWay Why not package this up?

JesseLeite commented Oct 14, 2015

@JeffreyWay Why not package this up?

@pascalbaljet

This comment has been minimized.

Show comment
Hide comment
@davidthingsaker

This comment has been minimized.

Show comment
Hide comment
@davidthingsaker

davidthingsaker Oct 22, 2015

If anyone needs this for Lumen, I have modified @JeffreyWay's version above here just needed tweaking as I found Lumen doesnt have symfony's CssSelector Component.

davidthingsaker commented Oct 22, 2015

If anyone needs this for Lumen, I have modified @JeffreyWay's version above here just needed tweaking as I found Lumen doesnt have symfony's CssSelector Component.

@ghost

This comment has been minimized.

Show comment
Hide comment
@ghost

ghost Dec 11, 2015

Thanks for this code. Greetings From Chile.

ghost commented Dec 11, 2015

Thanks for this code. Greetings From Chile.

@JacobBennett

This comment has been minimized.

Show comment
Hide comment
@devharandi

This comment has been minimized.

Show comment
Hide comment
@devharandi

devharandi May 21, 2016

Thanks for this code @JeffreyWay

devharandi commented May 21, 2016

Thanks for this code @JeffreyWay

@andervilo02

This comment has been minimized.

Show comment
Hide comment
@andervilo02

andervilo02 Mar 31, 2017

To use this, I only have to register on kernel.php and use like other middleware?

I'm face problems using pjax. Google Chrome Console Log only return: 500 Internal Server Error

I register this middleware in kernel and include the pjax.js asset, but not works.

This is my request and response header: http://prntscr.com/er0cu0

You know to solve this problem?

andervilo02 commented Mar 31, 2017

To use this, I only have to register on kernel.php and use like other middleware?

I'm face problems using pjax. Google Chrome Console Log only return: 500 Internal Server Error

I register this middleware in kernel and include the pjax.js asset, but not works.

This is my request and response header: http://prntscr.com/er0cu0

You know to solve this problem?

@ngrcode

This comment has been minimized.

Show comment
Hide comment
@ngrcode

ngrcode Sep 1, 2018

hi I have problem with this middleware and this is my error after adding this file
The current node list is empty.

ngrcode commented Sep 1, 2018

hi I have problem with this middleware and this is my error after adding this file
The current node list is empty.

@ngrcode

This comment has been minimized.

Show comment
Hide comment
@ngrcode

ngrcode Sep 4, 2018

and this is my middleware
#<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Symfony\Component\DomCrawler\Crawler;
class PjaxMiddleware
{
/**
* Handle an incoming request.
*
* @param Request $request
* @param Closure $next
* @return mixed
/
public function handle($request, Closure $next)
{
$response = $next($request);
if (! $request->pjax() || $response->isRedirection()) {
return $response;
}
$this->filterResponse($response, $request->header('X-PJAX-CONTAINER'))
->setUriHeader($response, $request);
return $response;
}
/
*
* Prepare the PJAX-specific response content.
*
* @param Response $response
* @param string $container
* @return $this
/
protected function filterResponse(Response $response, $container)
{
$crawler = new Crawler($response->getContent());
$response->setContent(
$this->makeTitle($crawler) .
$this->fetchContents($crawler, $container)
);
return $this;
}
/
*
* Prepare an HTML title tag.
*
* @param Crawler $crawler
* @return string
/
protected function makeTitle($crawler)
{
$pageTitle = $crawler->filter('head > title')->html();
return "<title>{$pageTitle}</title>";
}
/
*
* Fetch the PJAX-specific HTML from the response.
*
* @param Crawler $crawler
* @param string $container
* @return string
/
protected function fetchContents($crawler, $container)
{
$content = $crawler->filter($container);
if (! $content->count()) {
abort(422);
}
return $content->html();
}
/
*
* Set the PJAX-URL header to the current uri.
*
* @param Response $response
* @param Request $request
*/
protected function setUriHeader(Response $response, Request $request)
{
$response->header(
'X-PJAX-URL', $request->getRequestUri()
);
}
}

ngrcode commented Sep 4, 2018

and this is my middleware
#<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Symfony\Component\DomCrawler\Crawler;
class PjaxMiddleware
{
/**
* Handle an incoming request.
*
* @param Request $request
* @param Closure $next
* @return mixed
/
public function handle($request, Closure $next)
{
$response = $next($request);
if (! $request->pjax() || $response->isRedirection()) {
return $response;
}
$this->filterResponse($response, $request->header('X-PJAX-CONTAINER'))
->setUriHeader($response, $request);
return $response;
}
/
*
* Prepare the PJAX-specific response content.
*
* @param Response $response
* @param string $container
* @return $this
/
protected function filterResponse(Response $response, $container)
{
$crawler = new Crawler($response->getContent());
$response->setContent(
$this->makeTitle($crawler) .
$this->fetchContents($crawler, $container)
);
return $this;
}
/
*
* Prepare an HTML title tag.
*
* @param Crawler $crawler
* @return string
/
protected function makeTitle($crawler)
{
$pageTitle = $crawler->filter('head > title')->html();
return "<title>{$pageTitle}</title>";
}
/
*
* Fetch the PJAX-specific HTML from the response.
*
* @param Crawler $crawler
* @param string $container
* @return string
/
protected function fetchContents($crawler, $container)
{
$content = $crawler->filter($container);
if (! $content->count()) {
abort(422);
}
return $content->html();
}
/
*
* Set the PJAX-URL header to the current uri.
*
* @param Response $response
* @param Request $request
*/
protected function setUriHeader(Response $response, Request $request)
{
$response->header(
'X-PJAX-URL', $request->getRequestUri()
);
}
}

@ngrcode

This comment has been minimized.

Show comment
Hide comment
@ngrcode

ngrcode Sep 4, 2018

which version of crawler are u using?

ngrcode commented Sep 4, 2018

which version of crawler are u using?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment