Skip to main content

How to Include PHP Library File To Autoload in Laravel

PHP autoloading is a process of automatically loading classes into memory as they are needed. This mechanism eliminates the need to explicitly include classes in your code, which can save you time and typing. Laravel uses the PSR-4 autoloading standard, which requires that all class files be stored in a directory structure that mirrors the namespace of the classes.

When using Laravel, you may find yourself needing to use a PHP library that is not included in the standard autoload file. Starting from Laravel 5, the framework doesn’t have a folder to store library files by default. However, we can easily create a new folder under the App directory called Libraries (App/Libraries). Doing so will allow us to store all of our custom library files in one place.

This can be particularly helpful if we’re working on a large project with multiple developers. By storing our libraries in a central location, we can ensure that everyone is using the same versions of the files. Furthermore, it can help to prevent any accidental changes from being made to the files. In short, creating a Libraries folder is a simple way to keep our project organized and ensure that our libraries are always up-to-date.

Open composer.json and add the folder “app/Libraries” to autoload list.

"autoload": {
        "psr-4": {
            "App\\": "app/"
        },
        "classmap": [
            "database/seeds",
            "database/factories",
            "app/Libraries"
        ]
    },

By continuing to use the site, you agree to the use of cookies.