Superbalist / laravel-ajax-redirector by superbalist

A Laravel library for handling ajax redirects
3,686
3
33
Package Data
Maintainer Username: superbalist
Maintainer Contact: info@superbalist.com (Superbalist.com a division of Takealot Online (Pty) Ltd)
Package Create Date: 2016-07-08
Package Last Update: 2018-05-04
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2020-10-15 15:27:52
Package Statistics
Total Downloads: 3,686
Monthly Downloads: 2
Daily Downloads: 0
Total Stars: 3
Total Watchers: 33
Total Forks: 4
Total Open Issues: 2

laravel-ajax-redirector

A Laravel library for handling AJAX redirects

Author StyleCI Software License Packagist Version Total Downloads

This package changes the redirect response for AJAX calls from 301 or 302 to a 278 JSON response. XHR requests follow redirects which in a lot of cases, isn't the intention. In most cases, the intention is for the browser to redirect the client to the intended page.

For Laravel 4, please use version 1.0.0.

For Laravel 5, please use version 2.0.0+

Installation

composer require superbalist/laravel-ajax-redirector

Register the service provider in app.php

'providers' => [
    Superbalist\AjaxRedirector\AjaxRedirectServiceProvider::class,
]

Usage

In your application, you'll continue to do redirects like you always have.

return redirect()->to('/test');

In javascript, you'll need to watch for AJAX calls which result in HTTP 278 responses and do a client side redirect using eg: window.location.replace(json.redirect_url);

With jQuery, this can be done using a Global AJAX Event Handler.

With AngularJS, this can be done using a $http interceptor.

The response from the server will look something like:

HTTP/1.1 278 unknown status
Content-Type: application/json

{"redirect_url":"http:\/\/your.site.com\/test"}