laravel 5.2中如何自动加载自定义的类?
巴扎黑
巴扎黑 2017-05-16 16:49:26
0
2
469

1、在 appLibraires 下定义一个类 QrCode.php

<?php
/**
 * Created by PhpStorm.
 * User: AIMPER
 * Date: 2016/11/30
 * Time: 10:13
 */

namespace App\Libaries;
use Endroid\QrCode;
use Illuminate\Support\Facades\DB;

class QrCode{

    public static function generateQrCode($type = null, $id = null){
        $code = random_string(32,true);
        $create_date = time();
        $expires = 0;

        $qrcodeType = DB::table('qrcode_type')->where('id','=',$type)->select('code','params')-first();
        return $qrcodeType;

    }

}

2、调用该类的方法

...
use App\Libaries\QrCode;
class TestController extends Controller{
    public function index(){
        QrCode::generateQrCode(11,1);
    }
}

3、报错信息

ReflectionException in Route.php line 286:
Class App\Libaries\QrCode does not exist

4、解决尝试
有使用 composer dump-autoload 过,但是类还是没法加载进来,请问如何把自定义的类自动加载到项目中?

巴扎黑
巴扎黑

全部回复(2)
迷茫

在路由中通过依赖注入获取的类,需要注册至容器。

滿天的星座

查看下项目根目录下的composer.json文件的设置。

"autoload": {
    "files":[
        "app/helpers.php"
    ],
    "psr-4": {
        "App\": "app/"
    }
},

而且报错发生在路由定义环节,需要看看route.php的设置

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!