【问题标题】:Laravel 5 - Custom helper class with global accessLaravel 5 - 具有全局访问权限的自定义助手类
【发布时间】:2016-05-23 09:18:53
【问题描述】:

我的自定义助手类是这样的:

位于app/bootstrap/ACL.php

class ACL {

    public function __construct(){
       //Get user from session
       //Get user permission array
    }

    public function isAllowed($key){
        return 'calling from ACL class';
    }

} 

现在,我需要在所有项目控制器中访问这个 ACL 类。所以 我在我的 app.php 文件中require_once 'ACL.php';

然后在我的控制器中,我可以执行以下操作:

class UserController extends Controller {

    public function editDetails() {

        $acl = new \ACL();
        echo $acl->isAllowed('edit-details');

        //below is the code to edit details

    }
}

这段代码有效,但我觉得应该有一个 Laravel 5 正确的方法来做到这一点。 我想知道:

1) 这种方法可行还是有更好的方法来实现?

2) 如果不在每个控制器中执行$acl = new \ACL();,我可以使用全局变量吗? 或者类似ACL::isAllowed('edit-details');

3) 如何在 Blade 模板中正确运行 $acl->isAllowed('edit-details') 条件?

非常感谢!

【问题讨论】:

    标签: php laravel-5 acl ioc-container rbac


    【解决方案1】:

    为什么不简单地将它添加到类映射中?

    "autoload": {
      "classmap": [
          "app/Classes"
      ]
    }
    

    然后您可以将您的课程放入应用程序下的“/classes”文件夹中并使用它。 跑吧

    composer dump-autoload
    

    然后你就可以像 laravel 中的其他类一样使用它了。

    【讨论】:

      猜你喜欢
      • 2017-08-27
      • 1970-01-01
      • 2013-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-21
      • 2017-08-28
      • 2017-11-06
      相关资源
      最近更新 更多