laravel框架学习(二)
在了解Laravel框架的基本结构之后,初步认识访问过程中路由的使用方法,以一套基本的学生信息增删改查来迅速学习框架开发。
首先了解几个目录文件作为开发的主要阵地:
1. /app/Http/routes.php 路由文件,一切访问从路由开始
2./app/Http/Controllers 控制器目录,我们写的控制器都放在该目录下
3./app/config Laravel配置文件夹一般修改.env文件
4./.env 经常修改配置的文件,数据库配置就在这里
5./public Laravel框架项目的入口文件夹,初始访问就是访问该文件夹下的index.php文件
6./resources/views Laravel框架视图文件夹,使用blade模板引擎
然后在/app/Http/Controllers下创建一个学生控制器
命令:php artisan make:controller StuController --resource
创建完成后 /app/Http/Controllers/StuCOngtroller.php
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?php 2 3 namespace App\Http\Controllers; 4 5 use Illuminate\Http\Request; 6 7 use App\Http\Requests; 8 use App\Http\Controllers\Controller; 9 10 class StuController extends Controller 11 { 12 /** 13 * Display a listing of the resource. 14 * 15 * @return \Illuminate\Http\Response 16 */ 17 public function index() 18 { 19 // 20 } 21 22 /** 23 * Show the form for creating a new resource. 24 * 25 * @return \Illuminate\Http\Response 26 */ 27 public function create() 28 { 29 // 30 } 31 32 /** 33 * Store a newly created resource in storage. 34 * 35 * @param \Illuminate\Http\Request $request 36 * @return \Illuminate\Http\Response 37 */ 38 public function store(Request $request) 39 { 40 // 41 } 42 43 /** 44 * Display the specified resource. 45 * 46 * @param int $id 47 * @return \Illuminate\Http\Response 48 */ 49 public function show($id) 50 { 51 // 52 } 53 54 /** 55 * Show the form for editing the specified resource. 56 * 57 * @param int $id 58 * @return \Illuminate\Http\Response 59 */ 60 public function edit($id) 61 { 62 // 63 } 64 65 /** 66 * Update the specified resource in storage. 67 * 68 * @param \Illuminate\Http\Request $request 69 * @param int $id 70 * @return \Illuminate\Http\Response 71 */ 72 public function update(Request $request, $id) 73 { 74 // 75 } 76 77 /** 78 * Remove the specified resource from storage. 79 * 80 * @param int $id 81 * @return \Illuminate\Http\Response 82 */ 83 public function destroy($id) 84 { 85 // 86 } 87 }
在Laravel5.1LTS版本中会自动生成以上6个方法,接下来为该控制器注册一个资源路由
打开路由文件,插入一句话:Route::resource('stu', 'StuController');
这个路由声明包含了处理图片资源 RESTful 动作的多个路由,相应地,Artisan 生成的控制器也已经为这些动作设置了对应的处理方法。
方法 | 路径 | 动作 | 路由名称 |
GET | /stu | index | stu.index |
GET | /stu/create | create | stu.create |
POST | /stu | store | stu.store |
GET | /stu/{$id} | show | stu.show |
GET | /stu/{$id}/edit | edit | stu.edit |
PUT/PATCH | /stu/{$id} | update | stu.update |
DELETE | /stu/{$id} | destroy | stu.destroy |
表单提交我们通常使用GET/POST提交,这里需要伪装PUT/DELETE表单提交
HTML 表单不支持 PUT、PATCH 或者 DELETE 请求方法,因此,当 PUT、PATCH 或 DELETE 路由时,需要添加一个隐藏的 _method
字段到表单中,其值被用作该表单的 HTTP 请求方法:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <form action="/foo/bar" method="POST"> 2 <input type="hidden" name="_method" value="PUT"> 3 <input type="hidden" name="_token" value="{{ csrf_token() }}"> 4 </form>
还可以使用辅助函数 method_field
来实现这一目的:<?php echo method_field('PUT'); ?>
当然,也支持 Blade 模板引擎:{{ method_field('PUT') }}
要对数据库进行操作修改.env文件配置数据库
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 DB_HOST=localhost 2 DB_DATABASE=lamp 3 DB_USERNAME=root 4 DB_PASSWORD=
修改Stu控制器/app/Http/Controllers/StuCOngtroller.php
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?php 2 3 namespace App\Http\Controllers; 4 5 use Illuminate\Http\Request; 6 7 use App\Http\Requests; 8 9 class StuController extends Controller 10 { 11 public function index() 12 { 13 $data = \DB::table("stu")->get(); 14 return view("stu.index",['list'=>$data]); 15 } 16 17 public function create() 18 { 19 return view("stu.add"); 20 } 21 22 public function store(Request $request) 23 { 24 $data = ['name'=>$request->name,'age'=>$request->age,'sex'=>$request->sex,'classid'=>$request->classid]; 25 $m = \DB::table("stu")->insertGetId($data); 26 if($m>0){ 27 return "添加成功!自增id=".$m."<a href=".url('stu').">返回查看</a>"; 28 }else{ 29 return "添加失败!"; 30 } 31 } 32 33 public function show($id) 34 { 35 echo $id; 36 } 37 38 public function edit($id) 39 { 40 $user = \DB::table("stu")->where('id',$id)->first(); 41 //var_dump($user); 42 return view("stu.edit",['vo'=>$user]); 43 44 } 45 46 public function update(Request $request,$id) 47 { 48 \DB::table('stu')->where('id',"=",$id)->update(['name'=>$request->name,'age'=>$request->age,'sex'=>$request->sex,'classid'=>$request->classid]); 49 echo "<a href=".url('stu').">返回查看</a>"; 50 } 51 52 public function destroy($id) 53 { 54 //echo "删除".$id; 55 \DB::table("stu")->delete($id); 56 echo "<a href=".url('stu').">返回查看</a>"; 57 } 58 }
创建Stu视图/resources/views/stu
index.blade.php
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"/> 5 <title>PHP+MySQLI的学生信息管理</title> 6 <link type="text/css" rel="stylesheet" href="./bootstrap.min.css"/> 7 <script type="text/javascript"> 8 function fun(){ 9 var sub=document.getElementById("sub"); 10 11 //跳转 12 //window.location.href={{url('stu')}}; 13 } 14 15 16 17 </script> 18 </head> 19 <body> 20 <center> 21 @include("stu.menu") 22 <h3>浏览学生信息</h3> 23 <table width="700" border="1"> 24 <tr> 25 <th>学号</th> 26 <th>姓名</th> 27 <th>性别</th> 28 <th>年龄</th> 29 <th>班级</th> 30 <th>操作</th> 31 </tr> 32 @foreach($list as $stu) 33 <tr> 34 <td>{{$stu->id}}</td> 35 <td>{{$stu->name}}</td> 36 <td>{{$stu->sex}}</td> 37 <td>{{$stu->age}}</td> 38 <td>{{$stu->classid}}</td> 39 <td> 40 <a href="javascript:void(0)" onclick="doDel({{$stu->id}})">删除</a> 41 <a href="{{url("stu")."/".$stu->id."/edit"}}">编辑</a></td> 42 </tr> 43 @endforeach 44 </table> 45 <form action="" method="POST"> 46 <input type="hidden" name="_method" value="DELETE"> 47 <input type="hidden" name="_token" value="{{ csrf_token() }}"> 48 <input type="hidden" name="pname" value="" id="pname"> 49 </form> 50 <script> 51 function doDel(id){ 52 var form = document.getElementsByTagName("form")[0]; 53 form.action ="{{url('stu')}}"+"/"+id; 54 form.submit(); 55 } 56 </script> 57 58 </center> 59 </body> 60 </html>
menu.blade.php
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <h2>在线学生信息管理</h2> 2 <a href="{{url('stu')}}">浏览信息</a> | 3 <a href="{{url('stu/create')}}">添加信息</a> 4 <hr/>
add.blade.php
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"/> 5 <title>PHP+MySQLI的学生信息管理</title> 6 </head> 7 <body> 8 <center> 9 @include("stu.menu") 10 <h3>添加学生信息</h3> 11 <form action="{{url('stu')}}" method="post"> 12 <input type="hidden" name="_token" value="<?php echo csrf_token(); ?>"> 13 <table width="300" border="0"> 14 <tr> 15 <td align="right">姓名:</td> 16 <td><input type="text" name="name"/></td> 17 </tr> 18 <tr> 19 <td align="right">性别:</td> 20 <td><input type="radio" name="sex" value="m"/>男 21 <input type="radio" name="sex" value="w"/>女</td> 22 </tr> 23 <tr> 24 <td align="right">年龄:</td> 25 <td><input type="text" name="age"/></td> 26 </tr> 27 <tr> 28 <td align="right">班级:</td> 29 <td><input type="text" name="classid"/></td> 30 </tr> 31 <tr> 32 <td colspan="2" align="center"> 33 <input type="submit" value="添加"/> 34 <input type="reset" value="重置"/> 35 </td> 36 </tr> 37 </table> 38 </form> 39 </center> 40 </body> 41 </html>
edit.blade.php
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"/> 5 <title>学生信息管理</title> 6 </head> 7 <body> 8 <center> 9 @include("stu.menu") 10 11 <h3>编辑学生信息</h3> 12 <form action="{{url('stu')."/".$vo->id}}" method="post"> 13 <input type="hidden" name="_method" value="PUT"> 14 <input type="hidden" name="_token" value="{{ csrf_token() }}"> 15 16 <table width="280" border="0"> 17 <tr> 18 <td align="right">姓名:</td> 19 <td><input type="text" name="name" value="{{$vo->name}}"/></td> 20 </tr> 21 <tr> 22 <td align="right">性别:</td> 23 <td> 24 <input type="radio" name="sex" value="m" {{ $vo->sex=='m' ? "checked" : "" }} />男 25 <input type="radio" name="sex" value="w" {{ $vo->sex=='w' ? "checked" : "" }} />女 26 </td> 27 </tr> 28 <tr> 29 <td align="right">年龄:</td> 30 <td><input type="text" name="age" value="{{$vo->age}}"/></td> 31 </tr> 32 <tr> 33 <td align="right">班级:</td> 34 <td><input type="text" name="classid" value="{{$vo->classid}}"/></td> 35 </tr> 36 <tr> 37 <td colspan="2" align="center"> 38 <input type="submit" value="修改"/> 39 <input type="reset" value="重置"/> 40 </td> 41 </tr> 42 </table> 43 </form> 44 </center> 45 46 </body> 47 </html>