├── .gitignore ├── composer.json ├── licence ├── readme.md └── src └── SimonStamm └── LaravelPjax └── LaravelPjaxServiceProvider.php /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | composer.phar 3 | composer.lock 4 | .DS_Store -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "simonstamm/laravel-pjax", 3 | "description": "PJAX for Laravel 4 with redirection-support", 4 | "keywords": ["pjax", "laravel"], 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Simon Stamm", 9 | "email": "simon@stammtec.de" 10 | }, 11 | { 12 | "name": "Vincent Talbot", 13 | "email": "vtalbot@re-bot.co" 14 | } 15 | ], 16 | "require": { 17 | "php": ">=5.3.0", 18 | "illuminate/support": "~4" 19 | }, 20 | "autoload": { 21 | "psr-0": { 22 | "SimonStamm\\LaravelPjax": "src/" 23 | } 24 | }, 25 | "minimum-stability": "dev" 26 | } 27 | -------------------------------------------------------------------------------- /licence: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 Simon Stamm 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 4 2 | 3 | Enable the use of PJAX in Laravel 4 with redirections. 4 | 5 | #### Installation 6 | 7 | Add `simonstamm/laravel-pjax` to `require` section in your `composer.json` 8 | 9 | composer require simonstamm/laravel-pjax 10 | 11 | Add `'SimonStamm\LaravelPjax\LaravelPjaxServiceProvider',` to `providers` in your `app/config/app.php` 12 | 13 | #### How to use 14 | 15 | This service provider will check, before output the http response, for the `X-PJAX`'s 16 | header in the request. If found, it will crawl the response to return the requested 17 | element defined by `X-PJAX-Container`'s header. 18 | 19 | Works great with [jquery.pjax.js](https://github.com/defunkt/jquery-pjax). 20 | 21 | ### Contribution 22 | 23 | I'm open to any idea of features to add to it. 24 | -------------------------------------------------------------------------------- /src/SimonStamm/LaravelPjax/LaravelPjaxServiceProvider.php: -------------------------------------------------------------------------------- 1 | app; 27 | 28 | $this->app->after(function($request, $response) use ($app) 29 | { 30 | // Only handle non-redirections 31 | if (!$response->isRedirection()) { 32 | // Must be a pjax-request 33 | if ($request->server->get('HTTP_X_PJAX')) { 34 | $crawler = new Crawler($response->getContent()); 35 | 36 | // Filter to title (in order to update the browser title bar) 37 | $response_title = $crawler->filter('head > title'); 38 | 39 | // Filter to given container 40 | $response_container = $crawler->filter($request->server->get('HTTP_X_PJAX_CONTAINER')); 41 | 42 | // Container must exist 43 | if ($response_container->count() != 0) { 44 | 45 | $title = ''; 46 | // If a title-attribute exists 47 | if ($response_title->count() != 0) { 48 | $title = '' . $response_title->html() . ''; 49 | } 50 | 51 | // Set new content for the response 52 | $response->setContent($title . $response_container->html()); 53 | } 54 | 55 | // Updating address bar with the last URL in case there were redirects 56 | $response->header('X-PJAX-URL', $request->getRequestUri()); 57 | } 58 | } 59 | }); 60 | } 61 | 62 | /** 63 | * Get the services provided by the provider. 64 | * 65 | * @return array 66 | */ 67 | public function provides() 68 | { 69 | return array(); 70 | } 71 | 72 | } 73 | --------------------------------------------------------------------------------