A pjax middleware for Laravel

Overview

A pjax middleware for Laravel

Latest Version on Packagist Software License GitHub Workflow Status Total Downloads

Pjax is a jQuery plugin that leverages ajax to speed up the loading time of your pages. It works by only fetching specific html fragments from the server, and client-side updating only happens on certain parts of the page.

The package provides a middleware that can return the response that the jQuery plugin expects.

There's a Vue-PJAX Adapter equivalent by @barnabaskecskes which doesn't require jQuery.

Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Installation

You can install the package via composer:

$ composer require spatie/laravel-pjax

Next you must add the \Spatie\Pjax\Middleware\FilterIfPjax-middleware to the kernel.

// app/Http/Kernel.php

...
protected $middleware = [
    ...
    \Spatie\Pjax\Middleware\FilterIfPjax::class,
];

Usage

The provided middleware provides the behaviour that the pjax plugin expects of the server:

An X-PJAX request header is set to differentiate a pjax request from normal XHR requests. In this case, if the request is pjax, we skip the layout html and just render the inner contents of the container.

Laravel cache busting tip

When using Laravel Mix to manage your frontend cache busting, you can use it to your advantage to bust pjax's cache. Simply include the mix method as the content of the x-pjax-version meta tag:

<meta http-equiv="x-pjax-version" content="{{ mix('/css/app.css') }}">

Multiple files:

<meta http-equiv="x-pjax-version" content="{{ mix('/css/app.css') . mix('/css/app2.css') }}">

This way, anytime your frontend's cache gets busted, pjax's cache gets automatically busted as well!

Changelog

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

The middleware in this package was originally written by Jeffrey Way for the Laracasts-lesson on pjax. His original code can be found in this repo on GitHub.

License

The MIT License (MIT). Please see License File for more information.

Comments
  • Not working

    Not working

    HI I followed the instructions but it seems like I have a problem with this. I imported the js scripts jquery.min.js and jquery.pjax.js in my app.blade.php I also put this inside in script tag in my app.blade.php $(document).pjax('a', '#pjax-container'); and put an id pjax-container on my div tag but everytime I go to the chat link it will still refresh the whole page and vice versa if I go to profile page. I checked the Network in inspect element I got this error Name : http://circle.dev/profile?_pjax=%23pjax-container Status: 500 Internal server error

    Any idea on how to fix this? PS. my laravel project is 5.5 I just want to integrate the pjax to load faster

    opened by rexdarel 8
  • Unprocessable Entity

    Unprocessable Entity

    Hi, I installed the middleware as described in the readme, but in the console on the get request I can see an error: unprocessable entity and then the page redirects.

    Please help. Is there something I am missing?

    I am using laravel 5.2

    opened by louiscoetzee 5
  • Updates setVersionHeader() to check specific meta

    Updates setVersionHeader() to check specific meta

    Right now, the meta tag that's being filtered from the request by setVersionHeader() is actually all http-equiv meta tags.

    As I had <meta http-equiv="X-UA-Compatible" content="IE=edge"> in my project's <head>, it kept capturing IE=edge.

    So with this change instead, it will now look for a specific <meta http-equiv="X-PJAX-Version" content="foo"> tag, to enable the correct version to be passed on to the response.

    If this is accepted, I'll PR the Readme with a cool tip about using Laravel's elixir, to handle force reload automatically.

    opened by legshooter 5
  • handling json response

    handling json response

    Symfony\Component\Debug\Exception\FatalThrowableError in GET /applications Argument 1 passed to Spatie\Pjax\Middleware\FilterIfPjax::filterResponse() must be an instance of Illuminate\Http\Response, instance of Illuminate\Http\JsonResponse given, called in /.../vendor/spatie/laravel-pjax/src/Middleware/FilterIfPjax.php on line 35

    I've noticed the issue has already been raised and closed in #29 but it does not seem to be fixed yet and was closed without any explaination so the bug still seems to be there

    opened by juliangums 4
  • Laravel 9 Support

    Laravel 9 Support

    I am trying to install with Laravel 9 but its showing error.

    Your requirements could not be resolved to an installable set of packages.

      Problem 1
        - Root composer.json requires spatie/laravel-pjax ^2.2 -> satisfiable by spatie/laravel-pjax[2.2.0].
        - spatie/laravel-pjax 2.2.0 requires illuminate/http ^6.0|^7.0|^8.0 -> found illuminate/http[v6.0.0, ..., 6.x-dev, v7.0.0, ..., 7.x-dev, v8.0.0, ..., 8.x-dev] but these were 
    not loaded, likely because it conflicts with another require.
    
    You can also try re-running composer require with an explicit version constraint, e.g. "composer require spatie/laravel-pjax:*" to figure out if any version is installable, or "composer require spatie/laravel-pjax:^2.1" if you know which you need.
    

    Installation failed, reverting ./composer.json and ./composer.lock to their original content.

    opened by odnualam 2
  • install error.

    install error.

    composer require spatie/laravel-pjax Using version ^2.0 for spatie/laravel-pjax ./composer.json has been updated Loading composer repositories with package information Updating dependencies (including require-dev) Your requirements could not be resolved to an installable set of packages.

    Problem 1 - Conclusion: don't install spatie/laravel-pjax 2.0.1 - Conclusion: remove symfony/css-selector v5.0.1 - Installation request for spatie/laravel-pjax ^2.0 -> satisfiable by spatie/laravel-pjax[2.0.0, 2.0.1]. - Conclusion: don't install symfony/css-selector v5.0.1 ..... - Installation request for symfony/css-selector (locked at v5.0.1) -> satisfiable by symfony/css-selector[v5.0.1].

    help wanted 
    opened by sunny-kevin 2
  • Version bump to follow latest Laravel dependency range

    Version bump to follow latest Laravel dependency range

    Latest Laravel version would not support this package without this update.

    - Installation request for symfony/css-selector (locked at v4.0.3) -> satisfiable by symfony/css-selector[v4.0.3].
    
    opened by barnabaskecskes 2
  • Open-source development monetization

    Open-source development monetization

    In short: this PR proposes a way to monetize open-source development of your project. The idea is similar to how free-to-play works in gamedev industry: "You are free to wait for the package download - but in case if time is money for you, please consider buying non-waiting access to the package, every cent goes to the package developer to incentive the open-source development."

    For details please refer to https://github.com/Free2Wait/composer-free2wait .

    According to packagist statistics of your package https://packagist.org/packages/spatie/laravel-pjax/stats , in theory it could bring to you about 2,704 (installs per month) * 0.03 (estimated conversion installs-to-sale) * 5 (price of each non-waiting access per month) = estimated $405.6 per month.

    Please give me your feedback about the idea: what do you think? Are you interested in monetizing open-source development? Do you have any suggestions? Concerns?

    Would be glad to receive any feeedback. Thank you for attention.

    opened by PatchRanger 2
  • Middleware prority

    Middleware prority

    I have a middleware only apply to a controller but I want my middleware to execute before pjax's middleware.

    Example code of my middleware:

    public function handle($request, Closure $next)
    {
            $booking = session()->get('booking');
    
            if (!$booking) {
                $request->session()->flash('alert-warning', 'Your session is expired.');
    
                $request->headers->remove('X-PJAX');
    
                return redirect()->route('booking.index');
            }
    
            return $next($request);
    }
    

    The purpose of my middleware is to disable PJAX and redirect with normal request if my session variable is empty.

    Do you have any idea to achieve this?

    opened by iainheng 2
  • Fix Response constructor

    Fix Response constructor

    The create method was removed from Response in Symfony 6: https://github.com/symfony/symfony/blob/5.4/src/Symfony/Component/HttpFoundation/Response.php#L237

    opened by imacrayon 1
  • handling json response

    handling json response

    This error still persists even after https://github.com/spatie/laravel-pjax/pull/33 . The problem is after https://github.com/spatie/laravel-pjax/pull/33 the handle method returns Symfony\Component\HttpFoundation\Response whereas some of the functions in the FilterIfPjax class (function filterResponse(), function getCrawler()) expect Illuminate\Http\Response . I have changed the parameter type to use Symfony\Component\HttpFoundation\Response as BaseResponse; in https://github.com/spatie/laravel-pjax/pull/38

    opened by Dennis-Mwea 1
Releases(2.2.2)
Owner
Spatie
Webdesign agency based in Antwerp, Belgium
Spatie
Laravel Javascript Validation

Laravel Javascript Validation Laravel Javascript Validation package allows to reuse your Laravel Validation Rules, Messages, FormRequest and Validator

Proengsoft 991 Jan 4, 2023
Use your Laravel named routes in JavaScript

Ziggy – Use your Laravel routes in JavaScript Ziggy provides a JavaScript route() helper function that works like Laravel's, making it easy to use you

Tighten 3.1k Dec 28, 2022
A full-stack framework for Laravel that takes the pain out of building dynamic UIs.

Laravel Livewire See the docs for everything: https://laravel-livewire.com/docs/quickstart Awesome Livewire stuff here: https://github.com/imliam/awes

Livewire 17.7k Dec 31, 2022
Html Minifier adalah paket simpel untuk minify output Html, Css style, dan Javascript sebelum dirender ke browser untuk aplikasi Laravel anda.

Laravel Html Minifier Adalah Paket simpel untuk minify HTML, Css Style, dan Javascript sebelum dirender ke browser untuk aplikasi Laravel anda. Alat i

:D 16 Aug 17, 2022
Use your Laravel named routes in JavaScript

Ziggy – Use your Laravel routes in JavaScript Ziggy provides a JavaScript route() helper function that works like Laravel's, making it easy to use you

Tighten 3.1k Dec 28, 2022
PSR-7 middleware foundation for building and dispatching middleware pipelines

laminas-stratigility From "Strata", Latin for "layer", and "agility". This package supersedes and replaces phly/conduit. Stratigility is a port of Sen

Laminas Project 47 Dec 22, 2022
Laravel Idempotency Middleware

Laravel Idempotency Middleware Install Require this package with composer using the following command: composer require revangelista/laravel-idempoten

Renan Evangelista 6 Jun 9, 2022
Simple Laravel 5+ (5.4) Theme Switcher with Middleware!

Laravel Theme The simpliest of theme switching for Laravel 5 Installation Usage Installation Laravel 5.1+ Install Laravel Theme manager: composer requ

null 11 Aug 30, 2020
Sqlcommenter is a plugin/middleware/wrapper to augment SQL statements from laravel

sqlcommenter is a plugin/middleware/wrapper to augment SQL statements from laravel with comments that can be used later to correlate user code with SQL statements.

Google 7 Jul 14, 2022
PSR-15 middleware in minutes!

zend-expressive Repository abandoned 2019-12-31 This repository has moved to mezzio/mezzio. Develop PSR-7 middleware applications in minutes! zend-exp

Zend Framework 718 Dec 9, 2022
Middleware for PHP built on top of PSR-7 and PSR-15

zend-stratigility Repository abandoned 2019-12-31 This repository has moved to laminas/laminas-stratigility. From "Strata", Latin for "layer", and "ag

Zend Framework 236 Sep 9, 2022
Slim Framework 2 middleware

Slim Framework Middleware This repository contains a library of optional middleware for your Slim Framework application. How to Install Update your co

Slim Framework 47 Nov 7, 2022
Fast PSR-7 based routing and dispatch component including PSR-15 middleware, built on top of FastRoute.

Route This package is compliant with PSR-1, PSR-2, PSR-4, PSR-7, PSR-11 and PSR-15. If you notice compliance oversights, please send a patch via pull

The League of Extraordinary Packages 608 Dec 30, 2022
Provides a Middleware to integration Tideways into Symfony Messenger Processing

Tideways Middleware for Symfony Messenger This package is currently under development and might be moved into the Tideways PHP Extension or stay indep

Tideways 6 Jul 5, 2022
CORS (Cross-Origin Resource Sharing) middleware for Hyperf application.

CORS Middleware for Hyperf Implements fruitcake/laravel-cors for Hyperf. Features Handles CORS pre-flight OPTIONS requests Adds CORS headers to your r

Gang Wu 8 Sep 19, 2022
Disable Google's FLoC with help of PSR-15 middleware

Disable Google's FLoC with PSR-15 middleware This package will help you disable Google's FLoC. Installation You can install the package via composer:

P7V 9 Dec 14, 2022
A simple and flexible PHP middleware dispatcher based on PSR-7, PSR-11, and PSR-15

Woohoo Labs. Harmony Woohoo Labs. Harmony is a PSR-15 compatible middleware dispatcher. Harmony was born to be a totally flexible and almost invisible

Woohoo Labs. 153 Sep 5, 2022
CORS Middleware for Lumen micro-framework

Lumen-CORS Cross-origin resource sharing (CORS) Middleware for Lumen micro-framework. Installation After you install lumen as per lumen docs, install

Palani Kumanan 101 Nov 21, 2022
PHP CORS (Cross-origin resource sharing) middleware.

CORS PHP CORS (Cross-origin resource sharing) middleware. Support Array, Coding in Native PHP Using PSR-7 PSR-15 Support Symfony Support Laravel Suppo

Seven Du 269 Nov 9, 2022
PSR-15 middleware for Symfony framework.

PSR-15 middleware now in Symfony Contents Installation Configuration Usage Examples Customization Caching Real World Example Middlewares Testing Licen

kafkiansky 60 Dec 14, 2022