[英]How to pass extra parameters to controller from Laravel route
I'm trying to handle basic validation of my API calls in the Laravel's routes. 我正在尝试对Laravel路线中的API调用进行基本验证。 Here is what I want to achieve:
这是我要实现的目标:
Route::group(['prefix' => 'api/v1/properties/'], function () {
Route::get('purchased', 'PropertiesController@getPropertyByProgressStatus', function () {
//pass variable x = 1 to the controller
});
Route::get('waiting', 'PropertiesController@getPropertyByProgressStatus', function () {
//pass variable x = 2 to the controller
});
}); });
Long story short, depending on the segment of the URI after api/v1/properties/ I want to pass a different parameter to the controller. 长话短说,取决于api / v1 / properties /之后的URI段,我想将其他参数传递给控制器。 Is there a way to do that?
有没有办法做到这一点?
I was able to get it to work with the following route.php file: 我能够将其与以下route.php文件一起使用:
Route::group(['prefix' => 'api/v1/properties/'], function () {
Route::get('purchased', [
'uses' => 'PropertiesController@getPropertyByProgressStatus', 'progressStatusId' => 1
]);
Route::get('remodeled', [
'uses' => 'PropertiesController@getPropertyByProgressStatus', 'progressStatusId' => 1
]);
Route::get('pending', [
'uses' => 'PropertiesController@getPropertyByProgressStatus', 'progressStatusId' => 3
]);
Route::get('available', [
'uses' => 'PropertiesController@getPropertyByProgressStatus', 'progressStatusId' => 4
]);
Route::get('unavailable', [
'uses' => 'PropertiesController@getPropertyByProgressStatus', 'progressStatusId' => 5
]);
});
and the following code in the controller: 以及控制器中的以下代码:
public function getPropertyByProgressStatus(\\Illuminate\\Http\\Request $request) { 公共函数getPropertyByProgressStatus(\\ Illuminate \\ Http \\ Request $ request){
$action = $request->route()->getAction();
print_r($action);
Pretty much the $action variable is going to let me access the extra parameter that I passed from the route. $ action变量几乎可以让我访问从路由传递的额外参数。
I think that you can do it directly in the controller and receiving the value as a parameter of your route: 我认为您可以直接在控制器中执行此操作,并将该值作为路由的参数接收:
First you need to specify the name of the parameter in the controller. 首先,您需要在控制器中指定参数的名称。
Route::group(['prefix' => 'api/v1/properties/'], function ()
{
Route::get('{parameter}', PropertiesController@getPropertyByProgressStatus');
In this way, the getPropertyByProgressStatus method is going to receive this value, so in the controller: 这样,getPropertyByProgressStatus方法将接收此值,因此在控制器中:
class PropertiesController{
....
public function getPropertyByProgressStatus($parameter)
{
if($parameter === 'purchased')
{
//do what you need
}
elseif($parameter === 'waiting')
{
//Do another stuff
}
....
}
I hope it helps to solve your issue. 我希望它有助于解决您的问题。
Take a view for this courses: Learn Laravel or Create a RESTful API with Laravel 查看以下课程: 学习Laravel或使用Laravel创建RESTful API
Best wishes. 最好的祝愿。
----------- Edited --------------- You can redirect to the route that you want: -----------已编辑---------------您可以重定向到所需的路由:
Route::group(['prefix' => 'api/v1/properties/'], function () {
Route::get('purchased', function () {
return redirect('/api/v1/properties/purchased/valueToSend');
});
Route::get('waiting', function () {
return redirect('/api/v1/properties/waiting/valueToSend');
});
Route::get('purchased/{valueToSend}', PropertiesController@getPropertyByProgressStatus);
});
Route::get('waiting/{valueToSend}', PropertiesController@getPropertyByProgressStatus);
});
});
The last two routes response to the redirections and send that value to the controller as a parameter, is the most near that I think to do this directly from the routes. 最后两条路由响应重定向并将该值作为参数发送给控制器,这是我认为最直接从路由执行此操作的最接近的路由。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.