How import custom class in controller Laravel?

As above, make sure it is placed in the App directory and make sure it is properly namespaced e.g.

<?php
  $fOne = new \App\library\functions;
  $isOk = ($fOne->isOk());
?>

You should create Library folder inside app folder

namespace App\Library\My

app folder is alrdy used psr-4

In your controller

use App\Library\My as My

It's work for me. Hope this answer is helpful


You have to properly namespace your every class.

So you can import your class with use keyword, like so

use App\Library\My;

....

$my = new My();

Or if you've conflicting class name then you can use as keyword to alias the classname while importing

use App\Library\My as MySecond;

....

$my = new MySecond();

And if you want to directly access your class within the method then you can access it like so.

$my = new \App\Library\My();

Note: The leading \ means App was declared in the global scope.