Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:相当的用心
在app\Http\Controllers
目录下创建自定义的控制器
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
Class Home extends Controller{
public function index(){
echo '这是home中的index方法';
}
public function php(Request $request){
echo '这是home中的php方法';
$data['name'] = $request->name;
return view('test',$data);
}
}
控制器名与类名一致
打开命令行,进入本地的laravel的根目录输入以下命令:
php artisan make:controller 控制器名
控制器:Home.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
Class Home extends Controller{
public function index(){
echo '这是home中的index方法';
}
public function php(Request $request){
echo '这是home中的php方法';
$data['name'] = $request->name;
return view('test',$data);
}
}
视图:test.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>测试视图</title>
</head>
<body>
<a href="http://www.php.cn"><h2>php中文网{{$name}}</h2></a>
</body>
</html>
路由:web.php
<?php
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
});
Route::get('/home.hop','Home@php');
视图:test.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>测试视图</title>
</head>
<body>
<div>用户名:{{$username}}</div>
<div>账号:{{$zh}}</div>
<div>密码:{{$mm}}</div>
<div>地址:{{$dz}}</div>
<div>国家:{!!$gj!!}</div>
<a href="http://www.php.cn"><h2>php中文网{{$name}}</h2></a>
</body>
</html>
控制器:Home.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
Class Home extends Controller{
public function index(){
echo '这是home中的index方法';
}
public function php(Request $request){
echo '这是home中的php方法';
$data['name'] = $request->name;
$data['username'] = '<script>alert("admin");</script>';
$data['zh'] = '156320';
$data['mm'] = 'dasnfld';
$data['dz'] = '山西省临汾市';
$data['gj'] = '<sapn style="color:red">中国</span>';
return view('test',$data);
}
}
路由:web.php
<?php
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
});
Route::get('/home.hop','Home@php');
如果用户名存在就显示出来,不存在就显示登录
视图:test.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>测试视图</title>
</head>
<body>
<div>
@if($username)
用户名:{{$username}}
@else
<a href="">登录</a>
@endif
</div>
<div>账号:{{$zh}}</div>
<div>密码:{{$mm}}</div>
<div>地址:{{$dz}}</div>
<div>国家:{!!$gj!!}</div>
<a href="http://www.php.cn"><h2>php中文网{{$name}}</h2></a>
</body>
</html>
控制器:Home.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
Class Home extends Controller{
public function index(){
echo '这是home中的index方法';
}
public function php(Request $request){
echo '这是home中的php方法';
$data['name'] = $request->name;
//不存在就显示登录
//$data['username'] = '';
//存在就显示用户名
$data['username'] = 'admin';
$data['zh'] = '156320';
$data['mm'] = 'dasnfld';
$data['dz'] = '山西省临汾市';
$data['gj'] = '<sapn style="color:red">中国</span>';
return view('test',$data);
}
}
年龄匹配到17,显示未成年;匹配到18显示刚成年;匹配到30显示老男人
视图:test.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>测试视图</title>
</head>
<body>
<div>
@if($username)
用户名:{{$username}}
@else
<a href="">登录</a>
@endif
</div>
<div>账号:{{$zh}}</div>
<div>
@switch($age)
@case(17)
<div style="color:red;">未成年</div>
@break
@case(18)
<div style="color:green;">刚成年</div>
@break
@case(30)
<div style="color:yellow;">老男人</div>
@break
@default
<div>不知道</div>
@endswitch
</div>
<div>地址:{{$dz}}</div>
<div>国家:{!!$gj!!}</div>
<a href="http://www.php.cn"><h2>php中文网{{$name}}</h2></a>
</body>
</html>
控制器:Home.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
Class Home extends Controller{
public function index(){
echo '这是home中的index方法';
}
public function php(Request $request){
echo '这是home中的php方法';
$data['name'] = $request->name;
$data['username'] = 'admin';
$data['zh'] = '156320';
$data['age'] = '18';
$data['dz'] = '山西省临汾市';
$data['gj'] = '<sapn style="color:red">中国</span>';
return view('test',$data);
}
}