search

Home  >  Q&A  >  body text

How to automatically load custom classes in laravel 5.2?

1. Define a class QrCode.php

under app\Libraires
<?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. Call the method of this class

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

3. Error message

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

4. Solution attempt
I have used composer dump-autoload, but the class still cannot be loaded. How can I automatically load custom classes into the project?

巴扎黑巴扎黑2839 days ago548

reply all(2)I'll reply

  • 迷茫

    迷茫2017-05-16 16:51:26

    Classes obtained through dependency injection in routing need to be registered with the container.

    reply
    0
  • 滿天的星座

    滿天的星座2017-05-16 16:51:26

    Check the settings of the composer.json file in the project root directory.

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

    And the error occurred during the route definition process. You need to check the settings of route.php

    reply
    0
  • Cancelreply