得到GET"?" laravel变量

jus*_*eak 43 php api rest laravel

你好我使用REST API创建并按照Laravel 文章.

一切都按预期运作良好.

现在,我想使用"?"映射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'
));
Run Code Online (Sandbox Code Playgroud)

我的控制器/ 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);   
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如何使用"?"识别get参数 ?

ada*_*son 69

看看$ _GET$ _REQUEST超级全局.以下内容适用于您的示例:

$start = $_GET['start'];
$limit = $_GET['limit'];
Run Code Online (Sandbox Code Playgroud)

编辑

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

$start = Input::get('start');
$limit = Input::get('limit');
Run Code Online (Sandbox Code Playgroud)

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

  • 你可以通过使用Input :: get,Input :: has,Input :: all,Input :: file等获得更多功能.我建议使用超级全局的唯一一次是当你上传一个文件数组然后你做需要使用$ _FILES. (6认同)
  • 对于2016年阅读此内容的人来说,在Laravel的> v5.0中使用Input的正确方法是http://stackoverflow.com/a/34642837/1067293 (3认同)
  • 这不是 Laravel 获取请求参数的方式 (3认同)

pet*_*ula 28

它非常简单 - 也适用于POST请求.我还没有在其他Laravel版本上测试过,但在5.3-5.7版本中你引用了查询参数,好像它是一个成员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"
Run Code Online (Sandbox Code Playgroud)

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);
Run Code Online (Sandbox Code Playgroud)

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'];
Run Code Online (Sandbox Code Playgroud)

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

5.使用Request :: query()

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

//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);
Run Code Online (Sandbox Code Playgroud)

6.使用请求外观

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

//with a default value
$page = Request::get('page', 1);
Run Code Online (Sandbox Code Playgroud)

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


Fil*_*Fil 7

我们现在有类似的情况,截至这个答案,我正在使用laravel 5.6发布.

我不会在问题中使用你的例子而是我的,因为它有关系.

我有这样的路线:

Route::name('your.name.here')->get('/your/uri', 'YourController@someMethod');
Run Code Online (Sandbox Code Playgroud)

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

use Illuminate\Http\Request;
Run Code Online (Sandbox Code Playgroud)

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

  public function someMethod(Request $request)
  {
    $foo = $request->input("start");
    $bar = $request->input("limit");

    // some codes here
  }
Run Code Online (Sandbox Code Playgroud)

无论HTTP动词如何,input()方法都可用于检索用户输入.

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

希望这有帮助.


Ron*_*ani 6

这是最佳做法。这样你就可以从 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);
    }
Run Code Online (Sandbox Code Playgroud)


mal*_*hal 5

查询参数的使用方式如下:

use Illuminate\Http\Request;

class MyController extends BaseController{

    public function index(Request $request){
         $param = $request->query('param');
    }
Run Code Online (Sandbox Code Playgroud)