所有的路由都需要的web.php配置
文件路径
“routes\web.php “
laravel 的路由规则
1.只允许单一的方式请求
“//只允许get方式请求 route::get(‘index/hello’,function(){ echo 123; }); //只允许post方式请求 route::post(‘index/hello’,function(){ echo 123; }); //只允许put方式请求 route::put(‘index/hello’,function(){ echo 123; }); //只允许delete方式请求 route::delete(‘index/hello’,function(){ echo 123; }); “
2.允许多种方式请求
“//允许post和get请求 route::match([‘get’,’post’],’index’,function(){ echo 1; }); “
3.不限制请求方式
“route::any(‘index’,function(){ echo 123; }); “
4.路由传参
“route::get(‘index/{id}’,function($id){ echo $id; }); “
5.路由传可选参数
“route::get(‘index/{id?}’,function($id){ echo $id; }); “
6.路由传递多个参数
“route::get(‘index/{id}/{name}’,function($id,$name){ echo $id; echo $name; }); “
7.路由的正则限制
“route::get(‘index/{id}/{name}’,function($id,$name){ echo $id; echo $name; })->where([‘id’=>'[0-9]+’,’name’=>”[‘a-z’]{6,}”]); “
- 控制器响应路由
“route::get(‘index/index’,’IndexController@index’); “
M model 模型 数据逻辑的操作
C controller 控制器 业务逻辑的处理 链接M V
V view 视图 处理数据的可视化
模板操作
处理数据结果的可视化过程 html页面
加载模板
//不通过控制器直接加载模板 直接通过view加载
“Route::get(‘qqqqqq’, function () { return view(‘study.index’); //welcome(.blade).php }); “
通过控制器完成跳转
“`
public function index()
{
//view直接加载视图的模板
return view(‘study.index’);
//redirect 可以实现跨控制器的跳转
//return redirect(‘study.index’);
}
“`
模板文件 .php .blade.php 的区别
.php 所有的输出要以原生的PHP语法来编写
.blade.php 可以使用模板的输出方式
1.直接输出变量
“`
欢迎{{$name}}莅临指导
“`
2.判断语句
“@if($age>=18) 成年 @else 未成年 @endif “
3.循环语句
for循环
“`
@for(
i
=
0
;
i=0;
i=0;i<10;KaTeX parse error: Expected '}', got 'EOF' at end of input: i++) {{i}} @endfor foreach 循环
@foreach ($users as $user)
This is user {{ $user->id }}
@endforeach
“`
while循环
“`
@while (true)
I am looping forever.
@endwhile
“`
while循环和do while循环的区别
控制器操作
命名:大驼峰法
命名空间:一般遵循自动加载psr-4规范的 当前文件的命名空间就是相对于入口文件的位置
命名空间的作用:用来区分不同类中的相同方法 相当于给类起个别名
使用artisan 创建控制器
“php artisan make:controller ControllerName; “
使用php artisan 创建restful类型路由
先在web.php 配置一个resource类型的路由
“route::resource(‘user’,’Admin\UserController’); “
然后用artisan 命令 看路由列表
“php artisan route:list “
“| | GET|HEAD | user | user.index | App\Http\Controllers\Ad | | POST | user | user.store | App\Http\Controllers\Ad | | GET|HEAD | user/create | user.create | App\Http\Controllers\Ad | | GET|HEAD | user/{user} | user.show | App\Http\Controllers\Ad | | PUT|PATCH | user/{user} | user.update | App\Http\Controllers\Ad | | DELETE | user/{user} | user.destroy | App\Http\Controllers\Ad | | GET|HEAD | user/{user}/edit | user.edit | App\Http\Controllers\Ad “
+——–+———–+——————+————–+————————
然后在用artisan 创建资源类型的控制器
“php artisan make:controller ControllerName –resource;
在这里插入代码片`