dongsheng8158 2016-05-23 09:18
浏览 111

Laravel 5 - 具有全局访问权限的自定义助手类

My custom helper class is this:

Located in 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';
    }

} 

Now, I need to access this ACL class inside all of the project controllers. So I require_once 'ACL.php'; in my app.php file.

Then inside my controller I can do the following:

class UserController extends Controller {

    public function editDetails() {

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

        //below is the code to edit details

    }
}

This code works, but I feel there should be a Laravel 5 proper way to do this. I want to know:

1) Is this approach OK or is there a better way to achieve this?

2) Without doing $acl = new \ACL(); in every controller, can I use a global variable? Or something like this ACL::isAllowed('edit-details');

3) How can I run $acl->isAllowed('edit-details') condition in Blade templates properly?

Thanks a lot!

  • 写回答

1条回答 默认 最新

  • douchui4815 2016-05-23 09:26
    关注

    Why don't you simply add it to the classmaps?

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

    You can then put your class into the "/classes" folder under app and just use it. Just run

    composer dump-autoload
    

    and then you can use it like other classes in laravel.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用