如何构建需要特定URL查询参数的Laravel路由?
假设我有这样的网址:
- localhost/ admin/users/ < ---主要Admin用户"页面
- localhost/ admin/users/?data = refresh < ----从该页面发出的典型ajax请求
- localhost/admin/users/ <--- Main Admin Users page
- localhost/admin/users/?data=refresh <---- A typical ajax request made from that page
还有一个像这样的简单控制器:
And a simple controller like this:
class UsersController extends Controller {
public function index()
// call some services
// return a view
}
public function dataRefresh {
// call some services
// return some JSON
}
}
这是我正在使用的routes.php:
And here's my routes.php I'm working on:
Route::get('admin/users', array('as' => 'admin.users', 'uses' => 'Admin\Users\UsersController@index'));
Route::get('admin/users????' , array('before' => 'ajax', 'as' => 'admin.users', 'uses' => 'Admin\Users\UsersController@dataRefresh'));
在第二种方法中我需要URL查询参数该怎么做 ?data
并且还要求将其设置为 data=refresh
? strong>并且如何确保它与其他路线没有冲突?
What can I do in my second route to require a URL query parameter ?data
and furthermore require it is set to data=refresh
? And how do I ensure it doesn't conflict with the other route?
注意: 我知道某些人可能不会将其视为漂亮的网址".我会在适当的时候实现漂亮的网址/标签,但是我也认为在很多情况下查询参数更加清晰明了(例如,让用户清楚地了解页面网址的哪一部分用于过滤数据中的数据).数据网格...并确保用户可以删除参数而不会导致页面中断或丢失). Google以及许多其他知名网站都会这样做.
Note: I'm aware this may not be considered "pretty URL" by some. I do implement pretty URLs / slugs when appropriate, however I also think there are many cases where the query parameters are more clearer and cleaner (ie. give a user a clear understanding of what part of the page's URL is for filtering the data in a datagrid...and assures a user the parameters can be removed without causing the page to break or go missing). Google does this themselves, as well as many other reputable sites.
注意:我已将ajax路由过滤器应用于第二条路由.我还设置了路由,使其指向控制器中的dataRefresh方法.
Note: I have applied an ajax route filter to the second route. I've also set the route to point towards the dataRefresh method in my controller.
据我所知.有什么想法吗?
This is as far as I've got. Any ideas?
Laravel不使用uri
的查询部分进行路由,对于localhost/admin/users?data=refresh
,您可以使用以下内容:
Laravel doesn't use the query part of a uri
for routing, for localhost/admin/users?data=refresh
you may use something like this:
Route::get('admin/users', function(){
$data = Input::get('data');
});
您可以使用localhost/admin/users?data=refresh
来请求路线.您可以这样声明您的route
:
You can make a request to the route using localhost/admin/users?data=refresh
. You can declare your route
like this:
Route::get('admin/users' , array('before' => 'ajax:data', 'as' => 'admin.users', 'uses' => 'Admin\Users\UsersController@dataRefresh'));
在这里,refresh
被传递到路由过滤器,并且在第三个参数($param
)中可用,因此您可以在$param
中检索refresh
.创建如下所示的过滤器:
Here, refresh
is passed to route filter and is available in third argument ($param
) so you can retrieve refresh
in $param
. Create the filter as given below:
Route::filter('ajax', function($route, $request, $param){
// This will give query string 'refresh'
// if you passed it as http://domain.com?data=refresh
$data = $request->get($param);
// You can retrieve the $param, third argument
// if you pass a parameter, i.e. 'refresh'
// param will contain 'refresh'
});