How to use Packages in Laravel 4?

8

I am new to the Laravel 4 framework. In Laravel 3 it was simple to use Bundles. In version 4 it was removed the use of the Bundles and started to use Packages, but I did not understand how it is used, even seeing in the documentation. For example, what folder is placed in the Composer where it is loaded etc.

    
asked by anonymous 13.12.2013 / 17:38

2 answers

9

The composer simplifies and unifies the distribution of code in PHP, there is not much difference, however bundles for Laravel 3 are mostly not compatible with Laravel 4.

By composer, each package you want to install is a dependency of the project, so by editing the composer.json file, you must add the package in question in the require session, to search the packages, you can use packagist.org

Here's an example, there is a Generator Package for Laravel 4, made by Jeffrey Way, if I want to use it in the project, I should add the package to composer.json in the require

"require": {
    "laravel/framework": "4.0.*",
    "way/generators": "dev-master"
},

The line "laravel/framework": "4.0.*" already existed, I added the line "way / generators": "dev-master"

This still does not do anything in the project, so that the library is downloaded, run the command at the root of the project:

php composer.phar update

Or if the composer is installed Globally in your environment

composer update

Once this is done, the package source will be available in the vendor folder, and it will be loaded automatically by the composer autoloader, you do not need to use include or require.

Each package can be used separately, or as in the case of the "Generators" package in the example, it integrates with Laravel 4, simply add this line:

'Way\Generators\GeneratorsServiceProvider'

to the app / config / app.php file in the session providers.

Before trying to understand the Packages for Laravel 4, try to study Composer a little bit more, it is a phenomenal and indispensable tool for PHP programmers.

I hope I have been clear.

    
13.12.2013 / 19:33
2

As a complement to the response that has already been given, I recommend using the installation of the desired package directly from the command line, since Laravel , because it has many dependencies, causes Composer to read all of them, checking library by library, to see if there is a new version.

There are cases where this behavior is not wanted, for example, when I want to install only a new library, do not mess with the others that are already installed (and avoid delays, of course).

So, for this, you use the command composer.phar require or composer require (if you have installed globally).

For example, to add a specific library, you could do this:

 composer require wallacemaxters/timer 1.*

It would install only the library mentioned above and would automatically include it as a dependency in your composer.json file.

    
03.08.2016 / 18:35