Laravel is a modern and robust PHP framework. There are several tutorials available here on CodeForest, but I still receive plenty of questions on getting started with Laravel, How to use Composer and similar.

Composer

Composer is a dependency manager which installs several packages inside a vendor folder of your project or app. It basically deals with dependencies using a composer.json file which is included in the root of your app. It also deals with dependencies inside libraries.

It checks all the needed libraries and their dependencies and resolves, downloads and install everything inside a vendor folder.

Installing Composer

Windows

I will start with Windows, where it is the easiest to install it. Download the installer and follow the instructions. It will install composer globally and set PATH environment variable so you can just type composer from any folder in your command line. If it does not work after installation, restart you computer.

Verify everything is in order by running:

[code lang=”html”]
composer –version
[/code]

I suggest installing excellent console for Windows called Cmder.

Linux/Mac

In Linux, you can install Composer globally or locally.
To install globally, run:

[code lang=”html”]
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
[/code]

Now you can just run composer instead of php composer.phar

To install locally:

[code lang=”html”]
curl -sS https://getcomposer.org/installer | php — –install-dir=bin
[/code]

You can adjust install-dir to your liking, I suggest to install it in your project folder. Now you will have to run php composer.phar

Sometimes when you run composer you will get a message that it is too old. If you get that message, just run:

[code lang=”html”]
composer self-update
[/code]

and Composer will update to the latest version.

Using composer

Open up your console/terminal to start using Composer. Composer uses Packagist to search for available packages, you can search directly using Composer like this:

[code lang=”html”]
composer search monolog
[/code]

Result will look something like this:

composer search

This is handy, but it is better to search directly on Packagist website.

You can do everything from command line, when I need to add only one library, I just find a package and version on Packagist and write:

[code lang=”html”]
composer require monolog/monolog
[/code]

after which you will be asked which version do you want to install. For development purposes you can install dev-master version, but in production I would suggest a full version, which, in the case of Monolog, is 1.7.0. When you enter 1.7.0 and press enter, Composer will install Monolog and all it’s dependencies inside your vendor folder.

Composer will also generate an autoload.php file which will be used for autoloading freshly installed libraries.

Laravel

Ok, we are now ready to install Laravel framework. Simply enter this in your command line:

[code line=”html”]
composer create-project laravel/laravel project-name –prefer-dist
[/code]

This will download and install a fresh Laravel framework project in project-name folder and generate autoload file. It will take some time to download everything needed. You will now see what Composer is all about if you look at it’s output.

After everything is finished, you can browse through installed files. You will notice a composer.json file in the root of your project which you can use to add more dependencies. We will now add Way generators package, just to show how to do it.

Go to Packagist.org and search for “Way generators”, then click on it to open it. You will see something similar to this:

way laravel generators

I have clicked on stable version 1.1, just so you see it…now copy/paste the require line inside require section of your composer.json file, just behind laravel/framework so it now looks like this:

[code lang=”html”]
…..
"require": {
"laravel/framework": "4.1.*",
"way/generators": "1.1"
},
…..
[/code]

Save the file and run this on command line (be sure to run it inside your project-name folder that you used while creating project):

[code lang=”html”]
composer update
[/code]

Ok, now you installed Way generators, but in order to use it, there are some more steps. You can find more either by clicking the Canonical link on Packagist, or inside /vendor/way/generators/readme.md file.

I will help you here, you just need to open /app/config/app.php file and add this line:

[code lang=”php”]
‘Way\Generators\GeneratorsServiceProvider’
[/code]

into providers array (probably below workbench provider).

You can run:

[code lang=”html”]
php artisan
[/code]

and you will see a list of all available artisan commands, among which you will see generate:* section added by Way generators.

Let’s now test Laravel installation by running (if you have PHP 5.4+ installed, which would be great)

[code lang=”php”]
php artisan serve
[/code]

and point your browser to http://localhost:8000. You should see a famous You have arrived logo and be ready to create The next big thing…

I prefer adding a local folder for my own classes inside the project which will hold my custom services, validators and other stuff. So, create folder inside your /app folder, name it wahtever you want, I will call it Codeforest.

We need to add this newly created folder to be auto loaded, so I will edit my composer and add it to PSR-4 like this (more on this in next parts):

[code lang=”php”]

"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
],
"psr-4": {
"Codeforest\\" : "app/Codeforest"
}
},

[/code]

Every time we change autoload in composer, we need to regenerate the autoload file, so run:

[code lang=”html”]
composer dump-autoload
[/code]

I hope that this overview will help the beginners to better understand what is going on in all this Laravel tutorials. In next part, we will continue with Getting started with Laravel – Basics series.

If you still have any questions, do not hesitate to ask them in the comments section.