Laravel 4:响应来自控制器的 AJAX 请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20007797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:39:42  来源:igfitidea点击:

Laravel 4: Responding to AJAX requests from controller

ajaxlaravellaravel-4dry

提问by Tristan Godfrey

I'm trying to generate ajax specific responses from my controllers by using the Request::ajax() method, which is working just fine. The only problem is that the way I have it set up right now isn't really a nice looking solution.

我正在尝试使用 Request::ajax() 方法从我的控制器生成特定于 ajax 的响应,该方法工作正常。唯一的问题是我现在设置它的方式并不是一个很好看的解决方案。

My controller:

我的控制器:

class HomeController extends BaseController {

protected $layout = 'layouts/main';

public function __construct()
{
    $this->beforeFilter('auth');
}

public function getIndex()
{
    $view = View::make('content.home.index');
    if(Request::ajax()) return $view; //For ajax calls we only want to return the content to be placed inside our container, without the layout
    $this->layout->menu = 'content.menu';
    $this->layout->content = $view;
}

}

So right now, for every method I define within my controllers I need to add the code snippet that checks for an AJAX request and returns a single view if the statement returns true.

所以现在,对于我在控制器中定义的每个方法,我需要添加检查 AJAX 请求并在语句返回 true 时返回单个视图的代码片段。

This leads to my question that is probably more PHP related than it is to the framework;

这导致我的问题可能与 PHP 的相关性比与框架的相关性更高;

Is there a way of executing my AJAX check on every method call, without actually placing it inside the method? Or is there some other solution to keep my code DRY?

有没有办法对每个方法调用执行我的 AJAX 检查,而无需将其实际放置在方法中?或者是否有其他解决方案可以让我的代码保持干燥?

Thanks in advance!

提前致谢!

PS: This is my first post on stackoverflow, so feel free to correct me if I made any mistakes

PS:这是我在stackoverflow上的第一篇文章,所以如果我犯了任何错误,请随时纠正我

回答by tharumax

Create a new barebone layout named 'layouts/ajax' (or any name you like).

创建一个名为“layouts/ajax”(或任何你喜欢的名字)的新准系统布局。

    <?php echo $content ?>

In your Base controller, override this setupLayout()function.

在您的 Base 控制器中,覆盖此setupLayout()功能。

protected function setupLayout()
{
    if ( ! is_null($this->layout))
    {
        $layout = Request::ajax() ? 'layouts/ajax' : $this->layout;
        $this->layout = View::make($layout);            
    }
}

Change your getIndex()function to this.

将您的getIndex()功能更改为此。

public function getIndex()
{
    $view = View::make('content.home.index');
    $this->layout->menu = 'content.menu';
    $this->layout->content = $view;
}

Now non-ajax requests will be rendered using layoutset in the controller, where as ajax requests will receive whatever set to $this->layout->content.

现在,非 ajax 请求将使用layout控制器中的 set呈现,而 ajax 请求将接收任何设置为$this->layout->content.

Note : Controller will neglect the layout setup in setupLayout(), if the called method returns truthy value. So this method will not work for functions like below.

注意:setupLayout()如果被调用的方法返回真值,控制器将忽略 中的布局设置。因此,此方法不适用于以下功能。

public function getIndex()
{
    return View::make('content.home.index');
}

回答by jah

You could just change the layout property, in the constructor, if it's an ajax request:

如果是ajax请求,您可以在构造函数中更改布局属性:

public function __construct()
{
    $this->beforeFilter('auth');

    if(Request::ajax()) {
        $this->layout = '';
    }
}

If it doesn't work try setting it to NULL instead.

如果它不起作用,请尝试将其设置为 NULL。

回答by bgallagh3r

Why would you return a VIEW via ajax? Are you using it to create a SPA? If so there are better ways. I'm generally against returning HTML via AJAX.

为什么要通过 ajax 返回 VIEW?您是否使用它来创建 SPA?如果是这样,还有更好的方法。我通常反对通过 AJAX 返回 HTML。

The route I'd go in your position is probably opposite of how you're doing it. Render the view no matter what, if the request is ajax, pass the extra data back and have JS render the data on the page. That's essentially how most Javascript MVC frameworks function.

我在你的位置上走的路线可能与你的做法相反。无论如何渲染视图,如果请求是ajax,则将额外的数据传回并让JS在页面上渲染数据。这基本上就是大多数 Javascript MVC 框架的运作方式。

Sorry if I am totally missing the point here, just going on an assumption of your end goal with the info you provided.

很抱歉,如果我完全错过了这里的重点,只是根据您提供的信息假设您的最终目标。