Skip to content
This repository has been archived by the owner on Jul 17, 2024. It is now read-only.

Commit

Permalink
initial code push for first release
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewgoslett committed Jul 8, 2016
0 parents commit 133786e
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.idea
composer.lock
vendor
bin
coverage
coverage.xml
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016 Superbalist

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# laravel-ajax-redirector

A Laravel library for handling AJAX redirects

[![Author](http://img.shields.io/badge/[email protected]?style=flat-square)](https://twitter.com/superbalist)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)
[![Packagist Version](https://img.shields.io/packagist/v/superbalist/laravel-ajax-redirector.svg?style=flat-square)](https://packagist.org/packages/superbalist/laravel-ajax-redirector)
[![Total Downloads](https://img.shields.io/packagist/dt/superbalist/laravel-ajax-redirector.svg?style=flat-square)](https://packagist.org/packages/superbalist/laravel-ajax-redirector)

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.

## Installation

```bash
composer require superbalist/laravel-ajax-redirector
```

Register the service provider in app.php
```php
'providers' => [
Superbalist\AjaxRedirector\AjaxRedirectServiceProvider::class,
]
```


## Usage

In your application, you'll continue to do redirects like you always have.
```php
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"}
```
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Changelog

## 1.0.0 - 2016-07-08

* Initial release
26 changes: 26 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "superbalist/laravel-ajax-redirector",
"description": "A Laravel library for handling ajax redirects",
"license": "MIT",
"authors": [
{
"name": "Superbalist.com a division of Takealot Online (Pty) Ltd",
"email": "[email protected]"
}
],
"require": {
"php": ">=5.5.0",
"illuminate/routing": "^5.2"

},
"autoload": {
"psr-4": {
"Superbalist\\AjaxRedirector\\": "src/"
}
},
"extra": {
"branch-alias": {
"dev-master": "1.0-dev"
}
}
}
45 changes: 45 additions & 0 deletions src/AjaxRedirectResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Superbalist\AjaxRedirector;

use Illuminate\Http\RedirectResponse;

class AjaxRedirectResponse extends RedirectResponse
{

/**
* Sets the redirect target of this response.
*
* @param string $url The URL to redirect to
* @return RedirectResponse The current response.
* @throws \InvalidArgumentException
*/
public function setTargetUrl($url)
{
if (empty($url)) {
throw new \InvalidArgumentException('Cannot redirect to an empty URL.');
}

$this->targetUrl = $url;

$data = [
'redirect_url' => $url
];

$this->setContent(json_encode($data));
$this->headers->set('Content-Type', 'application/json');

return $this;
}

/**
* Is the response a redirect of some form?
*
* @param string $location
* @return bool
*/
public function isRedirect($location = null)
{
return true;
}
}
29 changes: 29 additions & 0 deletions src/AjaxRedirectServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Superbalist\AjaxRedirector;

use Illuminate\Support\ServiceProvider;

class AjaxRedirectServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app['redirect'] = $this->app->share(function ($app) {
$redirector = new Redirector($app['url']);

// If the session is set on the application instance, we'll inject it into
// the redirector instance. This allows the redirect responses to allow
// for the quite convenient "with" methods that flash to the session.
if (isset($app['session.store'])) {
$redirector->setSession($app['session.store']);
}

return $redirector;
});
}
}
37 changes: 37 additions & 0 deletions src/Redirector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Superbalist\AjaxRedirector;

use Illuminate\Routing\Redirector as BaseRedirector;

class Redirector extends BaseRedirector
{
/**
* Create a new redirect response.
*
* @param string $path
* @param int $status
* @param array $headers
* @return \Illuminate\Http\RedirectResponse
*/
protected function createRedirect($path, $status, $headers)
{
$request = $this->generator->getRequest();
if ($request->ajax()) {
// for ajax requests, we don't want to return a typical 301 or 302 redirect since the ajax request
// will follow that and not actually redirect the browser, which is likely our intention
// we therefore return a json response via our AjaxRedirectResponse class
$redirect = new AjaxRedirectResponse($path, 278, $headers);

if (isset($this->session)) {
$redirect->setSession($this->session);
}

$redirect->setRequest($this->generator->getRequest());

return $redirect;
} else {
return parent::createRedirect($path, $status, $headers);
}
}
}

0 comments on commit 133786e

Please sign in to comment.