Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I autoload a custom class in Laravel 5.1?

I've created a library folder within the app folder to add my own classes.

enter image description here

This is the content of the file app/library/helper.php:

<?php

namespace Library;

class MyHelper
{
    public function v($arr)
    {
        var_dump($arr);
    }
}

I added the namespace to composer.json:

enter image description here

and then I ran

$ composer dump-autoload

but it does not seem to have any effects.

The files

  • vendor/composer/autoload_psr4.php
  • vendor/composer/autoload_classmap.php

did not change.

If I try to create an instance of MyHelper, Laravel reports the following error:

enter image description here

I'm not sure what I am doing wrong.

like image 520
GerBawn Avatar asked Aug 10 '15 11:08

GerBawn


People also ask

What is laravel autoload?

Auto-Loading allows you to load class files when they are needed without explicitly loading or including them. This gives you ease in running your application by loading those files automatically which are needed every time. Laravel is built to work with Composer.

What is dump autoload in laravel?

php artisan dump-autoload. It regenerates the list of all the classes that need to be included in the project (autoload_classmap. php). It will 'recompile' loads of files creating the huge bootstrap/compiled.php. It wont't download any new thing to the project.

What is autoload class in PHP?

The spl_autoload_register() function registers any number of autoloaders, enabling for classes and interfaces to be automatically loaded if they are currently not defined. By registering autoloaders, PHP is given a last chance to load the class or interface before it fails with an error.

What is PSR 4 autoloading standard laravel?

This PSR describes a specification for autoloading classes from file paths. It is fully interoperable, and can be used in addition to any other autoloading specification, including PSR-0. This PSR also describes where to place files that will be autoloaded according to the specification.


3 Answers

Use files directive in composer.json: https://getcomposer.org/doc/04-schema.md#files

{
    "autoload": {
        "files": ["app/library/helper.php"]
    }
}
like image 181
Maxim Lanin Avatar answered Oct 19 '22 13:10

Maxim Lanin


Use composer.json :

   "autoload": {
    "classmap": [
        "database",
        "app/Transformers"
    ]
 },

Add your auto load directories like I added app/Transformers.

Don't Forget to add run composer dump-autoload.

The only problem with this method is you need to run composer dump-autoload whenever you add new class do that directory.

Or You can use "Files" in composer.json.

"autoload": {
    "files": ["src/MyLibrary/functions.php"]
}
like image 39
Rutvik Bhatt Avatar answered Oct 19 '22 13:10

Rutvik Bhatt


Your autoloading configuration is almost good, but you have

  • got the namespaces wrong
  • got the paths wrong

To fix the problem, adjust your autoloading configuration:

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

Then rename the directory /library to /Library (note the case).

Then rename the file /app/Library/helper.php to /app/Library/MyHelper.php (note how class name should match the file name).

Then adjust the namespace of the class provided by /app/Library/MyHelper to match the PSR-4 prefix (and thus the structure of your project), as well as usages of the class:

namespace App\Library;

class MyHelper 
{
    public function v($arr)
    {
        var_dump($arr);
    }
}

For reference, see:

  • http://www.php-fig.org/psr/psr-4/
like image 35
localheinz Avatar answered Oct 19 '22 11:10

localheinz