2

I want override laravel at place where response is returned. Then I want to detect status code(200 or 301) and if request is ajax. If status code is 200 and request is ajax I want to return custom html. Something like :

protected function returnResponse($statusCode, $html, $redirectUrl){
    if($statusCode == 200  &&  isAjax()){
        return parent::returnResponse($customStatusCode, $customHtml, $customRedirectUrl);
    }
    return parent::returnResponse($statusCode, $html, $redirectUrl);
}

EDITED: I have this:

class SomeMiddleware
{
    public function handle($request, Closure $next)
    {
        // do before 

        $request = $next($request);

        //do after

        return $request;
    }
}

But how to detect if current response is redirect ?

5
  • 1
    Middleware Commented Jun 20, 2016 at 14:21
  • do you have something more ? Commented Jun 20, 2016 at 14:29
  • In your question you said you want to return a custom response if the request is ajax. Why do you now need to know if the response is redirect? Commented Jun 20, 2016 at 14:49
  • because if response is redirect (301) and ajax, it is complicated and browser related "turn off" redirect follow in jquery, so when request is ajax and 301 I want to change 301 to 200 and then manualy redirect . Commented Jun 20, 2016 at 14:54
  • Ok see my answer below, I've walked through the pieces I think you are looking for. Commented Jun 20, 2016 at 14:55

2 Answers 2

1

If you want to inspect the final response and possibly return an alternate response, let's write a simple middleware.

It sounds like you want to do your checks at the end, after the default response has been built (so you can examine it). So we'll start like this:

// First get the default response
$response = $next($request);

Our $response variable will now hold the response Laravel is about to respond with.

If you want to see if the response is a redirect, you can easily check for a RedirectResponse instance:

$isRedirect = $response instanceof \Illuminate\Http\RedirectResponse;

You can test to see if the original request is ajax quite simply:

$isAjax = $request->ajax();

If you want to now return a different response instead of the one you were handed, I'd use the simple response() helper method:

return response($content, $status);

Putting it together, I believe this is roughly what you're looking for:

use Illuminate\Http\RedirectResponse;

class HijackMiddleware
{
    public function handle($request, Closure $next)
    {
        $response = $next($request);

        if($request->ajax() && $response instanceof RedirectResponse) {
            return response("This is a <strong>different and custom</bold> response");
        }

        return $response;
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Ok this is good, now I need to get redirected url and thats it.
@fico7489 $response->getTargetUrl()
@fico7489 I just rejected the edit. You already have the $request handed to your function, use that! Much better than calling the \Request facade.
@fico7489 if you prefer $response->getStatusCode() == 301, that's fine. I prefer looking for an instance RedirectResponse, I think it's clearer what the code is looking for. Your call.
@fico7489 Make sure you have that use statement at the top of your file to import that class. Also, just dd($response) to inspect it and see what it is!
1

My final solution :

<?php

namespace App\Http\Middleware;

use Closure;
use Response;
use Request;

class AjaxForm
{

    public function handle($request, Closure $next, $guard = null)
    {
        $response = $next($request);

        if(Request::ajax() && $response->status() == 301) {
            return (Response::make($response->getTargetUrl(), '200'));
        }

        return $response;
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.