Php 得到得到&引用;拉维变量
您好,在下面的文章中,我正在使用REST和Laravel创建一个API 一切正常 现在,我想映射一个GET请求,用“?”来识别一个变量 例如:Php 得到得到&引用;拉维变量,php,laravel,rest,restapi,Php,Laravel,Rest,Restapi,您好,在下面的文章中,我正在使用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' )); class Api_T
domain/api/v1/todos?start=1&limit=2
下面是我的routes.php的内容:
Route::any('api/v1/todos/(:num?)', array(
'as' => 'api.todos',
'uses' => 'api.todos@index'
));
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);
}
}
}
}
我的控制器/api/todos.php
:
Route::any('api/v1/todos/(:num?)', array(
'as' => 'api.todos',
'uses' => 'api.todos@index'
));
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);
}
}
}
}
如何使用“?”获取参数?查看和超全局。以下类似内容适用于您的示例:
$start = $_GET['start'];
$limit = $_GET['limit'];
编辑
根据,您需要使用Input::get()
,例如
$start = Input::get('start');
$limit = Input::get('limit');
另请参见:查询参数的使用方式如下:
use Illuminate\Http\Request;
class MyController extends BaseController{
public function index(Request $request){
$param = $request->query('param');
}
Route::name('your.name.here')->get('/your/uri', 'YourController@someMethod');
public function someMethod(Request $request)
{
$foo = $request->input("start");
$bar = $request->input("limit");
// some codes here
}
这是最好的做法。通过这种方式,您将从
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);
}
在laravel5.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'];
我没有在其他Laravel版本上进行测试,但在5.3-5.8中,您引用查询参数时,将其视为请求
类
的成员
1.网址
http://example.com/path?page=2
2.在使用magic方法请求的路由回调或控制器操作中::\uu get()
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.使用请求函数
默认值参数是可选的,因此可以忽略它
5.使用Request::query()
当输入方法从整个请求负载(包括查询字符串)中检索值时,查询方法将仅从查询字符串中检索值
6.使用请求
您可以在laravel 5.3的官方文档中阅读更多内容
我想在视图中显示get参数
第一步:我的路线
Route::get('my_route/{myvalue}', 'myController@myfunction');
步骤2:在控制器内编写函数
public function myfunction($myvalue)
{
return view('get')->with('myvalue', $myvalue);
}
现在返回传递给视图的参数
步骤3:在我的视图中显示它
在我看来,我可以简单地通过使用
{{ $myvalue }}
所以如果你的url里有这个
然后它会打印出来this@that.com在您的视图文件中
希望这对其他人有所帮助。我们现在也有类似的情况,在回答这个问题时,我正在使用laravel 5.6版本
在这个问题上,我不会使用你的例子,而是我的,因为它是相关的
我有这样的路线:
use Illuminate\Http\Request;
class MyController extends BaseController{
public function index(Request $request){
$param = $request->query('param');
}
Route::name('your.name.here')->get('/your/uri', 'YourController@someMethod');
public function someMethod(Request $request)
{
$foo = $request->input("start");
$bar = $request->input("limit");
// some codes here
}
然后在控制器方法中,确保包括
use Illuminate\Http\Request;
这应该在您的控制器上方,很可能是默认值,如果使用php artisan
生成,现在要从url获取变量,它应该如下所示:
use Illuminate\Http\Request;
class MyController extends BaseController{
public function index(Request $request){
$param = $request->query('param');
}
Route::name('your.name.here')->get('/your/uri', 'YourController@someMethod');
public function someMethod(Request $request)
{
$foo = $request->input("start");
$bar = $request->input("limit");
// some codes here
}
不管HTTP动词是什么,都可以使用input()方法检索用户输入
希望有帮助。使用本机php资源(如$\u GET
)不是很好,因为Laravel为我们提供了获取变量的简单方法。作为标准,只要可能,就使用laravel本身的资源,而不是纯PHP
至少有两种模式可以通过Laravel中的get获取变量(
Laravel 5.x或更高版本):
模式1
路线:
Route::get('computers={id}', 'ComputersController@index');
Route::get('computers', 'ComputersController@index');
请求(邮递员或客户…):
Controller-您可以通过以下方式访问Controller中的{id}
参数:
public function index(Request $request, $id){
return $id;
}
public function index(Request $request){
return $request->input('id');
}
模式2
路线:
Route::get('computers={id}', 'ComputersController@index');
Route::get('computers', 'ComputersController@index');
请求(邮递员或客户…):
控制器-您可以通过以下方式访问控制器中的?id
参数:
public function index(Request $request, $id){
return $id;
}
public function index(Request $request){
return $request->input('id');
}
通过使用Input::get、Input::has、Input::all、Input::file等,您可以获得更多的功能。我唯一建议您使用超级全局文件的时候是在上载一组文件时,您确实需要使用$\ U文件。对于2016年阅读此文件的任何人,在Laravel>v5.0中使用输入的正确方法是:这不是Laravel获取请求参数的方法请指定Input
的来源您使用的不是get参数而是URL片段。总有一天需要URL片段的人会出现在这里。尝试总比不尝试好。同样感谢您提供的信息和downvote:XY您甚至没有解释您使用的不是get参数。这就是否决票的目的。这并不能回答问题unfortunately@The死家伙太棒了,这对我来说是个绝妙的把戏