Tencent / Biny

Biny is a tiny, high-performance PHP framework for web applications
BSD 3-Clause "New" or "Revised" License
1.69k stars 259 forks source link

新建的Service没有生效 #134

Open gmplato opened 3 years ago

gmplato commented 3 years ago

我在service中建一个新的xxxService.php.结构与privilegeService一样。但Action中没有效果。是什么会事?

gmplato commented 3 years ago

protected function xxx() { return array( 'check_basic' => array( 'actions' => '*', 'params' => [],
'callBack' => [], ) ); } 没有效果

gmplato commented 3 years ago

在Action中直接使用$this->xxxService->check_basic(); 也是可以的,但用上面的格式引用就没有效果了,将check_basic()函数复制到privilegeService也是可以生效的。就是另建一个xxxService不行,不知道是那是理解错了

billge1205 commented 3 years ago

文件是放在service目录下吗 你class名称是跟xxxService保持一致的吧 另外namespace 和 use 都确认一下哈

billge1205 commented 3 years ago

protected function xxx() { return array( 'check_basic' => array( 'actions' => '*', 'params' => [], 'callBack' => [], ) ); } 没有效果

这样定义 然后获取 $this->xxxService->check_basic(); ??没有这种用法 xxxService里 定义 public function check_basic 然后才能调用 $this->xxxService->check_basic(); 一定要public哦

billge1205 commented 3 years ago

privilegeService 比较特殊 是框架的鉴权service 鉴权逻辑都写在/lib/business/Action.php 中的 valid_privilege 函数里了

正常使用service 就是 $this->xxxService->check_basic() 等同于 (new xxxService())->check_basic()

gmplato commented 3 years ago

哦,原来privilegeService是特殊定义的,我原来以为每定义一个userauthService.php都可以这样在Action中定义这种鉴权格式。因为我想另外定义一种鉴权方法。那只好将方法定义在privilegeService了。

gmplato commented 3 years ago

还有valid_privilege()的 if (method_exists($this, 'privilege') && $privileges = $this->privilege())

$this->privilege() 这个定义是不是错了

billge1205 commented 3 years ago

$this->privilege() 是当前xxxAction 定义的鉴权声明 有的话就读出来处理 鉴权逻辑还是在privilegeService里的 xxxAction 里定义的privilege 只是个array 里面是鉴权配置项 例如:

protected function privilege()
    {
        return array(
            // 登录验证(在privilegeService中定义)
            'login_required' => array(
                'actions' => '*', // 绑定action,*为所有method
                'params' => [],   // 传参(能获取到$this,不用另外传)可不传
                'callBack' => [], // 验证失败回调函数, 可不传
            ),
            'my_required' => array(
                'actions' => ['index'], // 对action_index进行验证
                'params' => [$this->key],   // 传参
                'callBack' => [$this, 'test'], // 验证失败后调用$this->test()
            ),
        );
    }
gmplato commented 3 years ago

哦,好的,谢谢