Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include external files in laravel project?

Tags:

laravel

So I have a public directory in my localhost that contains some classes that I want to be able to use in my current laravel project.

So how will I include these, external classes/files in laravel?

I tried to add these files in my composer.json file,

"autoload": {
        "classmap": [
            "database",
            "../public_classes/priceClass.php"
        ],
        "psr-4": {
            "App\\": "app/"
        },
        "files": [
            "../public_classes/priceClass.php"
        ]
    },

and do composer dump-autoload, then when I tried to call the function, it won't work.

like image 519
A Mendoza Avatar asked Nov 24 '25 18:11

A Mendoza


2 Answers

You don't need to add anything in composer.json if you're using PSR-4. For example, you can create PHP file in \app\MyClasses\MyClass.php and use this namespace in custom class: namespace App\MyClasses.

Laravel will autoload this class for you.

like image 155
Alexey Mezenin Avatar answered Nov 27 '25 23:11

Alexey Mezenin


I also wanted to include a few classes in my Laravel Project for that I just copied the files in a new directory(app/Libraries) and assigned Package name to all the classes and followed PSR-4 Autoloading standard for all those class files and I have added following code in my composer.json.

     ...
        "autoload": {
            "classmap": [
                "app/Libraries",
            ],
...

hope this will solve your problem...

Happy to help!!!

like image 22
Akshay Khale Avatar answered Nov 28 '25 01:11

Akshay Khale



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!