php 得到“?” Laravel 中的变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/15081090/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting GET "?" variable in laravel
提问by justmyfreak
Hello I'm creating API using REST and Laravel following thisarticle.
你好我使用REST和Laravel下列寻衅API此文章。
Everything works well as expected.
一切都按预期运行。
Now, I want to map GET request to recognise variable using "?".
现在,我想映射 GET 请求以使用“?”识别变量。
For example: domain/api/v1/todos?start=1&limit=2
例如: domain/api/v1/todos?start=1&limit=2
Below is the content of my routes.php :
以下是我的 routes.php 的内容:
Route::any('api/v1/todos/(:num?)', array(
    'as'   => 'api.todos',
    'uses' => 'api.todos@index'
));
my controllers/api/todos.php :
我的控制器/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);   
            }
        }
    }
}
How to recognise get parameter using "?" ?
如何使用“?”识别获取参数 ?
回答by adamdunson
Take a look at the $_GETand $_REQUESTsuperglobals. Something like the following would work for your example:
看看$_GET和$_REQUEST超全局变量。类似以下内容适用于您的示例:
$start = $_GET['start'];
$limit = $_GET['limit'];
EDIT
编辑
According to this post in the laravel forums, you need to use Input::get(), e.g.,
根据laravel论坛中的这篇文章,您需要使用Input::get(),例如,
$start = Input::get('start');
$limit = Input::get('limit');
See also: http://laravel.com/docs/input#input
回答by peterchaula
I haven't tested on other Laravel versions but on 5.3-5.8 you reference the query parameter as if it were a member of the Requestclass.
我还没有在其他 Laravel 版本上测试过,但是在 5.3-5.8 上,您引用查询参数就好像它是Requestclass.
1. Url
1. 网址
http://example.com/path?page=2
http://example.com/path?page=2
2. In a route callback or controller action using magic method Request::__get()
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. Default values
3. 默认值
We can also pass in a default value which is returned if a parameter doesn't exist. It's much cleaner than a ternary expression that you'd normally use with the request globals
我们还可以传入一个默认值,如果参数不存在则返回该默认值。它比通常用于请求全局变量的三元表达式要干净得多
   //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. Using request function
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'];
The defaultparameter is optional therefore one can omit it
该默认参数是可选的,因此可以忽略它
5. Using Request::query()
5. 使用 Request::query()
While the input method retrieves values from entire request payload (including the query string), the query method will only retrieve values from the query string
虽然输入方法从整个请求负载(包括查询字符串)中检索值,但查询方法只会从查询字符串中检索值
//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. Using the Request facade
6. 使用请求门面
$page = Request::get('page');
//with a default value
$page = Request::get('page', 1);
You can read more in the official documentation https://laravel.com/docs/5.8/requests
您可以在官方文档https://laravel.com/docs/5.8/requests 中阅读更多内容
回答by Fil
We have similar situation right now and as of this answer, I am using laravel 5.6 release.
我们现在有类似的情况,截至这个答案,我使用的是 laravel 5.6 版本。
I will not use your example in the question but mine, because it's related though.
我不会在问题中使用您的示例,而是使用我的示例,因为它是相关的。
I have route like this:
我有这样的路线:
Route::name('your.name.here')->get('/your/uri', 'YourController@someMethod');
Then in your controller method, make sure you include
然后在你的控制器方法中,确保你包括
use Illuminate\Http\Request;
and this should be above your controller, most likely a default, if generated using php artisan, now to get variable from the url it should look like this:
这应该在您的控制器上方,很可能是默认值,如果使用php artisan, 现在从 url 获取变量,它应该如下所示:
  public function someMethod(Request $request)
  {
    $foo = $request->input("start");
    $bar = $request->input("limit");
    // some codes here
  }
Regardless of the HTTP verb, the input()method may be used to retrieve user input.
不管 HTTP 动词是什么,input()方法都可以用来检索用户输入。
https://laravel.com/docs/5.6/requests#retrieving-input
https://laravel.com/docs/5.6/requests#retrieving-input
Hope this help.
希望这有帮助。
回答by Ronak Dattani
This is the best practice. This way you will get the variables from GET method as well as POST method
这是最佳做法。这样你就可以从 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);
    }
回答by malhal
Query params are used like this:
查询参数的使用方式如下:
use Illuminate\Http\Request;
class MyController extends BaseController{
    public function index(Request $request){
         $param = $request->query('param');
    }
回答by Waqas Bukhary
In laravel 5.3$start = Input::get('start');returns NULL
在 Laravel 5.3 中$start = Input::get('start');返回NULL
To solve this
为了解决这个
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'];
回答by Fellipe Sanches
It is not very nice to use native php resources like $_GETas Laravel gives us easy ways to get the variables. As a matter of standard, whenever possible use the resources of the laravel itself instead of pure PHP.
使用原生 php 资源不是很好,$_GET因为 Laravel 为我们提供了获取变量的简单方法。作为一个标准,尽可能使用 laravel 本身的资源而不是纯 PHP。
There is at least two modes to get variables by GET in Laravel ( Laravel 5.x or greater):
在 Laravel(Laravel 5.x 或更高版本)中通过 GET 获取变量至少有两种模式:
Mode 1
模式一
Route:
路线:
Route::get('computers={id}', 'ComputersController@index');
Request (POSTMAN or client...):
请求(邮递员或客户端...):
http://localhost/api/computers=500
Controler - You can access the {id}paramter in the Controlller by:
控制器 - 您可以通过以下方式访问{id}控制器中的参数:
public function index(Request $request, $id){
   return $id;
}
Mode 2
模式2
Route:
路线:
Route::get('computers', 'ComputersController@index');
Request (POSTMAN or client...):
请求(邮递员或客户端...):
http://localhost/api/computers?id=500
Controler - You can access the ?idparamter in the Controlller by:
控制器 - 您可以通过以下方式访问?id控制器中的参数:
public function index(Request $request){
   return $request->input('id');
}
回答by The Dead Guy
In laravel 5.3
在 Laravel 5.3
I want to show the get param in my view
我想在我的视图中显示 get 参数
Step 1 : my route
第 1 步:我的路线
Route::get('my_route/{myvalue}', 'myController@myfunction');
Step 2 : Write a function inside your controller
第 2 步:在控制器中编写一个函数
public function myfunction($myvalue)
{
    return view('get')->with('myvalue', $myvalue);
}
Now you're returning the parameter that you passed to the view
现在您要返回传递给视图的参数
Step 3 : Showing it in my View
第 3 步:在我的视图中显示它
Inside my view you i can simply echo it by using
在我的视图中,你可以简单地通过使用来回应它
{{ $myvalue }}
So If you have this in your url
所以如果你的网址中有这个
http://127.0.0.1/yourproject/refral/[email protected]
http://127.0.0.1/yourproject/refral/[email protected]
Then it will print [email protected] in you view file
然后它会在你查看文件中打印 [email protected]
hope this helps someone.
希望这有助于某人。

