How to add custom class in laravel 5.4.10

Viewed 1664

I am new in laravel. I am writing following code in the controller method.

include_once(app_path() .'/Classes/Pgp_2fa.php');
$pgp = new Pgp_2fa();

Following errors are shown.

FatalThrowableError in AuthController.php line 146: Class 'App\Http\Controllers\Pgp_2fa' not found

6 Answers

You have to use the correct namespace for your class.

If you are not using any namespace, you should add a \ in front of the class to let php know that the class exists in the root namespace. Otherwise php will look in the current namespace which is App\Http\Controllers when you are in a controller.

$pgp = new \Pgp_2fa();

This is not just Laravel behavior but php in general.

When you add new classes there are couple way to use them. (I mean, load them)

If your classes has unique name and don't use namespace, then you can add backslash \ start of the class.

$class = new \Pgp_2fa;

But if you want to define them namespaces and use with same name so many times, then you have to add these classes in composer.json. Open your composer.json and come into "autoload" section and define class directory in classmap

"autoload": {
        "classmap": [
            "database/seeds",
            "database/factories",
            "app/Classes",     // so all classes in this directory are going to load     
        ],

And don't forget to say composer dump-autoload after these changes and creating new classes

If you don't want to dump your composer when you add a new class, then you may want add those classes inside of the psr-4 section in composer

"autoload": {

    "psr-4": {
        "App\\": "app/",
        "App\\Classes\\": "app/Classes",

    }
},

you need to add namespace for your custom class or just add slash on your class name. like :

$pgp = new \Pgp_2fa();

You can call inside the constructor like below, and you can use the $Pgp_2fa variable in remaining function

 class controllername extends Controller
      {
        public function __construct(Route $route,Request $request)
          { 
            $Pgp_2fa = new \Pgp_2fa();
          }
      }

You can easily add your custom classes by added it to Composer's autoload array. Go to composer.json and add it to:

"autoload": {
    "files": [
      "app\\Classes\\Pgp_2fa.php"
    ]
  },

Now in your app folder, under classes folder add your Pgp_2fa.php file which will have your class. You can use it anywhere you want, it should be automatically auto-load-ed.

After adding your class write command:

composer dumpautoload to refresh autoload class mapping.

For example, There is our class Common.php in a directory called Mylibs in app directory.

app/mylibs/Common.php we need to namespace App\Mylibs;

namespace App\Mylibs;

class Common {

   public function getSite() {
        return 'AmirHome.com';
    }

}

Now, you can access this class using the namespace within your classes just like other Laravel classes.

use App\Mylibs\Common

in Controller:

namespace App\Http\Controllers;

use App\Mylibs\Common;

class TestController extends Controller {

   protected $common;

   public function __construct(Common $common) {
       $this->common = $common;
   }

   public function index() {
       echo $this->common->getSite();
   }     
}
Related