1、laravel控制器
代码示例——后端
<?php
namespace App\Http\Controllers\admin;
use App\Http\Controllers\controller;
use Illuminate\Support\Facades\DB;
class Index extends Controller{
public function index(){
return view("index");
}
//获取数据
public function get(){
$res = DB::select('select * from users ');
$lists = [];
foreach($res as $val){
$lists[] = (array)$val;
}
$data['result'] = $lists;
return view("index",$data);
}
//插入用户数据
public function insert(){
$data = time();
$res = DB::insert('insert users(`name`,`pwd`,`email`,`add_time`)values("ccc","123456","asd@qq.com","$data")');
print_r($res);
}
//更新数据
public function update(){
$data = time();
$res = DB::update("update users set add_time = $data where id=5");
var_dump($res);
}
//删除数据
public function delete(){
$res = DB::delete("delete from users where id=5");
var_dump($res);
}
}
代码示例——前端
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="/layui/css/layui.css">
<title>首页</title>
</head>
<body>
<div style="margin:auto;width:500px">
<table class="layui-table">
<caption align="top">用户信息</caption>
<thead>
<tr>
<th>ID</th>
<th>用户名</th>
<th>email</th>
<th>添加时间</th>
</tr>
</thead>
<tbody>
<?php foreach($result as $val) { ?>
<tr>
<th><?php echo $val['id'] ?></th>
<th><?php echo $val['name'] ?></th>
<th><?php echo $val['email'] ?></th>
<th><?php echo date("y-m-d H:i:s", $val['add_time']); ?></th>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</body>
</html>
2、路由
代码示例
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| 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('/admin/index','admin\Index@index');
Route::get('/admin/index/get','admin\Index@get');
Route::get('/admin/index/insert','admin\Index@insert');
Route::get('/admin/index/update','admin\Index@update');
Route::get('/admin/index/delete','admin\Index@delete');
学习总结
本节课我们学习了laravel控制器与路由,通过本节课的学习知道了laravel控制器的使用以及学到了路由的使用,有助于后期的学习。