laravel框架——请求实例 之 获取请求数据

路由:

//获取单个参数
Route::get('/request','RequestController@index');
//获取路径及url
Route::get('/request/url','RequestController@getUrl');
//获取请求方式
Route::get('/request/getmethod','RequestController@getMethod');
//获取请求参数的值,如果有则返回,如果没有则返回默认值 || 判断是否存在
Route::get('/request/input-data','RequestController@getInputData');
//获取上一次请求的内容
Route::get('/request/last-request','RequestController@getLastRequest');
Route::get('/request/current-request','RequestController@getCurrentRequest');

控制器:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Response;

class RequestController extends Controller
{
    public function index(Request $request)
    {
        //接收指定参数
        $test = $request->input('test');
        echo $test;
    }

    function getUrl(Request $request)
    {
        //获取路径
        if (!$request->is('request/*')) {
            abort(404);
        }
        $uri = $request->path();
        $url = $request->url();
        echo $uri;
        echo '
'
; echo $url; } function getMethod(Request $request) { //get请求不能访问 if (!$request->isMethod('get')) { abort(404); } $method = $request->method(); echo $method; } function getInputData(Request $request) { //获取GET方式传递的name参数,默认为LaravelAcademy $name = $request->input('name','LaravelAcademy'); //判断是否存在 if($request->has('name')){ echo 'name: ' .$name; }else{ echo $name; } //获取全部数据 $data = $request->all(); //获取指定数据 $only = $request->only('name','hello'); //获取排除内容 $except = $request->except('hello'); echo '
';
        var_dump($data,$only,$except);
    }

    public function getLastRequest(Request $request){
        $request->flash();//flash方法将请求数据暂时保存到session        return redirect('/request/current-request')->withInput();
    }

    public function getCurrentRequest(Request $request){
        $lastRequestData = $request->old(); //获取session中的数据
        echo '
';
        print_r($lastRequestData);
    }
}

 该文为学习笔记,原代码请查看:http://laravelacademy.org/post/606.html

你可能感兴趣的:(框架,PHP,学习笔记)