[英]How to build a Laravel route that requires a specific URL query parameter?
Let's say I have URLs like this: 假设我有这样的网址:
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
}
}
And here's my routes.php I'm working on: 这是我的routes.php我正在努力:
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'));
What can I do in my second route to require a URL query parameter ?data
and furthermore require it is set to data=refresh
? 在我的第二个路径中我需要做什么才能要求URL查询参数 ?data
并且还需要将其设置为 data=refresh
? And how do I ensure it doesn't conflict with the other route? 我如何确保它不与其他路线冲突?
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). 我在适当的时候实现了漂亮的URL / slugs,但是我也认为在很多情况下查询参数更清晰,更清晰(即让用户清楚地了解页面URL的哪一部分用于过滤数据) datagrid ...并确保用户可以删除参数而不会导致页面中断或丢失)。 Google does this themselves, as well as many other reputable sites. 谷歌自己以及许多其他声誉良好的网站都是这样做的。
Note: I have applied an ajax route filter to the second route. 注意:我已将ajax路由过滤器应用于第二个路由。 I've also set the route to point towards the dataRefresh method in my controller. 我还设置了路由指向我的控制器中的dataRefresh方法。
This is as far as I've got. 这是我所拥有的。 Any ideas? 有任何想法吗?
Laravel doesn't use the query part of a uri
for routing, for localhost/admin/users?data=refresh
you may use something like this: Laravel不使用uri
的查询部分进行路由,对于localhost/admin/users?data=refresh
你可以使用这样的东西:
Route::get('admin/users', function(){
$data = Input::get('data');
});
You can make a request to the route using localhost/admin/users?data=refresh
. 您可以使用localhost/admin/users?data=refresh
向路由发出请求。 You can declare your route
like this: 您可以这样声明您的route
:
Route::get('admin/users' , array('before' => 'ajax:data', 'as' => 'admin.users', 'uses' => 'Admin\Users\UsersController@dataRefresh'));
Here, refresh
is passed to route filter and is available in third argument ( $param
) so you can retrieve refresh
in $param
. 这里, refresh
传递给路由过滤器,并在第三个参数( $param
)中可用,因此您可以在$param
检索refresh
。 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'
});
I think the closest you will get to what you want is Route::input
. 我认为你最接近你想要的是Route::input
。
http://laravel.com/docs/routing#route-parameters http://laravel.com/docs/routing#route-parameters
Accessing A Route Parameter Value 访问路径参数值
If you need to access a route parameter value outside of a route, you may use the
Route::input
method: 如果需要访问路由之外的路由参数值,可以使用Route::input
方法:
Route::filter('foo', function()
{
if (Route::input('id') == 1)
{
//
}
});
I would not personally do it this way myself, I would just check for the parameter within the controller and if it matches then perform the refresh or use a admin/users/refresh
route instead. 我不会亲自这样做,我只是检查控制器中的参数,如果匹配,则执行刷新或使用admin/users/refresh
路径。
Route::get('admin/users/{data?}' , array('before' => 'ajax', 'as' => 'admin.users', 'uses' => 'Admin\Users\UsersController@dataRefresh'));
And you can 你可以
/admin/users/refresh /管理/用户/刷新
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.