Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

TheAthenA714's avatar

[L5] How to add custom php classes in L5 ?

Hello everyone!

I was developing a laravel 4.2 website with custom php classes, and now I want to migrate to laravel 5 to continue developing this "library". The way I did it in laravel 4.2 was by following this helpful StackOverflow answer : http://stackoverflow.com/a/17091089

So basically I created a "library" folder in my app folder, with a sub-folder named after my WIP library (let's call it CustomPhpLib). Then I added ""app/library" to my composer.json file, in the "autoload>classmap" array and I could just call "new CustomPhpLib" whenever I wanted (in a view or in a controller), and it worked.

However I'm not sure how to do that in laravel 5. Where should I put the "library" folder? How should I load it automatically?

I should point out that my library is not namespaced yet. I understand the basic principles of namespacing, PSR and all that, but I never used that kind of things before. I plan on making all those changes later, but I would like to make it work with L5 just like in L4.2 before.

I tried a very basic (and probably very wrong) approch by creating public/CustomPhpLib and adding "public/CustomPhpLib" to the autoload>classmap array, but it doesn't work, I get a 500 error whenever I try to use the class.

Any help on understanding how this works would be really appreciated.

0 likes
7 replies
Alias's avatar
Alias
Best Answer
Level 2

Everything in Laravel 5 is autoloaded using PSR-4, within the app/ directory. You can see this in the composer.json file.

Basically, you could create another directory within app, and then namespace your files in there as appropriate:

app/CustomStuff/CustomDirectory/SomeClass.php.

Then, within your SomeClass.php, make sure you namespace it:

<?php namespace App\CustomStuff\CustomDirectory;

class Someclass {}

Now, you can access this class using the namespace within your classes:

use App\CustomStuff\CustomDirectory\SomeClass;

16 likes
milon's avatar

create a folder on your root directory of laravel. suppose the name of the folder is Library. then add the following line to composer.json file.

"autoload": {
    "psr-4":{
            "ePOS\\": "app/ePOS"
        }
}

then namespace your class as psr-4 naming standard.

3 likes
TheAthenA714's avatar

Alias > Thank you very much, it's more simpler than what I expecting it to be. Everything works fine now!

Alias's avatar

No worries, I think that should work just did it from the top of my head.

bgarrison25's avatar

I like the selected answer but I feel like the second answer is more of a best practice. However I don't understand how if you make a folder named library, you then use "ePOS\": "app/ePOS" in the PSR-4.....that doesn't make any sense to me. Then again im pretty new to PSR-4

1 like
yogesh-mistry's avatar

@Alias Can app/CustomStuff/CustomDirectory/SomeClass.php be app/vendor/MYPDF/MYPDF.php containing class MYPDF {? Because it throws an error Class 'App\Vendor\FPDF\FPDF' not found while using use App\Vendor\FPDF\FPDF; in the controller which is instantiating the MYPDF class.

Please or to participate in this conversation.