https://www.kancloud.cn/manual/thinkphp5/118003
找到application/config.php文件改成true,这样便于调试。
// 应用调试模式
'app_debug' => true,
// 应用Trace
'app_trace' => true,
//后台入口控制器
php think make:controller admin/Entry --plain
//后台用户管理控制器
php think make:controller admin/User --plain
//后台公共控制器
php think make:controller admin/Common --plain
后台入口控制器建立index方法。
//首页
public function index()
{
//加载后台首页模板
return view();
}
在application/admin/view建立entry文件夹,放入index.html作为后台首页。
在地址栏可以尝试访问:http://你的域名/admin;
如果不行,请修改public目录下的.htaccess。
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
也可以更改模板后缀名,找到application/config.php,这样模板就可以是以php后缀结尾了。
'template'=>[
// 模板后缀
'view_suffix' => 'php',
]
a)登录验证
让application/admin/controller/Entry.php的Entry控制器继承Common,在Common控制器中写下初始化方法。
/**
* TP框架提供的初始化函数
*/
public function _initialize() {
//如果没有登陆就去跳转
if(!Session::get('user')){
//重定向到登陆
$this->redirect('Login/index');
}
}
让其他后台控制器(除去用户管理控制器)都继承Common,这样就可以登陆验证了。
b)自定义一个函数包 help.php文件
在单入口最上方,也就是框架运行之前进行载入。
include './helper.php';
helper.php代码,定义打印P函数。
if(!function_exists('p')){
function p($data)
{
echo "<pre style='padding: 15px;background: #ccc;border-radius: 6px'>";
if (is_null($data)) {
var_dump($data);
} elseif (is_bool($data)) {
var_dump($data);
} else {
print_r($data);
}
echo '</pre>';
}
}
c)配置数据库
找到application/database.php文件,连接数据库
注:这里不详细介绍数据表,有需要请看博文底部。
// 服务器地址
'hostname' => 'localhost',
// 数据库名
'database' => 'blog_edu',
// 用户名
'username' => 'root',
// 密码
'password' => 'root',
d)登录功能
在数据库user表中手动添加一个admin用户,密码默认为md5(‘admin888’)。
建立Users模型,会在application/common/model生成User.php文件,模型名和数据表名最好保持一致。
php think make:model Users
接着完成用户管理控制器。
public function login()
{
//测试数据库连接
//$data = db('user')->find(1);
//p($data);
//手册搜请求类型 采用了request类进行处理
if (request()->isPost()) {
//变量调试输出并中断执行
//p($_POST);die;
$res = (new User())->login(input('post.'));
if ($res['valid']) {
$this->success($res['msg'], 'admin/entry/index');
} else {
$this->error($res['msg']);
}
}
//载入模板
return $this->fetch();
}
再是处理用户管理模型。
public function login($data)
{
//1.执行验证
$validate = Loader::validate('User');
//如果验证不通过
if (!$validate->check($data)) {
// dump($validate->getError());
return ['valid' => 0, 'msg' => $validate->getError()];
}
//2.比对用户名和密码是否正确
$userInfo = $this->where('username', $data['username'])->where('password', Crypt::encrypt($data['password']))->find();
if (!$userInfo) {
return ['valid' => 0, 'msg' => '用户名或密码错误'];
}
//3.将用户名和密码存入session中
session('admin.username', $userInfo['username']);
session('admin.uid', $userInfo['uid']);
return ['valid' => 1, 'msg' => '登录成功'];
}
以上代码中用到验证器进行验证,可以在application/admin/中建立一个validate文件夹,文件夹内放给后台每个管理控制器的验证器。我们要处理用户登陆的话,建立user.php这样一个类,代码如下:
namespace app\admin\validate;
use think\Validate;
class User extends Validate
{
//规则
protected $rule = [
'username'=>'require',
'password'=>'require',
'code'=>'require|captcha'
];
//提示消息
protected $message = [
'username.require'=>'请输入用户名',
'password.require'=>'请输入密码',
'code.require'=>'请输入验证码',
'code.captcha'=>'验证码错误',
];
}
在后台模板右上角输出用户名。
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button">
<i class="fa fa-w fa-user"></i>
<?php echo \think\Session::get( 'user.username' ) ?>
<span class="caret"></span>
</a>
e)退出功能
public function logout() {
// 清除当前请求有效的session
Session::delete('user');
$this->success('退出成功','index');
}
把公共部分摘取出来叫base.html放到application/admin/view目录下面。
base.html非共用部分使用如下:
{block name="content"}{/block}
后台首页模板index.html代码如下:
{extend name="base" /}
{block name="content"}
我是非公共部分
{/block}
composer require houdunwang/arr
$validate = new Validate([
'keywords' => 'require',
'des' => 'require',
'content' => 'require',
],[
'keywords.require' => '请填写关键字',
'des.require' => '请填写文章描述',
'content.require' => '请填写文章内容',
]);
if (!$validate->check($data)) {
return ['valid' => 0, 'msg' => $validate->getError()];
}
手册中搜索url重写,执行以下几步:
a)httpd.conf配置文件中加载了mod_rewrite.so模块
b)AllowOverride None 将None改为 All
c)把下面的内容保存为.htaccess文件放到应用入口文件的同级目录下
<IfModule mod_rewrite.c>
Options +FollowSymlinks -Multiviews
RewriteEngine on #开启Rewrite
RewriteCond %{REQUEST_FILENAME} !-d #如果访问的不是目录
RewriteCond %{REQUEST_FILENAME} !-f #如果访问的不是文件
RewriteRule ^(.*)$ index.php?/$1 [QSA,PT,L]
#将访问路径重写到index.php/的后面,作为参数传递给index.php文件
#QSA:表示保留参数如get传值?xxx==xx...;PT:再把这个URL交给Apache处理;L:作为最后一条;PT和L可加可不加
</IfModule>
注:以上几步框架一般自带(#为博文加的注释),所以真正的优雅链接只要执行下面几步:
a)找到框架application/route.php文件
b)copy文件中'[hello]’的内容,如下代码
'[c]' => [
':cid' => ['index/lists/index', ['method' => 'get'], ['id' => '\d+']],
], //处理分类跳转
'[t]' => [
':tid' => ['index/lists/index', ['method' => 'get'], ['id' => '\d+']],
],//处理标签跳转
'[a]' => [
':aid' => ['index/content/index', ['method' => 'get'], ['id' => '\d+']],
],//处理阅读全文跳转
'[i]' => [
':iid' => ['index/index/index', ['method' => 'get'], ['id' => '\d+']],
],//处理首页跳转,这里注意给首页跳转的url加上参数
OK…老实说,写这篇博文真的很费时间…所以我打算就引导到这里啦。
有兴趣讨论请留言或者加WeChat:hello_McGrady。
Finally,谢谢大家的阅读!祝每天开心~
版权声明:本文为博主半原创文章,未经博主允许不得转载。
赞赏
微信赞赏
支付宝赞赏
发表评论