完成了登录模块,对构造函数有一点了解。
login控制器
<?php namespace app\admin\controller; use think\Controller; use app\admin\model\Admins; use think\facade\Request; use think\facade\Session; class Login extends Controller { public function login() { // if(request()->isPost()) // { // } return $this->fetch(); } public function dologin() { $res=input('post.'); // dump($res); $res1 = $res['name']; $admins = admins::get(function($suib)use($res1){ $suib->where('name',$res1); }); // dump($admins);die; if ($admins) { if ($res['password']==$admins['password']) { // dump('11111');die; $info=['res'=>1,'msg'=>'登录成功']; Session::set('name', $res['name']); }else{ $info=['res'=>2,'msg'=>'密码不正确']; } }else{ $info=['res'=>3,'msg'=>'账户不存在']; } // dump($info);die; return $info; } public function Logout() { Session::delete('name'); $this->redirect('login'); } }
common控制器
<?php
/**
*
*/
namespace app\admin\controller;
use think\Controller;
use think\facade\Session;
class Common extends Controller
{
public function __construct()
{
parent::__construct();
if (!Session::has('name')) {
$this->error('您还未登录!请返回登录!','Login/Login');
}
}
}