• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

查询字符串PARAMS传递把在Laravel4路线

用户头像
it1352
帮助1

问题说明

我写在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.

正确答案

#1

您可能会想在你的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
系列文章
更多 icon
同类精品
更多 icon
继续加载