Using the code from this question,
@extends('layouts.' . isset($ajax) ? 'ajax' : 'master')
to check for Ajax. It works for regular Ajax page loads but not when using a popup.
In this case I'm using Magnific Popup's Ajax mode, the request header is XMLHttpRequest but Laravel returns the non-ajax (extended) layout.
First of all I don't know how the $ajax
variable is being set(isset($ajax)
), but the right way to check for an ajax request in Laravel
is
if(Request::ajax()) {
// ...
}
Or, short form (using ternary operator in a single expression)
$ajax = Request::ajax() ? true : false;
So, according to your link of another answer, this should work
@extends(((Request::ajax()) ? 'layouts.ajax' : 'layouts.master'))
How this works ?
In vendor\laravel\framework\src\Illuminate\Http
there is a Request.php
class you can see
/**
* Determine if the request is the result of an AJAX call.
*
* @return bool
*/
public function ajax()
{
return $this->isXmlHttpRequest();
}
Here isXmlHttpRequest()
is an extended method from Request.php
class of Symphony
, because Laravel
's Request
class extends Symfony\Component\HttpFoundation\Request.php
and in this class there is the main method which determines the ajax
request by
public function isXmlHttpRequest()
{
return 'XMLHttpRequest' == $this->headers->get('X-Requested-With');
}
So, if X-Requested-With
request header is set then it's an ajax request and if this header is not sent then it's not an ajax
request. So, the question is how isset($ajax)
is being set and if it's set by you then the jQuery
library you are using it is not doing it but it's sending X-Requested-With
request header instead and in this case you should use Laravel
's Request::ajax()
method to determine the ajax
request.
BTW, I would prefer to use a completely different view
for ajax
request which doesn't extend master
layout. You may like this Detect Ajax Request-Php And Frameworks.