查询字符串PARAMS传递把在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);
}
}
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhcfefja
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
检查邮件是否发送成功
it1352 07-25 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
Android微调工具-删除当前选择
it1352 06-20 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20 -
希伯来语的空格句子标记化错误
it1352 06-22