Skip to content

Instantly share code, notes, and snippets.

@terion-name
Last active December 20, 2015 06:39
Show Gist options
  • Save terion-name/6087641 to your computer and use it in GitHub Desktop.
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…
$(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) {
}
}
});
<?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);
}
}
}
<?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;
}
}
<?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();
}
}
}
@terion-name
Copy link
Author

  • Put php classes in /app/lib/RedirectAxax directory
  • Add "app_path().'/lib'" to ClassLoader array 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"} 

with status code 200 (to prevent browser redirecting).

Also jQuery global ajax setup included to resolve this redirects automatically.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment