Laravel 4阿贾克斯检查,包括XMLHtt prequest(Magnific酒店从弹出)

问题描述:

使用the从这个问题code

@extends('layouts.' . isset($ajax) ? 'ajax' : 'master')

要检查阿贾克斯。它使用弹出时,适用于常规Ajax页面加载,但并非如此。

to check for Ajax. It works for regular Ajax page loads but not when using a popup.

在这种情况下,我使用弹出Magnific酒店的Ajax模式,请求头是XMLHtt prequest但Laravel返回非Ajax(扩展)的布局。

In this case I'm using Magnific Popup's Ajax mode, the request header is XMLHttpRequest but Laravel returns the non-ajax (extended) layout.

首先,我不知道如何使用 $ AJAX 变量被设置(使用isset($阿贾克斯)),但正确的方式为您在 Laravel

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()) {
    // ...
}

或者短格式(使用三元运营商在一个前pression)

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'))

如何工作的吗?

厂商\ laravel \框架\ SRC \照亮\的Http 有一个 Request.php 类,你可以看

/**
 * Determine if the request is the result of an AJAX call.
 * 
 * @return bool
 */
public function ajax()
{
    return $this->isXmlHttpRequest();
}

下面 isXmlHtt prequest() Request.php扩展方法交响曲,因为 Laravel 要求类扩展 Symfony的\分量\ HttpFoundation \ Request.php 并在这个类存在的主要方法,它决定了 AJAX

要求>

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');
}

所以,如果 X-要求 - 以请求头被设置,那么它是一个Ajax请求,如果这个头不发送那就不是一个 AJAX 请求。所以,问题是如何使用isset($阿贾克斯)正在设置,如果它是由你设置那么的jQuery 库您使用的是它不这样做,但它发送 X-要求 - 以请求头,而不是在这种情况下,你应该使用 Laravel 支持::阿贾克斯()的方法来确定 AJAX 的要求。

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.

顺便说一句,我想preFER使用完全不同的视图 AJAX 要求其不延长的布局。你可能会喜欢这个检测Ajax请求,PHP和框架

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.