Git Product home page Git Product logo

laravel-pjax's Introduction

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've found a bug regarding security please mail [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.

laravel-pjax's People

Contributors

adrianmrn avatar akoepcke avatar askdkc avatar barisbora avatar barnabaskecskes avatar brendt avatar carusogabriel avatar cupoftea696 avatar drbyte avatar freekmurze avatar imacrayon avatar laravel-shift avatar legshooter avatar nielsvanpach avatar patinthehat avatar sebastiandedeyne avatar sfavorite avatar srmklive avatar thunder87 avatar waclaw66 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

laravel-pjax's Issues

Some views needs to be refreshed to see the correct content (CSS issue?)

Hi,

I use this plugin, but even the vanilla code from Laracasts causes the same issue.

The issue is, some pages, and always the same pages, show incorrect content. The content that is missing is Javascript per page scripts. For example, I have an Orgchart page. This uses Google's Chart API to draw an org chart. It loads on a certain page and initialises the names of the people from a controller that sends data to the view.

Is there an easy workaround?

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.

Laravel 6 support?

Hello, do you plan to update the code to work with Laravel 6?

Thanks!

handling json response

This error still persists even after #33 .
The problem is after #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 #38

resources?

Was just wondering why there is a useless resources folder since this is just a package for the middleware? Great work otherwise :)

Metas

Hi,

How to update other metas? example: facebook og tags and canonical?

Thanks

Laravel 7 Support

Installation request for laravel/framework (locked at v7.0.7, required as ^7.0)

Please Update composer

Thanks

Typo in readme

Hey, great job with the package.
I saw a typo in the readme and I thought that making a pull request would be a bit much.
The second paragraph says:

The package provides a middleware that can eturn the reponse that the jquery plugin expects.

It should be return of course. ๐Ÿ˜„

not working pjax in my laravel project

hello
i am using pjax in a laravel 8 project but it doesnt work. below i attached my code and a picture of my error.
pleease help me to solve this problem.
thanks

Kernel.php code:

protected $middleware = [
        // \App\Http\Middleware\TrustHosts::class,
        \App\Http\Middleware\TrustProxies::class,
        \Fruitcake\Cors\HandleCors::class,
        \App\Http\Middleware\PreventRequestsDuringMaintenance::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \App\Http\Middleware\TrimStrings::class,
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
        \Spatie\Pjax\Middleware\FilterIfPjax::class,
    ];

link code:

<a data-pjax href="{{route('artists')}}" class="sidebar__nav-link">artists</a>

container code:

<!-- main content -->
<main class="main">
    <div class="container-fluid" id="mainContainerModule">
        @yield('content')
    </div>
</main>

js code:

document.addEventListener('DOMContentLoaded', function(){
        $(document).pjax('[data-pjax] a, a[data-pjax]', '#mainContainerModule');
    });

image

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].

How to handle JsonResponses? or is this a bug

I am facing this issue where if session gets expired or an exception gets thrown, the middleware FilterIfPjax::filterResponse throws the following error.

Spatie\Pjax\Middleware\FilterIfPjax::filterResponse() must be an instance of Illuminate\Http\Response, instance of Illuminate\Http\JsonResponse given

What is the best way to handle this problem? I have only noticed this issue when accessing a route using pjax call if the user session gets expired or an exception like UnauthorizedException gets thrown.

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?

Middleware returns empty content

Hi!

I'm trying to get this set up on a fresh laravel 5.2 installation, however I cannot get it working.
There seems to be a problem with the response not being set to the correct content. The following block of code is responsible:

$response->setContent(
    $this->makeTitle($crawler).
    $this->fetchContainer($crawler, $container)
);

If I check the output of the makeTitle and fetchContainer functions, both correctly extract and return, however all requests come back with empty content (however the correct headers are set).

Any idea what could be causing this problem?

Update to Laravel 5.2

laravel-pjax won't install with composer update because Laravel 5.2 uses "symfony/dom-crawler": "~3.0" and "symfony/css-selector": "~3.0", whereas laravel-pjax requires the respective ^2.7 versions.

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

Laravel 8 support

Please add support of illuminate/support and illuminate/http ^8.0.
Thanks.

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

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.