How to use Packages in Laravel 4?

Asked

Viewed 650 times

8

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

2 answers

9


Composer simplifies and unifies the code distribution in PHP, there is not much difference, but 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 should add the package in question in the require section, to search for packages, you can use packagist.org

For example, there is a Laravel 4 Generator Pack made by Jeffrey Way, if I want to use it in the project, I should add the package to Composer.json, in the require session

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

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

This still does nothing in the project, for the library to be downloaded, run at the root of the project the command:

php composer.phar update

Or if Composer is installed Globally in your environment

composer update

Once this is done, the package source will be available in the vendor folder, and will be automatically loaded by Composer autoloader, no need to 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'

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

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

I hope I was clear.

  • That was exactly the use I was not understanding, about the Poser already know some things of it, the problem was in the same places, thank you very much

  • 1

    Consider reading the documentation for Each Package, virtually all packages for Laravel 4 have installation instructions

2

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

There are cases where this behavior is not desired, for example when I want to install only a new library, without messing 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 it has been installed globally).

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

 composer require wallacemaxters/timer 1.*

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.