Last active
December 20, 2015 06:39
-
-
Save terion-name/6087641 to your computer and use it in GitHub Desktop.
Laravel 4 lib that provides AJAX redirects in Redirector.
Put it in /app/lib/RedirectAxax directory; add "app_path().'/lib'" to ClassLoader in /start/global.php; bind alias in /app/config/app.php: 'Redirect'=> 'RedirectAjax\RedirectAjax'. After that redirects called in ajax requests will output JSON: {"redirect":"http://example.com"}. Also jQuer…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ajaxComplete(function (event, xhr, settings) { | |
/* | |
* Ajax-response redirect handler | |
* add "catchRedirect" : true to request to override this default behaviour | |
*/ | |
if (!settings.catchRedirect) { | |
try { | |
var resp = $.parseJSON(xhr.responseText); | |
if (resp.redirect) { | |
window.location.href = resp.redirect; | |
} | |
} | |
catch (e) { | |
} | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace RedirectAjax; | |
class RedirectAjax extends \Illuminate\Support\Facades\Redirect { | |
public static function __callStatic($method, $args) { | |
\App::bind('redirect', 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'])) | |
{ | |
$redirector->setSession($app['session']); | |
} | |
return $redirector; | |
}); | |
switch (count($args)) | |
{ | |
case 0: | |
return parent::$method(); | |
case 1: | |
return parent::$method($args[0]); | |
case 2: | |
return parent::$method($args[0], $args[1]); | |
case 3: | |
return parent::$method($args[0], $args[1], $args[2]); | |
case 4: | |
return parent::$method($args[0], $args[1], $args[2], $args[3]); | |
default: | |
//return call_user_func_array(array($instance, $method), $args); | |
return call_user_func_array('parent::'.$method, $args); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace RedirectAjax; | |
class Redirector extends \Illuminate\Routing\Redirector { | |
protected function createRedirect($path, $status, $headers) | |
{ | |
$redirect = new RedirectResponse($path, $status, $headers); | |
if (isset($this->session)) | |
{ | |
$redirect->setSession($this->session); | |
} | |
$redirect->setRequest($this->generator->getRequest()); | |
return $redirect; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace RedirectAjax; | |
class RedirectResponse extends \Illuminate\Http\RedirectResponse { | |
public function __toString() { | |
if ($this->request->ajax()) { | |
return json_encode(['redirect' => $this->headers->get('Location')]); | |
} | |
else { | |
return parent::__toString(); | |
} | |
} | |
public function sendHeaders() | |
{ | |
if ($this->request->ajax()) { | |
$location = $this->headers->get('Location'); | |
$this->headers->remove('Location'); | |
$this->headers->set('Content-type', 'application/json'); | |
$this->setStatusCode(200); | |
$return = parent::sendHeaders(); | |
$this->headers->set('Location', $location); | |
return $return; | |
} | |
else { | |
return parent::sendHeaders(); | |
} | |
} | |
public function sendContent() | |
{ | |
if ($this->request->ajax()) { | |
echo json_encode(['redirect' => $this->headers->get('Location')]); | |
return $this; | |
} | |
else { | |
return parent::sendContent(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
After that redirects called in ajax requests will output JSON:
with status code 200 (to prevent browser redirecting).
Also jQuery global ajax setup included to resolve this redirects automatically.