如何对 Laravel 5 中的方法进行宁静的 ajax 路由?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28970350/
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
How to do restful ajax routes to methods in Laravel 5?
提问by prograhammer
So I have a route that looks like this:
所以我有一个看起来像这样的路线:
Route::any('some/page', ['as' => 'some-page', 'uses' => 'SomePageController@index']);
However, I also have ajax calls at the same URL (using a request parameter called ajaxlike: some/page/?ajax=my_action
) that I want to hit methods on my controller:
但是,我也在同一个 URL 上进行了 ajax 调用(使用一个名为ajax的请求参数,例如 : some/page/?ajax=my_action
),我想在我的控制器上点击这些方法:
index already routes: 'SomePageController@index' ajax = my_action needs to route: 'SomePageController@ajaxMyAction' ajax = my_other_action needs to route: 'SomePageController@ajaxMyOtherAction' ajax = blah_blah needs to route: 'SomePageController@ajaxBlahBlah ...
What's the elegant solution to setting this up in my routes.phpfile?
在我的routes.php文件中设置它的优雅解决方案是什么?
采纳答案by prograhammer
After inspection of Laravel's Http Request and Route classes, I found the route()and setAction()methods could be useful.
在检查了 Laravel 的 Http Request 和 Route 类之后,我发现route()和setAction()方法可能很有用。
So I created a middleware to handle this:
所以我创建了一个中间件来处理这个:
<?php namespace App\Http\Middleware;
class Ajax {
public function handle($request, Closure $next)
{
// Looks for the value of request parameter called "ajax"
// to determine controller's method call
if ($request->ajax()) {
$routeAction = $request->route()->getAction();
$ajaxValue = studly_case($request->input("ajax"));
$routeAction['uses'] = str_replace("@index", "@ajax".$ajaxValue, $routeAction['uses']);
$routeAction['controller'] = str_replace("@index", "@ajax".$ajaxValue, $routeAction['controller']);
$request->route()->setAction($routeAction);
}
return $next($request);
}
}
Now my route looks like:
现在我的路线看起来像:
Route::any('some/page/', ['as' => 'some-page', 'middleware'=>'ajax', 'uses' => 'SomePageController@index']);
And correctly hits my controller methods (without disturbing Laravel's normal flow):
并正确命中我的控制器方法(不干扰 Laravel 的正常流程):
<?php namespace App\Http\Controllers;
class SomePageController extends Controller {
public function index()
{
return view('some.page.index');
}
public function ajaxMyAction(Requests\SomeFormRequest $request){
die('Do my action here!');
}
public function ajaxMyOtherAction(Requests\SomeFormRequest $request){
die('Do my other action here!');
}
...
I think this is a fairly clean solution.
我认为这是一个相当干净的解决方案。
回答by Alexandre Butynski
You can't make this dispatch in the routing layer if you keep the same URL. You have two options :
如果您保持相同的 URL,则无法在路由层进行此分派。你有两个选择:
Use different routes for your AJAX calls. For example, you can prefix all your ajax calls by
/api
. This is a common way :Route::group(['prefix' => 'api'], function() { Route::get('items', function() { // }); });
If the only different thing is your response format. You can use a condition in your controller. Laravel provides methods for that, for example :
public function index() { $items = ...; if (Request::ajax()) { return Response::json($items); } else { return View::make('items.index'); } }
为您的 AJAX 调用使用不同的路由。例如,您可以在所有 ajax 调用前加上
/api
. 这是一种常见的方式:Route::group(['prefix' => 'api'], function() { Route::get('items', function() { // }); });
如果唯一不同的是您的响应格式。您可以在控制器中使用条件。Laravel 为此提供了方法,例如:
public function index() { $items = ...; if (Request::ajax()) { return Response::json($items); } else { return View::make('items.index'); } }
You can read this http://laravel.com/api/5.0/Illuminate/Http/Request.html#method_ajaxand this http://laravel.com/docs/5.0/routing#route-groupsif you want more details.
如果您需要更多详细信息,您可以阅读此http://laravel.com/api/5.0/Illuminate/Http/Request.html#method_ajax和此http://laravel.com/docs/5.0/routing#route-groups。