Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

新建的Service没有生效 #134

Open
gmplato opened this issue May 10, 2021 · 9 comments
Open

新建的Service没有生效 #134

gmplato opened this issue May 10, 2021 · 9 comments

Comments

@gmplato
Copy link

gmplato commented May 10, 2021

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

@gmplato
Copy link
Author

gmplato commented May 10, 2021

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

@gmplato
Copy link
Author

gmplato commented May 10, 2021

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

@billge1205
Copy link
Collaborator

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

@billge1205
Copy link
Collaborator

billge1205 commented May 10, 2021

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
Copy link
Collaborator

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

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

@gmplato
Copy link
Author

gmplato commented May 10, 2021

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

@gmplato
Copy link
Author

gmplato commented May 10, 2021

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

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

@billge1205
Copy link
Collaborator

billge1205 commented May 10, 2021

$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
Copy link
Author

gmplato commented May 10, 2021

哦,好的,谢谢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants