#轻松学会Laravel-表单篇
###Controller之Request
StudentController.php
```
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Student;
class StudentController extends Controller
{
public function index(Request $request){
// 取值
echo $request->input('name', '默认值');
}
}
```
![](https://box.kancloud.cn/b41e186a6e70970e9789582bc297ea20_444x110.png)
![](https://box.kancloud.cn/5648ee46f4b79c9f97ea16f3cef543da_557x110.png)
StudentController.php
```
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Student;
class StudentController extends Controller
{
public function index(Request $request){
// 取值
if($request->has('name')){
echo $request->input('name');
}else{
echo '参数不存在';
}
}
}
```
![](https://box.kancloud.cn/106a8b19035eb2bac08e84a302963967_450x109.png)
![](https://box.kancloud.cn/30c384318f79e6b13c93668435e6290e_563x111.png)
StudentController.php
```
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Student;
class StudentController extends Controller
{
public function index(Request $request){
// 判断请求类型
echo $request->method();
echo '<br />';
if($request->isMethod('GET')){
echo 'Yes';
}else{
echo 'No';
}
echo '<br />';
// 判断是否是Ajax请求
if($request->ajax()){
echo 'Yes';
}else{
echo 'No';
}
echo '<br />';
// 判断请求的url是否符合特定的格式
if($request->is('student')){
echo 'Yes';
}else{
echo 'No';
}
echo '<br />';
if($request->is('student/*')){
echo 'Yes';
}else{
echo 'No';
}
echo '<br />';
// 获取当前的url
echo $request->url();
}
}
```
![](https://box.kancloud.cn/88d59186bc163abf897c3f631c1792e0_586x212.png)
###Controller之Session
`config/session.php` session配置文件
app/Http/Kernel.php
![](https://box.kancloud.cn/b79f4bb13578fb74c221d78a25fe2181_719x345.png)
routes.php
```
Route::group(['middleware' => ['web']], function () {
Route::get('student', [
'uses' => 'StudentController@index',
'as' => 'student'
]);
});
```
StudentController.php
```
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Student;
use Session;
class StudentController extends Controller
{
public function index(Request $request){
// HTTP request session()
$request->session()->put('name', 'zhangsan');
echo $request->session()->get('name');
echo '<br />';
// session()
session()->put('email', 'zhulinjie_cool@126.com');
echo session()->get('email');
echo '<br />';
// Session
Session::put('address', 'beijing');
echo Session::get('address');
echo '<br />';
// 不存在取默认值
echo Session::get('location', 'beijing chaoyang');
echo '<br />';
// 以数组的形式存储数据
Session::put(['key1'=>'value1']);
echo Session::get('key1', 'default');
// 把数据存放到Session的数组中
Session::push('arr1', 'value1');
Session::push('arr1', 'value2');
$res = Session::get('arr1');
echo '<pre>';
print_r($res);
echo '<br />';
// 取出数据并删除
Session::put('key2', 'value2');
echo Session::pull('key2', 'default');
echo '<br />';
echo Session::pull('key2', 'default');
// 获取session所有的数据
// $res = Session::all();
// echo '<pre>';
// print_r($res);
echo '<br />';
// 判断session中某个key是否存在
if(Session::has('key1')){
echo 'Yes';
}else{
echo 'No';
}
echo '<br />';
// 暂存数据
Session::flash('key3', 'value3');
echo Session::get('key3');
echo '<br />';
// 删除session中指定的key的值
// Session::forget('key1');
// 清空所有session信息
Session::flush();
}
}
```
![](https://box.kancloud.cn/02bccca0d60031501f55d18a3aa3e3a3_580x354.png)
###Controller之Response
routes.php
```
Route::group(['middleware' => ['web']], function () {
Route::get('student', ['uses' => 'StudentController@index', 'as' => 'student']);
Route::get('response', ['uses' => 'StudentController@response', 'as' => 'response']);
});
```
StudentController.php
```
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Student;
use Session;
class StudentController extends Controller
{
public function index(Request $request){
// 响应的常见类型 1、字符串 2、视图 3、Json 4、重定向
// 响应json
// $data = [
// 'errCode' => 0,
// 'errMsg' => 'success',
// 'data' => 'zhangsan'
// ];
// return response()->json($data);
// 重定向 with原理同session()->flash()
// return redirect('response')->with('message', '我是快闪数据');
// return redirect()->action('StudentController@response')->with('message', '我是快闪数据');
return redirect()->route('response')->with('message', '我是快闪数据');
// 返回上一个页面
// return redirect()->back();
}
public function response(){
echo Session::get('message', 'default');
}
}
```
访问:http://www.zhulinjie.com/laravel/public/student
![](https://box.kancloud.cn/53f79232f2852bae5c56ddbad816f37d_509x107.png)
再次访问:http://www.zhulinjie.com/laravel/public/student
![](https://box.kancloud.cn/3ba83886300eb017fd5a2c37b2ca56a7_481x105.png)
###Controller之Middleware
routes.php
```
// 宣传阶段
Route::get('student', ['uses' => 'StudentController@index', 'as' => 'student']);
// 活动进行阶段
Route::group(['middleware' => ['activity']], function () {
Route::get('response', ['uses' => 'StudentController@response', 'as' => 'response']);
});
```
StudentController.php
```
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Student;
use Session;
class StudentController extends Controller
{
public function index(Request $request){
echo '活动还未开始...';
}
public function response(){
echo '活动正在进行中...';
}
}
```
Activity.php
```
<?php
namespace App\Http\Middleware;
use Closure;
class Activity{
// 前置写法
public function handle($request, Closure $next){
if(time() < strtotime('2017-01-04')){
return redirect('student');
}
return $next($request);
}
}
```
Kernel.php
```
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
...
'activity' => \App\Http\Middleware\Activity::class
];
```
访问:http://www.zhulinjie.com/laravel/public/response
2017-01-04日之前访问
![](https://box.kancloud.cn/12d45fd3a4047111ba2d50e3a7ef27d2_439x107.png)
2017-01-04日之后访问
![](https://box.kancloud.cn/646712c3f2220fe0efd24a31277684a8_465x107.png)