[英]Laravel passing parameters to controller from route
I am creating an API for my Laravel project. 我正在为Laravel项目创建一个API。 I have included my API authentication directly in my route.php file.
我已经将我的API身份验证直接包含在route.php文件中。 After validation passes, I want to be able to pass my $api parameter to the controller itself to use.
验证通过后,我希望能够将$ api参数传递给控制器本身以使用。 As you will see below I appended ->with() after my Route:post call but that is failing.
正如您将在下面看到的那样,我在Route:post调用之后附加了-> with(),但这失败了。 How can I pass $api to my control?
如何将$ api传递给我的控件?
Thanks in advance! 提前致谢!
Route::group(array('prefix' => 'api/1.0.0'), function()
{
$api = Api::checkCredentials(Input::get('username'), Input::get('api_key'));
if($api)
{
Route::post('beacons/fetch', 'ApiController@fetchBeacons')->with('api', $api);
}
else
{
return Response::json(array(
'error' => true,
'output' => 'Invalid API credentials'),
200
);
}
});
You may use before
filter, for example: 您可以
before
过滤器before
使用,例如:
Route::group(array('prefix' => 'api/1.0.0'), function()
{
Route::post(
'beacons/fetch',
array('before' => 'checkUser', 'uses' => 'ApiController@fetchBeacons')
);
});
Declare the checkUser
filter which will be executed before the route
is dispatched: 声明将在分派
route
之前执行的checkUser
过滤器:
Route::filter('checkUser', function($route, $request) {
$inputs = Input::only(array('username', 'api_key'));
$api = app('Api')->checkCredentials($inputs);
if(!$api) {
return Response::json(array(
'error' => true,
'output' => 'Invalid API credentials'),
200
);
}
else {
Input::merge(array('api' => $api));
}
});
So now, you may access the api
in your controller using this: 因此,现在,您可以使用以下命令访问控制器中的
api
:
$api = Input::get('api');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.