douyinlai2169 2016-06-23 18:21
浏览 31

你如何在laravel中包括一个全球课程

I would like to include a class for every controller to use. Here is what I tried with no luck.

In the Controller.php file, I added this line of code:

use App\Lib\RequestType;

Then in my controller UserController.php, I called a test function.

dd(RequestType::test());

But a fatal error is thrown.

Class 'App\Http\Controllers\RequestType' not found

What is laravel looking for the class in the Controllers folder. Shouldn't UserController inherit the Controller class?

Thanks in advance

  • 写回答

2条回答 默认 最新

  • douqin2108 2016-06-23 18:30
    关注

    If you are trying to write a static class and call it by use, then one option is to make use of autoload feature of composer, which will enable the class to be used anywhere.

    "autoload": {
            "psr-4": {
                "App\\": "app/"
            },
            "classmap": [
              "app/Lib"
            ]
        }, 
    

    Then in any controller

    use RequestType;
    

    then

    dd(RequestType::test());
    

    Note: Function test should be like

    public static function test()
    
    评论

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算