Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Pjax Laravel Package

spatie/laravel-pjax

Laravel middleware that detects PJAX (X-PJAX) requests and returns only the expected HTML fragments instead of full pages, enabling faster navigation with jquery-pjax. Simple composer install and add FilterIfPjax to your HTTP kernel.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require spatie/laravel-pjax
    

    Publish the config (optional):

    php artisan vendor:publish --provider="Spatie\Pjax\PjaxServiceProvider" --tag="config"
    
  2. Middleware Registration: Add the middleware to your app/Http/Kernel.php in the $routeMiddleware array:

    'pjax' => \Spatie\Pjax\Middleware\HandlePjaxRequests::class,
    
  3. First Use Case: Apply the middleware to a route (e.g., pjax prefix for PJAX-enabled routes):

    Route::middleware('pjax')->group(function () {
        Route::get('/dashboard', [DashboardController::class, 'index']);
    });
    
  4. Client-Side Setup: Include jQuery and PJAX in your layout (e.g., resources/views/layouts/app.blade.php):

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.pjax/2.0.1/jquery.pjax.min.js"></script>
    <script>
        $(document).pjax('a', '#pjax-container', {timeout: 5000});
    </script>
    

    Wrap your content in a container with an ID (e.g., pjax-container).


Implementation Patterns

Core Workflow

  1. Route-Level PJAX: Apply the middleware to routes where PJAX is desired (e.g., admin dashboards, forms):

    Route::middleware('pjax')->prefix('admin')->group(function () {
        Route::resource('posts', PostController::class);
    });
    
  2. Conditional PJAX: Use the middleware conditionally in controllers:

    public function __construct()
    {
        $this->middleware('pjax')->only(['index', 'store']);
    }
    
  3. Partial Responses: Return partial views (e.g., _partials/dashboard.blade.php) for PJAX requests:

    public function index()
    {
        if (request()->pjax) {
            return view('partials.dashboard-content');
        }
        return view('dashboard');
    }
    
  4. Dynamic Container IDs: Use a consistent container ID (e.g., pjax-container) or dynamically set it:

    $(document).pjax('a[data-pjax]', '#' + $(this).data('pjax-container'));
    
  5. Form Handling: Handle PJAX form submissions by returning partials for success/error responses:

    public function store(Request $request)
    {
        $validated = $request->validate([...]);
        if (request()->pjax) {
            return view('partials.form-success', ['data' => $validated]);
        }
        return redirect()->back()->with('success', 'Submitted!');
    }
    
  6. Layout Overrides: Override the default layout for PJAX requests:

    public function index()
    {
        if (request()->pjax) {
            return view('partials.content', [], ['layout' => 'layouts.pjax']);
        }
        return view('page');
    }
    
  7. API + PJAX Hybrid: Combine PJAX with API responses for dynamic data:

    $(document).on('pjax:complete', function() {
        $.get('/api/data', function(data) {
            $('#dynamic-data').html(data);
        });
    });
    

Gotchas and Tips

Pitfalls

  1. CSRF Token Mismatch: PJAX requests may fail due to missing CSRF tokens. Ensure tokens are included in partials:

    @csrf
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
    
  2. JavaScript Conflicts: PJAX may interfere with event listeners or plugins. Use pjax:beforeSend/pjax:complete to reinitialize:

    $(document).on('pjax:complete', function() {
        $('[data-toggle="tooltip"]').tooltip();
    });
    
  3. Route Caching: Clear route cache if PJAX routes behave unexpectedly:

    php artisan route:clear
    
  4. Partial Caching: Avoid caching partials that rely on dynamic data (e.g., user-specific content).

  5. History API Issues: PJAX may break browser history. Use data-pjax attributes carefully:

    <a href="/page" data-pjax>Load via PJAX</a>
    

Debugging Tips

  1. Check Headers: Verify X-PJAX header is present in responses:

    if (request()->header('X-PJAX')) {
        // PJAX request
    }
    
  2. Log PJAX Requests: Add middleware logging for debugging:

    public function handle($request, Closure $next)
    {
        \Log::info('PJAX request detected', ['pjax' => request()->pjax]);
        return $next($request);
    }
    
  3. Inspect Network Traffic: Use browser dev tools to verify AJAX responses match expected partials.

Extension Points

  1. Custom Middleware: Extend the middleware to add logic (e.g., analytics):

    public function handle($request, Closure $next)
    {
        if ($request->pjax) {
            Analytics::track('pjax_request');
        }
        return $next($request);
    }
    
  2. Dynamic Container IDs: Override the default container ID in config:

    'container_id' => 'dynamic-container',
    
  3. PJAX Events: Listen for PJAX events in JavaScript:

    $(document).on('pjax:start', function() { /* Show loader */ });
    $(document).on('pjax:end', function() { /* Hide loader */ });
    
  4. Vue/PJAX Integration: Use vue-pjax-adapter for Vue.js:

    import VuePjax from 'vue-pjax-adapter';
    Vue.use(VuePjax);
    
  5. Server-Side Rendering (SSR): Disable PJAX for SSR (e.g., Nuxt.js) by checking window.pjax:

    if (window.pjax) {
        // Enable PJAX
    }
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport