Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:抽空多看看手册, 每次都会有新发现
laravel框架任何请求都要经过路由。
public目录下有文件,其访问优先级高于路由。
Route::get('/', function () {return view('welcome');});
welcome
:视图文件名称。view('welcome', 数组);
: 数组参数,带一些数据到视图视图文件加载演示代码:
// test.php
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>加载视图文件测试</title>
</head>
<body>
<div>
好好学习,天天向上!
</div>
</body>
</html>
// controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class Home extends Controller
{
public function test(){
return view('test');
}
}
// 路由
Route::get('/', function () {return view('test');});
演示效果图:
图1:
图2成功访问:
Route::get('路由地址', '控制器类@类方法名称');
控制器类
:默认是Controllers
目录下,如果还有下级目录要带上。如Route::get('/admins/article/list', 'admins\Article@lists');
artisian
文件目录php artisan make:controller Home
php artisan make:controller 目录名/文件名
使用命令创建成功图:
视图文件加载演示代码:
// controlle
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
// 控制器
class Home extends Controller
{
// 加载test视图文件的方法
public function index(){
// echo '<pre>';
$res = DB::select('SELECT * FROM `article`');
// print_r($res);
$lists = [];
foreach ($res as $val){
$lists[] = (array)$val;
}
$data['result'] = $lists;
// print_r($data);
return view('test', $data);
}
// 数据库查询(原生)
public function select(){
echo '<pre>';
$res = DB::select('SELECT * FROM `article`');
print_r($res);
}
// 数据库更新(原生)
public function update(){
echo '<pre>';
$res = DB::update("UPDATE `article` SET `title` = '是的,我知道了' WHERE = `id` = 4 ");
print_r($res);
}
// 数据库插入(原生)
public function insert(){
echo '<pre>';
$res = DB::insert("INSERT `article` (`title`) VALUES ('php如何向文件中写入换行php如何向文件中写入换行'),
('PS精简版和完整版的区别PS精简版和完整版的区别')");
print_r($res);
}
// 数据库删除(原生)
public function delete(){
echo '<pre>';
$res = DB::delete("DELETE FROM `article` WHERE `id` = 4 ");
print_r($res);
}
// 数据库删除(链式)
public function finds(){
echo '<pre>';
$res = DB::table('article')->where('id', 2)->first();
print_r($res);
}
}
// 视图文件
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>数据库操作</title>
<link rel="stylesheet" type="text/css" href="/layui/css/layui.css">
</head>
<body>
<table class="layui-table">
<thead>
<tr>
<td>ID</td>
<td>标题</td>
</tr>
</thead>
<tbody>
<?php foreach ($result as $val): ?>
<tr>
<td><?php echo $val['id']; ?></td>
<td><?php echo $val['title']; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</body>
</html>
// 路由
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
// Route::get('/', function () {return view('welcome');});
Route::get('/', function () {
return view('test');
});
Route::get('/home/index', 'Home@index');
// 数据库查询
Route::get('/home/select', 'Home@select');
// 数据库更新
Route::get('/home/update', 'Home@update');
// 数据库插入
Route::get('/home/insert', 'Home@insert');
// 数据库删除
Route::get('/home/delete', 'Home@delete');
// 数据库链式查询
Route::get('/home/finds', 'Home@finds');
演示效果图:
1、本节课学会了如何在MVC模型框架中把视图view
展示出来。
2、了解如何把控制器映射到路由。
3、加强了一下原生的数据库增删改查操作
总之收获很多。