批改状态:合格
老师批语:尽可能使用命令行来创建控制器, 模型等
使用 php artisan make:controller Index 命令就可以自动生成一个 Index.php控制器

并且控制器已经被写好了类和命名空间:

下一步,给控制器写上一个方法,然后使用路由绑定这个控制器中的方法,实现一个映射:
index.php:
<?phpnamespace App\Http\Controllers;use Illuminate\Http\Request;class Index extends Controller{public function index(){return 'index页面';}}
web.php:
<?phpuse Illuminate\Support\Facades\Route;Route::get('/', function () {return view('welcome');});Route::get('index','Index@index');
访问页面时,如下:


在Controllers目录下,直接新建文件,比如:Show.php,创建好以后是一个空白文件,需要自己补上命名空间以及类的继承
命名空间和文件目录对应,如:namespace App\Http\Controllers
类名和文件名对应,并且要继承基类,如:class Show extends Controllers
都创建好以后在类中再写一个方法用于测试,如下:
Show.php:
<?phpnamespace App\Http\Controllers;class Show extends Controller{public function test(){return '这是手动创建的控制器';}}
web.php:
Route::get('index/test','Show@test');
访问页面如下:

view()函数中直接传值,以数组的方式,如下:User.php:(控制器)
<?phpnamespace App\Http\Controllers;use Illuminate\Http\Request;class User extends Controller{public function info(){$username = 'alice';return view('user',['username'=>$username]);}}
web.php:(路由)
Route::get('index/user','User@info');
user.blade.php:
<!doctype html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport"content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>Document</title></head><body><div>用户名:{{$username}}</div></body></html>
访问页面如下:

view()方法中当做参数使用
class User extends Controller{public function info(){$data['name'] = 'jack';$data['hobby'] = 'ball';$data['age'] = 20;$data['sex'] = 'male';$data['score'] = 'very good';return view('user',$data);}}
<body><div>姓名:{{$name}}</div><div>爱好:{{$hobby}}</div><div>年龄:{{$age}}</div><div>性别:{{$sex}}</div><div>成绩:{{$score}}</div></body>

这里看到 {{}} 这样的双括号语法,其实这是blade翻译模板引擎的语法,使用了{{}}以后,Laravel框架会自动将变量显示出来,就类似于:
<div>姓名:<?php echo $name;?></div>
而在blade翻译模板中,翻译以后多了一个e()函数:(翻译文件在storge->framework->views中查找)
<div>爱好:<?php echo e($hobby); ?></div>
使用此函数的原因是,Laravel框架为了保证代码的相对安全性,因为当你使用HTML标签时,e()方法会原样输出,如下:

如果不使用{{}}语法,使用原生的<? echo ...;?>语法,可能会造成安全漏洞,比如:(当加入script标签时)
$data['score'] = '<script>window.location.href="http://www.baidu.com";</script>';

当你进入网页时,页面会自动跳转到其他网站,这是非常危险的,所有都要尽量使用{{}}这种语法
不过有时候在确保相对安全的情况下,很想使用HTML标签,比如给字体做点样式,那么这时候可以使用{!! 变量 !!}来禁用Lavarel的e()方法,禁止转义翻译,如下:
$data['sex'] = '<span style="color: red;font-size: 1.5rem">male</span>';
<div>性别:{!! $sex !!}</div>

控制器:
<?phpnamespace App\Http\Controllers;use Illuminate\Http\Request;class Index extends Controller{public function login(){$username = 'lisa';// $username = false;return view('login',['username'=>$username]);}}
路由:
Route::get('login','Index@login');
视图:
<!doctype html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport"content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>Document</title></head><body>@if($username)<div>进入主页了,欢迎您,{{$username}}</div>@else<div>请您先登录</div>@endif</body></html>
当$username='lisa'时,访问页面如下:

当$username=false时,访问页面如下:

控制器:
public function info(){$score = 50;return view('user',['score'=>$score]);}}
路由:
Route::get('index/user','User@info');
视图:
<!doctype html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport"content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>Document</title></head><body><h3>学生成绩</h3>@switch(true)@case($score>=90 && $score<100)<p>成绩={{$score}}分,优秀!</p>@break@case($score>=70 && $score<90)<p>成绩={{$score}}分,良好!</p>@break@case($score>=60 && $score<70)<p>成绩={{$score}}分,刚好及格!</p>@break@default<p>成绩={{$score}}分,准备补考!</p>@endswitch</body></html>
当$score=50时,页面输出如下:

当$score=88时,页面输出如下:

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号