首页 > 代码库 > Laravel-表单篇-controller

Laravel-表单篇-controller

(慕课网_轻松学会Laravel-表单篇_天秤vs永恒老师_http://www.imooc.com/learn/699)

Controller

  1、Controller-Request   

 1 //Request
 2     public function request1(Request $request){
 3         //1、取值
 4         // return $request->input(‘name‘,‘未知‘);
 5         //判断有没有sex
 6         if($request->has(‘sex‘)){
 7             echo $request->input(‘sex‘);
 8         }else{
 9             echo ‘无参数‘;
10         }
11         //获取所有的参数  dd打印,会结束掉脚本
12         $res = $request->all();
13         var_dump($res);
14 
15         //判断请求类型
16         echo $request->method();
17         var_dump($request->isMethod(‘GET‘));
18         var_dump($request->ajax());
19         //判断路由
20         var_dump($request->is(‘people/student/*‘));
21         echo ‘<br>‘;
22         //获取当前的url
23         echo $request->url();
24     }

  2、Controller-session

    默认使用file驱动,可以在配置文件中修改 Laravel中使用session有三种方式

    1)、HTTP request类中的session()方法

    2)、session()辅助函数

    3)、Session facade

    代码示例:

    路由

  

1    Route::group([‘middleware‘ => [‘web‘]], function(){
2         Route::get(‘student/setSession‘,‘People\StudentController@setSession‘);
3         Route::get(‘student/getSession‘,‘People\StudentController@getSession‘);
4         Route::get(‘student/deleteSession‘,‘People\StudentController@deleteSession‘);
5     }); 
use Symfony\Component\HttpFoundation\Request;
use Illuminate\Support\Facades\Session;
 1 //设置session
 2     public function setSession(Request $request){
 3         //1 HTTP request
 4         $request->session()->put(‘key1‘,‘value1‘);
 5 
 6         // 2 session()
 7         session()->put(‘key2‘,‘value2‘);
 8 
 9         //3 Session类
10         Session::put(‘key3‘,‘value3‘);
11 
12         Session::put([‘key5‘=>‘value5‘,‘key6‘=>‘value6‘]);
13         //把数据放在数组中
14         Session::push(‘student‘,‘helloJiu‘);
15         Session::push(‘student‘,‘sss‘);
16 
17         //存入session,获取一次就消失;
18         Session::flash(‘key-flash‘,‘value-flash‘);
19     }
20     //获取session
21     public function getSession(Request $request){
22         //1 HTTP request
23         echo $request->session()->get(‘key1‘);
24 
25         // 2 session()
26         echo session()->get(‘key2‘);
27 
28         //3 Session类
29         echo Session::get(‘key3‘,‘default‘);
30         //不存在,使用默认值 
31         echo Session::get(‘key4‘,‘default‘);
32         echo Session::get(‘key6‘);
33 
34         var_dump(Session::get(‘student‘,‘default‘));
35         //从session拿出数据就删除
36         var_dump(Session::pull(‘student‘));
37 
38         //取出session所有的值
39         echo ‘<pre>‘;
40         var_dump(Session::all());
41 
42         //判断session中某个key是否存在
43         var_dump(Session::has(‘key1‘));    
44         //第一次访问时存在,下次消失
45         echo Session::get(‘key-flash‘);    
46 
47     }
48 
49     //输出session中的值
50     public function deleteSession(){
51         //在session中删除key1和其对应的值
52         Session::forget(‘key1‘);
53 
54         //删除所有的session
55         Session::flush();
56     }

 

  3 Controller-Response

  响应的常见类型:字符串,视图,Json,重定向

  

 1 public function response(){
 2 
 3         $data = [
 4             ‘errCode‘ => 0,
 5             ‘errMsg‘ => ‘success‘,
 6             ‘data‘ => ‘helloJiu‘,
 7         ];
 8         //响应一个json数据
 9         return response()->json($data);
10         
11         //重定向,with原理是存入session,获取一次就删除
12         return redirect(‘people/student/getSession‘)->with(‘message‘,‘hellojiu‘);
13         return redirect()->action(‘People\StudentController@getSession‘)->with(‘message‘,‘helloJiu‘);
14         return redirect()->route(‘people/student/setSession‘);
15         return redirect()->back();//返回上一个页面
16     }

  4 Controller之Middleware

   Laravel中间件提供了一个方便的机制来过滤进入应用程序的HTTP请求

   如:有一个活动,在指定日期后开始,如果活动没有开始,只能访问宣传页面

    新建中间件、注册中间件、使用中间件、中间件的前置和后置操作

   控制器中有三个方法,activity1(为注册中间件)activity2\activity3都注册了,代码如下

    1)控制器   

 1 //对应活动的宣传页面
 2     public function activity1(){
 3         return ‘活动快要开始了‘;
 4     }
 5 
 6     public function activity2(){
 7         return ‘活动进行中‘;
 8     }
 9     public function activity3(){
10         return ‘活动完成,谢谢参与‘;
11     }

  2)、路由系统  

1     //宣传页面
2     Route::any(‘student/activity1‘,‘People\StudentController@activity1‘);
3     //活动页面,需要经过中间件验证
4     Route::group([‘middleware‘ => [‘activity‘]], function(){
5 
6         Route::any(‘student/activity2‘,‘People\StudentController@activity2‘);
7         Route::any(‘student/activity3‘,‘People\StudentController@activity3‘);
8     });

  3)、Http\Middleware\Activity.php中间件代码

 1 <?php
 2 namespace App\Http\Middleware;
 3 use Closure;
 4 
 5 class Activity{
 6 
 7     //前置操作
 8     public function handle($request, Closure $next){
 9 
10         if(time() < strtotime(‘2016-12-3‘)){
11             return redirect(‘people/student/activity1‘);
12         }
13         //访问当前页面
14         return $next($request);
15     }
16 
17     //后置操作
18     // public function handle($request, Closure $next){
19 
20     //     $response = $next($request);
21 
22     //     //逻辑在请求后面执行
23     //     echo ‘我是后置‘;
24     // }
25 }

  4)、App\Http\Kernel.php进行中间件注册  

1 protected $routeMiddleware = [
2         ‘auth‘ => \App\Http\Middleware\Authenticate::class,
3         ‘auth.basic‘ => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
4         ‘guest‘ => \App\Http\Middleware\RedirectIfAuthenticated::class,
5         ‘throttle‘ => \Illuminate\Routing\Middleware\ThrottleRequests::class,
6         ‘activity‘ => \App\Http\Middleware\Activity::class,
7     ];

   当请求activity2时,路由系统发现有中间件,就会通过Kerne找到Activity,调用其handle()方法,进行判断

  

    

  

Laravel-表单篇-controller