如何查询字符串PARAMS传递给在Laravel4路线 [英] How to pass query string params to routes in Laravel4
问题描述
我写在Laravel 4.我想查询字符串参数传递给我的控制器的API。具体来说,我想允许这样的事情:
I'm writing an api in Laravel 4. I'd like to pass query string parameters to my controllers. Specifically, I want to allow something like this:
api/v1/account?fields=email,acct_type
,其中查询参数是一起有一个签名这样的路由控制器的方法传递:
where the query params are passed along to the routed controller method which has a signature like this:
public function index($cols)
在routes.php文件路线是这样的:
The route in routes.php looks like this:
Route::get('account', 'AccountApiController@index');
我是手动指定我的清晰度和灵活性(所有路由,而不是使用路线::控制器
或路线::资源
)和我总是路由到一个控制器和控制方法。
I am manually specifying all my routes for clarity and flexibility (rather than using Route::controller
or Route::resource
) and I am always routing to a controller and method.
我所做的'场'查询字符串元素隔离到一个数组 $ COLS
A(全局)的辅助功能,但调用每个控制器ISN的每个方法里面的那个函数T干燥。我怎样才能有效地 $ COLS
变量传递给我所有的路线::获得
路线控制器的方法呢?或者更一般地说,我怎么能有效地通过路由(或路由组)控制器方法传递从查询字符串的一个或多个额外的参数?我在考虑使用过滤器,但似乎有点假标签。
I made a (global) helper function that isolates the 'fields' query string element into an array $cols
, but calling that function inside every method of every controller isn't DRY. How can I effectively pass the $cols
variable to all of my Route::get
routes' controller methods? Or, more generally, how can I efficiently pass one or more extra parameters from a query string through a route (or group of routes) to a controller method? I'm thinking about using a filter, but that seems a bit off-label.
推荐答案
您可能会想在你的BaseController来实现这一点。这是可能的解决方案之一:
You might want to implement this in your BaseController. This is one of the possible solutions:
class BaseController extends Controller {
protected $fields;
public function __construct(){
if (Input::has('fields')) {
$this->fields = Input::get('fields');
}
}
}
之后,$领域可能在每艘BaseController孩子航线进行访问:
After that $fields could be accessed in every route which is BaseController child:
class AccountApiController extends \BaseController {
public function index()
{
dd($this->fields);
}
}
这篇关于如何查询字符串PARAMS传递给在Laravel4路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!