向Laravel资源控制器提出AJAX请求,最佳实践

时间:2018-12-03 12:47:48

标签: javascript ajax laravel get

我已经在Laravel中使用以下索引功能设置了资源控制器:

public function index()
{
    if (!Auth::check()) {
        return redirect()->route('login');
    }
    $decks = Auth::user()->decks->sortByDesc('name');
    return view('decks.index')->with('decks', $decks);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
}

在另一个页面上,我还需要通过AJAX调用来$decks变量。现在,我已经建立了一条到我的控制器的附加路由,可以通过GET请求从中检索甲板:

public function getDecks()
{
    if (!Auth::check()) {
        return;
    }
    $decks = Auth::user()->decks->sortByDesc('name');
    return response()->json($decks);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
}

我的问题:是否可以通过直接向$decks的请求获取index变量,还是我的解决方案呢?

如果我请求获取索引索引,则会获取decks.index视图的HTML,但是如何访问(如果可能的话)$decks变量?

我想我不太了解的是:$decks语句中的->with('decks', $decks)会发生什么?我知道我可以使用该页面上的刀片语法访问$decks,但是它从哪里访问数据,我也可以通过AJAX访问它吗?

3 个答案:

答案 0 :(得分:2)

是的!您可以根据执行的请求类型返回不同的结果。不需要2条路线:

/(?:^(?:36[0-4]|3[0-5]\d|[12]\d{2}|[0-9]\d?)$)|(?:^0[0-9].*$)/

@Sven Hakvoort是正确的,您应该在路由定义中检查身份验证:

/^(36[0-4]|3[0-5]\d|[12]\d{2}|[0-9]\d?)|(?:0[0-9].*)/

答案 1 :(得分:0)

You can not retrieve the $decks from the index route directly. When you call view(..)->with(...) it is internally passed to the blade template processor which also receives the $decks variable, the HTML is then build on the server-side and the 'compiled' HTMl is then returned to the browser. So once the server returns the result the $decks variable is not present anymore. Because of this behavior it is not possible to do what you want.

So yes, your solution is the way to go, although you might consider wrapping the Auth::check() in a middleware and move the call to $decks to a separate function in order to simplify your code.

I hope this answers your question, if anything is unclear feel free to ask!

答案 2 :(得分:0)

Your solution is the proper way. It is not typically a good idea to handle any business logic or data retrieval using your routes - using a controller is preferred, which is what you are doing. If you want to avoid the if (Auth::check()) statement in your controller, you could add the auth middleware directly to that route like this.

Route::get('example', 'YourController@index')->middleware('auth');

Regarding your question about ->with('decks', $decks): That function is sending your $decks variable to the view to be rendered in the blade template. That data is used by the server to render the page, and then the variable is discarded. If you want to also be able to work with that data on your page using javascript, you could do something like this in your blade template.

<script>
    var decks = {!!$decks->toJSON()!!};
</script>