# 逻辑与验证 * * * * * 上一篇中实现了后台友情链接控制器研发,现在将研发对应的业务逻辑。 代码是作者提前写好的,逻辑代码如下 ~~~ <?php // +---------------------------------------------------------------------+ // | OneBase | [ WE CAN DO IT JUST THINK ] | // +---------------------------------------------------------------------+ // | Licensed | http://www.apache.org/licenses/LICENSE-2.0 ) | // +---------------------------------------------------------------------+ // | Author | Bigotry <3162875@qq.com> | // +---------------------------------------------------------------------+ // | Repository | https://gitee.com/Bigotry/OneBase | // +---------------------------------------------------------------------+ namespace app\admin\logic; /** * 友情链接逻辑 */ class Blogroll extends AdminBase { /** * 获取友情链接列表 */ public function getBlogrollList($where = [], $field = true, $order = '', $paginate = 0) { return $this->modelBlogroll->getList($where, $field, $order, $paginate); } /** * 友情链接信息编辑 */ public function blogrollEdit($data = []) { $validate_result = $this->validateBlogroll->scene('edit')->check($data); if (!$validate_result) : return [RESULT_ERROR, $this->validateBlogroll->getError()]; endif; $url = url('blogrollList'); $result = $this->modelBlogroll->setInfo($data); $handle_text = empty($data['id']) ? '新增' : '编辑'; $result && action_log($handle_text, '友情链接' . $handle_text . ',name:' . $data['name']); return $result ? [RESULT_SUCCESS, '操作成功', $url] : [RESULT_ERROR, $this->modelBlogroll->getError()]; } /** * 获取友情链接信息 */ public function getBlogrollInfo($where = [], $field = true) { return $this->modelBlogroll->getInfo($where, $field); } /** * 友情链接删除 */ public function blogrollDel($where = []) { $result = $this->modelBlogroll->deleteInfo($where); $result && action_log('删除', '友情链接删除' . ',where:' . http_build_query($where)); return $result ? [RESULT_SUCCESS, '删除成功'] : [RESULT_ERROR, $this->modelBlogroll->getError()]; } } ~~~ OK 了,逻辑代码写完了,下面添加一个友情链接表,Sql如下。 ~~~ -- ---------------------------- -- Table structure for `ob_blogroll` -- ---------------------------- DROP TABLE IF EXISTS `ob_blogroll`; CREATE TABLE `ob_blogroll` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` char(50) NOT NULL DEFAULT '' COMMENT '链接名称', `img_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '链接图片封面', `url` varchar(255) NOT NULL DEFAULT '' COMMENT '链接地址', `describe` varchar(255) NOT NULL DEFAULT '' COMMENT '描述', `sort` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '排序', `status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '数据状态', `create_time` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '创建时间', `update_time` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '更新时间', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='友情链接表'; -- ---------------------------- -- Records of ob_blogroll -- ---------------------------- ~~~ 数据表也搞定了,那么下面来看看业务逻辑中的validateBlogroll如何实现。 ~~~ <?php // +---------------------------------------------------------------------+ // | OneBase | [ WE CAN DO IT JUST THINK ] | // +---------------------------------------------------------------------+ // | Licensed | http://www.apache.org/licenses/LICENSE-2.0 ) | // +---------------------------------------------------------------------+ // | Author | Bigotry <3162875@qq.com> | // +---------------------------------------------------------------------+ // | Repository | https://gitee.com/Bigotry/OneBase | // +---------------------------------------------------------------------+ namespace app\admin\validate; /** * 友情链接验证器 */ class Blogroll extends AdminBase { // 验证规则 protected $rule = [ 'name' => 'require|unique:blogroll', 'url' => 'require', 'sort' => 'require|number', ]; // 验证提示 protected $message = [ 'name.require' => '链接名称不能为空', 'name.unique' => '链接名称已存在', 'url.require' => '链接URL不能为空', 'sort.require' => '排序值不能为空', 'sort.number' => '排序值必须为数字' ]; // 应用场景 protected $scene = [ 'edit' => ['name','url','sort'], ]; } ~~~ 好啦,控制器-逻辑-验证 都搞定啦。。不过不要急还没结束呢,再来看看 最后一步 视图 和 模型。