How to load my own code into Laravel? Differences between those ways?

I'm kind of confused on how to load my own code (classes or just regular functions) into a Laravel app. I've seen this done in several ways:

  1. Creating a folder inside the app directory (for example: app/libs) and add app_path().'/libs' to start/global.php
  2. Add it into composer.json's "require"
  3. Add "psr-0" into composer.json's "autoload" and add there the files
  4. Add a My\Custom\Service\Provider into app/config/app.php's 'providers' and the alias for the facade

What's the difference between them? Why and when should I use any of those ways? Should I load a class, several .php files or simply the folder? Maybe reference those 3 things at the same time?

EDIT:

These are my guesses:

  • Option 2 is just for packages
  • Option 3 if you want to load every class inside a custom namespace declared within the new created folder (don't get why the "psr-0" instead of just adding it to "classmap")
  • Option 1 is the same as option 3, just handled by Laravel instead of Composer
  • You can reference a folder and it will load every class found inside, or you can reference a certain file and it will load the class found inside

About option 4:

  • If you want to use the facade anywhere on your code, and that will need the namespace added into composer.json

EDIT 2:

  • If you add them to "classmap":

    "classmap": [
        "app/libs"
    ]
    

    every class from any namespace within files inside the app/libs folder, will be loaded

  • If you add them to "psr-0":

    "psr-0": {
        "Libs": "app/"
    }
    

    it will load every class within the Libs namespace inside the app/libs folder

Still not sure why/when to use service providers and aliases.

EDIT 3:

"psr-0" if I want to load a namespace that follows a folder structure (it won't load a class within a subnamespace if it doesn't match the folder structure)

"classmap" for "random" classes, functions... sort of "the rest"


ANSWERS:


you can load your own code 2 (maybe 3) ways in laravel.

  1. use composer
  2. use ClassLoader
  3. Manual include or require anywhere

Option 2 is just for packages

yes, you're right.

Option 1 is the same as option 3, just handled by Laravel instead of Composer

yes, you're right.

Option 3 if you want to load every class inside a custom namespace declared within the new created folder (don't get why the "psr-0" instead of just adding it to "classmap")

some packages or classes adhere psr-0 standard, the rest is not. psr-0 option is mapping namespace to directory. the classmap is mapping the namespace to certain directory or file and used for the class that is not adhere psr-0 standard.

You can use the classmap generation support to define autoloading for all libraries that do not follow PSR-0/4.

If you want to use the facade anywhere on your code, and that will need the namespace added into composer.json

nope, instead, you have to add class alias for the facade in app/config/app.php

  1. if your code is just file, not a class, then use composer autoload files
  2. if your code is class but not adhere psr-0/4 standard, use composer autoload classmap or just add the containing directory to app/start/global.php.
  3. otherwise, use composer autoload psr-0 or psr-4.

in Laravel 4,I add all of my class in "mylibrary" folder.

Then at app/start/global.php , I add app_path().'/mylibrary',

ClassLoader::addDirectories(array(

    app_path().'/commands',
    app_path().'/controllers',
    app_path().'/models',
    app_path().'/database/seeds',
    app_path().'/mylibrary',
));

Within mylibrary there is MyClass.php , within MyClass.php there is test_myfunction()

and at app/view/home.blade.php I add these code :

<?php
  $FMyClass11 = new MyClass;
  $just_test=($FMyClass1->test_myfunction());
?>

Hope it works for you. :)



 MORE:


 ? Class 'Role' not found
 ? Laravel 4 custom package not appearing in autoload_namespace.php
 ? Laravel 5 - Creating Artisan Command for Packages
 ? Laravel 5 - Creating Artisan Command for Packages
 ? Laravel 5 - Creating Artisan Command for Packages
 ? Create artisan commands for package Laravel 5
 ? How to call Model via artisan command in Laravel 5?
 ? Laravel 5 php artisan not running in Vagrant, missing vendor/services.json?
 ? Laravel 5 php artisan not running in Vagrant, missing vendor/services.json?
 ? Laravel 5 php artisan not running in Vagrant, missing vendor/services.json?