# :-: 编写Helper
## 编写助手函数
```php
use think\permissions\facade\Permissions;
use think\permissions\facade\Roles;
/**
* 是否有权限
*
* @time at 2018年09月29日
* @param $permission => controller@action
* @return bool
*/
if (!function_exists('can')) {
function can($permission)
{
$module = request()->module();
list($controller, $action) = explode('@', $permission);
// 这里就需要获取登录用户信息,来进行权限判断
$user = request()->session('config.permission.user');
$roleIDs = $user->getRoles(false);
$permission = Permissions::getPermissionByModuleAnd($module, $controller, $action);
$permissions = [];
foreach ($roleIDs as $role) {
$permissions = array_merge($permissions, (Roles::getPermissions($role, false)));
}
if (!in_array($permission->id, $permissions)) {
return false;
}
return true;
}
}
```
## composer 自动加载
利用 composer.json 的 files 就可以自动加载了 助手函数了, 无需手动 require
```
// 测试用的
"autoload": {
"files": [
"vendor/thinkphp-permission/src/helper.php"
]
},
// 实际composer 包内
"autoload": {
"files": [
"src/helper.php"
]
},
```