【问题标题】:Getting GET "?" Variable in Laravel获取“?” Laravel 中的变量
【发布时间】:2013-02-11 10:19:27
【问题描述】:

您好,我正在按照this 文章使用 REST 和 Laravel 创建 API。

一切都按预期进行。

现在,我想映射一个 GET 请求以使用“?”识别变量。

例如:domain/api/v1/todos?start=1&limit=2

以下是我routes.php的内容:

Route::any('api/v1/todos/(:num?)', array(
    'as'   => 'api.todos',
    'uses' => 'api.todos@index'
));

我的controllers/api/todos.php

class Api_Todos_Controller extends Base_Controller {

    public $restful = true;

    public function get_index($id = null) {
        if(is_null($id)) {
            return Response::eloquent(Todo::all(1));

        } else {
            $todo = Todo::find($id);
            if (is_null($todo)) {
                return Response::json('Todo not found', 404);
            } else {
                return Response::eloquent($todo);   
            }
        }
    }
}

如何使用“?”获取参数?

【问题讨论】:

    标签: php laravel rest


    【解决方案1】:

    看看$_GET$_REQUEST 超全局变量。以下内容适用于您的示例:

    $start = $_GET['start'];
    $limit = $_GET['limit'];
    

    编辑

    根据this post in the laravel forums,需要使用Input::get(),例如,

    $start = Input::get('start');
    $limit = Input::get('limit');
    

    另见:http://laravel.com/docs/input#input

    【讨论】:

    • 您可以通过使用 Input::get、Input::has、Input::all、Input::file 等获得更多功能。我唯一推荐使用超级全局的时候是上传文件数组,那么您确实需要使用 $_FILES。
    • 对于 2016 年阅读本文的任何人,在 Laravel >v5.0 中使用 Input 的正确方法是 stackoverflow.com/a/34642837/1067293
    • 这不是 Laravel 获取请求参数的方式
    • 请注明Input的出处
    • 看起来你应该使用$request->input()laravel.com/docs/8.x/requests#input,而不是Input。 404s 以上答案中的共享链接对我来说:laravel.com/docs/input#input
    【解决方案2】:

    在 5.3-8.0 上,您引用查询参数,就好像它是 Request class 的成员一样。

    1。网址

    http://example.com/path?page=2

    2。在使用魔术方法 Request::__get() 的路由回调或控制器操作中

    Route::get('/path', function(Request $request){
     dd($request->page);
    }); 
    
    //or in your controller
    public function foo(Request $request){
     dd($request->page);
    }
    
    //NOTE: If you are wondering where the request instance is coming from, Laravel automatically injects the request instance from the IOC container
    //output
    "2"
    

    ###3。默认值 我们还可以传入一个默认值,如果参数不存在则返回该值。它比您通常与请求全局变量一起使用的三元表达式要干净得多

       //wrong way to do it in Laravel
       $page = isset($_POST['page']) ? $_POST['page'] : 1; 
       
       //do this instead
       $request->get('page', 1);
      
       //returns page 1 if there is no page
       //NOTE: This behaves like $_REQUEST array. It looks in both the
       //request body and the query string
       $request->input('page', 1);
    

    ###4。使用请求函数

    $page = request('page', 1);
    //returns page 1 if there is no page parameter in the query  string
    //it is the equivalent of
    $page = 1; 
    if(!empty($_GET['page'])
       $page = $_GET['page'];
    

    default 参数是可选的,因此可以省略它

    ###5。使用 Request::query()

    虽然输入方法从整个请求负载(包括查询字符串)中检索值,但查询方法只会从查询字符串中检索值

    //this is the equivalent of retrieving the parameter
    //from the $_GET global array
    $page = $request->query('page');
    
    //with a default
    $page = $request->query('page', 1);
     
    

    ###6。使用请求facade

    $page = Request::get('page');
    
    //with a default value
    $page = Request::get('page', 1);
    

    您可以在官方文档https://laravel.com/docs/5.8/requests阅读更多内容

    【讨论】:

      【解决方案3】:

      我们现在有类似的情况,在这个答案中,我使用的是 laravel 5.6 版本。

      我不会在问题中使用您的示例,而是使用我的示例,因为它是相关的。

      我有这样的路线:

      Route::name('your.name.here')->get('/your/uri', 'YourController@someMethod');
      

      然后在你的控制器方法中,确保你包含

      use Illuminate\Http\Request;
      

      这应该在您的控制器之上,很可能是默认值,如果使用 php artisan 生成,现在从 url 获取变量应该如下所示:

        public function someMethod(Request $request)
        {
          $foo = $request->input("start");
          $bar = $request->input("limit");
      
          // some codes here
        }
      

      不管 HTTP 动词是什么,input() 方法都可用于检索用户输入。

      https://laravel.com/docs/5.6/requests#retrieving-input

      希望对您有所帮助。

      【讨论】:

        【解决方案4】:

        这是最佳做法。这样你就可以得到变量 GET 方法和 POST 方法

            public function index(Request $request) {
                    $data=$request->all();
                    dd($data);
            }
        //OR if you want few of them then
            public function index(Request $request) {
                    $data=$request->only('id','name','etc');
                    dd($data);
            }
        //OR if you want all except few then
            public function index(Request $request) {
                    $data=$request->except('__token');
                    dd($data);
            }
        

        【讨论】:

          【解决方案5】:

          查询参数是这样使用的:

          use Illuminate\Http\Request;
          
          class MyController extends BaseController{
          
              public function index(Request $request){
                   $param = $request->query('param');
              }
          

          【讨论】:

            【解决方案6】:

            在 laravel 5.3 $start = Input::get('start'); 返回 NULL

            解决这个问题

            use Illuminate\Support\Facades\Input;
            
            //then inside you controller function  use
            
            $input = Input::all(); // $input will have all your variables,  
            
            $start = $input['start'];
            $limit = $input['limit'];
            

            【讨论】:

              【解决方案7】:

              在 laravel 5.3 中

              我想在我的视图中显示 get 参数

              第 1 步:我的路线

              Route::get('my_route/{myvalue}', 'myController@myfunction');
              

              第 2 步:在控制器中编写一个函数

              public function myfunction($myvalue)
              {
                  return view('get')->with('myvalue', $myvalue);
              }
              

              现在您正在返回您传递给视图的参数

              第 3 步:在我的视图中显示它

              在我看来,你可以简单地通过使用来回应它

              {{ $myvalue }}
              

              所以如果你的网址中有这个

              http://127.0.0.1/yourproject/refral/this@that.com

              然后它将在您的查看文件中打印 this@that.com

              希望这对某人有所帮助。

              【讨论】:

              • 你使用的不是get参数而是URL片段。
              • 有一天,需要 url 片段的人会在这里。尝试总比不尝试好。非常感谢您的信息和downvote:x
              • 您甚至没有解释您使用的不是get参数。这就是反对票的目的。
              • 不幸的是,这并没有回答问题
              • @The Dead Guy 非常棒,这对我来说是个绝妙的技巧,点赞:D
              【解决方案8】:

              使用像 $_GET 这样的原生 php 资源并不是很好,因为 Laravel 为我们提供了获取变量的简单方法。作为标准,尽可能使用 laravel 本身的资源,而不是纯 PHP。

              在 Laravel 中通过 GET 获取变量至少有两种模式( Laravel 5.x 或更高版本):

              模式 1

              路线:

              Route::get('computers={id}', 'ComputersController@index');
              

              请求(邮递员或客户端...):

              http://localhost/api/computers=500
              

              控制器 - 您可以通过以下方式访问控制器中的{id} 参数:

              public function index(Request $request, $id){
                 return $id;
              }
              

              模式 2

              路线:

              Route::get('computers', 'ComputersController@index');
              

              请求(邮递员或客户端...):

              http://localhost/api/computers?id=500
              

              控制器 - 您可以通过以下方式访问控制器中的?id 参数:

              public function index(Request $request){
                 return $request->input('id');
              }
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2014-04-05
                • 1970-01-01
                • 2016-05-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2015-11-02
                • 2018-07-15
                相关资源
                最近更新 更多