application/admin/controller/Article.php
<?php
/*** 文章管理*/namespace app\admin\controller;use app\admin\validate\ArticleValidate;
use app\common\model\Articles;
use think\Controller;
use think\Request;class Article extends Controller {/*** 显示资源列表** @return \think\Response*/public function index() {$data = model('articles')->paginate(3);return view('admin@article/index', compact('data'));}/*** 显示创建资源表单页.** @return \think\Response*/public function create() {// 读取栏目$data = db('cates')->select();return view('admin@article/create', compact('data'));}/*** 保存新建的资源** @param \think\Request $request* @return \think\Response*/public function save(Request $request) {// 收集表单数据$input = $request->post();// 验证数据$ret = $this->validate($input, ArticleValidate::class);if (true !== $ret) {return $this->error($ret);}// 入库$userinfo = session('admin.user');$input['users_id'] = $userinfo['id'];$ret = Articles::create($input);return redirect(url('article/index'));}/*** 显示指定的资源** @param int $id* @return \think\Response*/public function read($id) {}/*** 显示编辑资源表单页.** @param int $id* @return \think\Response*/public function edit(int $id) {// 根据ID查询$data = Articles::find($id);// 读取栏目$cdata = db('cates')->select();return view('admin@article/edit', compact('data', 'cdata'));}/*** 保存更新的资源** @param \think\Request $request* @param int $id* @return \think\Response*/public function update(Request $request, int $id) {$input = $request->put();// 验证$ret = $this->validate($input, ArticleValidate::class);if (true !== $ret) {return $this->error($ret);}// 修改数据unset($input['__token__']);unset($input['_method']);Articles::where('id', $id)->update($input);// 跳转return redirect(url('article/index'));}/*** 删除指定资源** @param int $id* @return \think\Response*/public function delete(int $id) {Articles::destroy($id);$data = Articles::paginate(3)->toArray();#return json(['status' => 0, 'msg' => '删除成功']);return json($data);}
}
application\admin\controller\Index.php
<?php
/*** 后台首页*/
namespace app\admin\controller;use think\Controller;class Index extends Controller
{// 首页public function index() {return view('admin@index/index');}// 欢迎页面public function welcome() {return view('admin@index/welcome');}
}
application/admin/controller/Login.php
<?phpnamespace app\admin\controller;use app\admin\validate\UserValidate;
use think\Controller;
use think\Request;class Login extends Controller
{// 登录界面public function index(){return view('admin@login/index');}// 登录处理public function loginHandler(Request $request) {$input = $request->post();// 验证$ret = $this->validate($input,UserValidate::class);// 判断验证是否通过if (true !== $ret){return $this->error('请重新登录');}// 查询数据库$ret = model('users')->checkUser($input);if ($ret){return $this->success('登录成功',url('admin/index/index'));}else{return $this->error('用户名或密码不正确');}}
}
route/admin/admin.php
<?php
// 后台路由
use think\facade\Route;Route::group(['prefix' => 'admin/', 'name' => 'admin'], function () {// 登录Route::get('login', 'login/index')->name('admin/login/index');Route::post('login', 'login/loginHandler')->name('admin/login/index');Route::group(['middleware'=>['CheckLogin']],function (){// 后台首页Route::get('index', 'index/index')->name('admin/index/index');// 欢迎页Route::get('welcome','index/welcome')->name('admin/index/welcome');// 资源路由Route::resource('art','article');});
application/http/middleware
<?phpnamespace app\http\middleware;class CheckLogin
{public function handle($request, \Closure $next){// 判断用户是否登录if (!session('?admin.user')){// 此处用到了闪存return redirect('admin/login/index')->with('error','非法请求请登录!');}#向下执行操作return $next($request);}
}