├── .gitignore ├── composer.json ├── licence ├── readme.md └── src └── JacobBennett └── Pjax └── PjaxMiddleware.php /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | composer.phar 3 | composer.lock 4 | .DS_Store 5 | .idea -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jacobbennett/pjax", 3 | "description": "PJAX for Laravel 5", 4 | "keywords": ["pjax", "laravel"], 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "email": "me@jakebennett.net", 9 | "name": "Jacob Bennett", 10 | "homepage": "http://jakebennett.net" 11 | }, 12 | { 13 | "name": "Simon Stamm", 14 | "email": "simon@stammtec.de" 15 | }, 16 | { 17 | "name": "Vincent Talbot", 18 | "email": "vtalbot@re-bot.co" 19 | } 20 | ], 21 | "require": { 22 | "php": ">=5.4.0", 23 | "symfony/dom-crawler": "3.0.*|3.1.*|4.*.*|5.*.*", 24 | "symfony/css-selector": "3.0.*|3.1.*|4.*.*|5.*.*" 25 | }, 26 | "autoload": { 27 | "psr-0": { 28 | "JacobBennett\\Pjax": "src/" 29 | } 30 | }, 31 | "minimum-stability": "dev" 32 | } 33 | -------------------------------------------------------------------------------- /licence: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Jacob Bennett 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # PJAX for Laravel 5.* 2 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/jacobbennett/pjax.svg?style=flat-square)](https://packagist.org/packages/jacobbennett/pjax) 3 | [![Total Downloads](https://img.shields.io/packagist/dt/jacobbennett/pjax.svg?style=flat-square)](https://packagist.org/packages/jacobbennett/pjax) 4 | 5 | Enable the use of PJAX in Laravel 5.*. 6 | 7 | #### Installation 8 | 9 | Add `jacobbennett/pjax` to `require` section in your `composer.json` 10 | 11 | "jacobbennett/pjax": "~1.0" 12 | 13 | Add `'JacobBennett\Pjax\PjaxMiddleware',` to `$middleware` in `app/Http/Kernel.php` 14 | 15 | #### How to use 16 | 17 | This middleware will check, before outputting the http response, for the `X-PJAX`'s 18 | header in the request. If found, it will crawl the response to return the requested 19 | element defined by `X-PJAX-Container`'s header. 20 | 21 | 22 | jQuery PJAX JS is required to use this package [jquery.pjax.js](https://github.com/defunkt/jquery-pjax). 23 | 24 | See an example on [Laracasts](https://laracasts.com/lessons/faster-page-loads-with-pjax) 25 | 26 | ### Notes: 27 | 28 | Sometimes when using PJAX it will timeout and trigger a standard page reload. This could be due to various factors but one thing you may try is to extend the default timeout for PJAX using this little snippet when you initialize PJAX. 29 | 30 | ```js 31 | 32 | $(document).ready(function(){ 33 | 34 | // does current browser support PJAX 35 | if ($.support.pjax) { 36 | $.pjax.defaults.timeout = 1000; // time in milliseconds 37 | } 38 | 39 | }); 40 | 41 | ``` 42 | -------------------------------------------------------------------------------- /src/JacobBennett/Pjax/PjaxMiddleware.php: -------------------------------------------------------------------------------- 1 | isRedirection() && $request->pjax()) { 26 | $crawler = new Crawler($response->getContent()); 27 | 28 | // Filter to title (in order to update the browser title bar) 29 | $response_title = $crawler->filter('head > title'); 30 | 31 | // Filter to given container 32 | $response_container = $crawler->filter($request->header('X-PJAX-CONTAINER')); 33 | 34 | // Container must exist 35 | if ($response_container->count() != 0) { 36 | $title = ''; 37 | // If a title-attribute exists 38 | if ($response_title->count() != 0) { 39 | $title = '' . $response_title->html() . ''; 40 | } 41 | 42 | // Set new content for the response 43 | $response->setContent($title . $response_container->html()); 44 | } 45 | 46 | // Updating address bar with the last URL in case there were redirects 47 | $response->header('X-PJAX-URL', $request->getRequestUri()); 48 | } 49 | 50 | return $response; 51 | } 52 | } 53 | --------------------------------------------------------------------------------