Laravel Classroom image

Prashant  Nigam / Student / Web Technology

To post your Question Join Classroom
Lesson Topics's No:-  |1 | 2 | 3 | 4|Last
Lessons:-Autoloading Classes

Autoloading Classes
With many frameworks knowing where to put your files, and how to load class definitions can
be a tricky topic. However with Laravel there are no strict rules applied to the layout of your
application. Laravel’s auto loader is a clever library which simplifies the loading of classes with
various naming or sub-folder conventions. It is flexible enough to handle the addition of complex
libraries or packages with ease. Let’s have a look at the functions we have available.
18.1 Mappings
Mappings are the simplest method of loading classes, you can pass the auto loader an array
of Class name to file location key-value pairs and Laravel will handle the rest. It’s efficient
autoloader will only load the required class definition when the class is used. By default the
Autoloader mappings are set within the start.php file, you can however use the class from
anywhere, but the start.php file is a good choice due to it being loaded early. Let’s take a look
at a mapping..
1 <?php
3 // application/start.php
5 Autoloader::map(array(
6 'Cloud' => path('app').'libraries/ff/cloud.php',
7 'Tifa' => path('app').'libraries/ff/tifa.php',
8 ));
The path('app') is a handy helper method to retrieve the absolute path to your projects
application folder. You can also retrieve absolute paths to other folders using the path()
method, here is a short list.
Method Directory
path(‘app’) application
path(‘sys’) laravel
path(‘bundle’) bundles
path(‘storage’) storage
In the mapping example, you will see that we specify our Class name as the array index, and the
file and location as the value. So if we wish to use the Cloud class..
1 <?php
3 $c = new Cloud();Laravel’s autoloader will ‘detect’ (php magic methods) that a class definition needs to be loaded,
it will look at the mapping definitions to see if our Class exists there, and proceed to include()
the source.


Prashant  Nigam

Skills    Laravel

Qualifications :-
Location :-,,,

  Students (0)