TP5框架 之自定义标签

一、创建控制器

<?php
namespace app\common\taglib;


use think\auth\Auth;
use think\template\TagLib;

class MyTag extends TagLib
{
    protected $tags = [
        'auth' => ['attr' => 'rule', 'close' => 1]
    ];

    /**
     * 权限判断标签
     * @param $tag
     * @param $content
     * @return string
     */
    public function tagAuth($tag, $content)
    {
        $rule = $tag['rule'];
        $auth = new Auth();
        $uid = session('uid');
        $res = $auth->check($rule, $uid);
        $parseStr  = '<?php if(' . intval($res) . '): ?>' . $content .'<?php endif; ?>';
        return $parseStr;
    }
}

二、配置

 

三、使用

{MyTag:auth rule='Group/index'}
<a href="{:url('Group/index')}">用户组</a> &nbsp;&nbsp;&nbsp;&nbsp;
{/MyTag:auth}

 

posted @ 2024-02-02 15:17  样子2018  阅读(52)  评论(0编辑  收藏  举报