59

您好,我正在按照本文使用 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);   
            }
        }
    }
}

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

4

8 回答 8

72

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

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

编辑

根据laravel 论坛中的这篇文章,您需要使用Input::get(),例如,

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

另请参阅: http: //laravel.com/docs/input#input

于 2013-02-26T04:02:47.463 回答
43

在 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'];

默认参数是可选的,因此可以省略

###5。使用请求::查询()

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

//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。使用请求门面

$page = Request::get('page');

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

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

于 2017-01-24T20:07:55.483 回答
9

我们现在有类似的情况,在这个答案中,我使用的是 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

希望这有帮助。

于 2018-04-21T11:56:10.250 回答
7

这是最佳实践。这样,您将从 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);
    }
于 2016-10-01T19:10:59.367 回答
5

查询参数的使用如下:

use Illuminate\Http\Request;

class MyController extends BaseController{

    public function index(Request $request){
         $param = $request->query('param');
    }
于 2016-10-01T19:05:04.890 回答
3

在 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'];
于 2016-12-03T15:29:15.587 回答
0

在 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

希望这可以帮助某人。

于 2017-02-26T08:40:02.547 回答
0

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

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

模式一

路线:

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

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

http://localhost/api/computers=500

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

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

模式二

路线:

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

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

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

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

public function index(Request $request){
   return $request->input('id');
}
于 2020-04-22T15:13:29.010 回答