1. Laravel Basics
If youre going to be doing PHP development, you should download Laravel. Symfony and Laravel are the most popular and best frameworks for PHP in the world. To get Laravel on your machine you can follow the . You will need PHP7 with the OpenSSL, PDO, Mbstring, Tokenizer, and XML PHP. It also requires PHP version 5.6.4 or above enabled. You know how to install those, right? Awesome. But just in case, on Ubuntu, use the following code.
Install PHP and a Few Dependencies
> sudo add-apt-repository ppa:ondrej/php
> sudo apt-get update
> sudo apt-get install php7.0 php7.0-curl php7.0-mcrypt
Now, to create a new laravel app we simply follow the instructions provided to us on https://laravel.com/docs/5.3/installation#installing-laravel inside the folder named designpatterns . As you build out various applications in the book, consult the git branch for each chapter so you git Jedis can trace along.
The next thing you will do is look at Composer. Laravel is built off of about 20 Composer packages; Composer is the cats meow (see Figure for important information).
What Is Composer?
Composer is the dependency management tool for PHP. It allows you to list the packages your application depends upon to function correctly. A file in the root of the project named composer.json allows for plenty of configuration options, so lets brush over some of those.
Composer does several neat things:
Dependency management with packages
PSR and custom file-based autoloading
Compiler optimization to help code run faster
Custom hooks into lifecycle events, such as application installed, updated, or first created.
Stability checking
With your favorite text editor, open up the composer.json file inside the project root. Note that throughout this book file names will all be relative to the project root. Just to be clear here, when I say project root , that means directly inside the designpatterns folder you created, so app/MODELS/User.php is actually the path /home/kelt/book/designpatterns/app/models/User.php on my machine.
Meta Information
In the first part of the Composer manifest, you see basic meta information .
composer.json
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
All of this information is used by a web site called , which catalogs packages out there in the wild. As a standard practice, if you create packages to host on Packagist youll probably want the name the same as the GitHub repository for that package.
Dependency Management
Next you see a require block. Here is where package dependency management comes into play. Currently you are only requiring the Laravel framework, which is made up of many other packages; however, as time goes on you will add additional packages.
composer.json
"require": {
"php": ">=5.6.4",
"laravel/framework": "5.3.*"
},
Seems pretty straightforward, right? One gotcha here though is that you might see a 4.1 or >=1.0,<1.1 | >=1.2. Visiting .
Name | Example | Description |
---|
Exact version | 1.0.2 | You can specify the exact version of a package. |
Range | >=1.0 | By using comparison operators you can specify ranges of valid versions. |
>=1.0,<2.0 | Valid operators are >, >=, <, <=, !=. |
>=1.0,<1.1 | >=1.2 | You can define multiple ranges, separated by a comma, which will be treated as a logical AND. A pipe symbol | will be treated as a logical OR. AND has higher precedence than OR. |
Wildcard | 1.0.* | You can specify a pattern with a * wildcard. 1.0.* is the equivalent of >=1.0,<1.1. |
Tilde Operator | 1.2 | Very useful for projects that follow semantic versioning. 1.2 is equivalent to >=1.2,<2.0. |
Although it isnt shown here, you could add a mapping for development only packages by using require-dev. Some good candidates for development only packages are behat , phpspec , and clockwork .
Autoloading
Earlier I mentioned that Composer comes with an autoloader and even optimizes the PHP to run faster. It knows how to do this because of the autoload section.
composer.json
"autoload": {
"classmap": [
"database"
],
"psr-4":{
"App\\": "app/"
}
},
You can also use PSR autoloading. If youve never heard of PSR, take a moment and go to http://petermoulding.com/php/psr . Basically it deals with standardizing the folder structure, namespace, and class names of PHP.
Laravel 5 uses psr-4 autoloading unlike it's predecessor Laravel 4. If you look inside of composer.json you will notice these lines
"psr-4": {
"App" : "app/"
}
This allows us to reference a file such as app/Services/FooService.php with the fully qualified namespace `use App\Services\FooService;` inside another php file.
Now your application will look inside the app folder and autoload any files from that directory for you. Pretty nifty, right?
Lifecycle Hooks/Scripts
Below are a list of scripts you execute after running composer install or composer update or composer create-project (respectively).
composer.json
"scripts": {
"post-root-package-install": [
"php -r \"file_exists('.env') || copy('.env.example', '.env');\""
],
"post-create-project-cmd": [
"php artisan key:generate"
],
"post-install-cmd": [
"illuminate\\Foundation\\ComposerScripts::postInstall",
"php artisan optimze"
],
"post-update-cmd": [
"illuminate\\Foundation\\ComposerScripts::postUpdate",
"php artisan optimize"
],
},
You can tap into certain events of composer if you want to run custom commands here. I use these hooks to automatically run things like migrations any time composer install is executed on the server. When you deploy to production servers, you just follow this simple two-step process:
You dont have to remember to run migrations or clean assets or whatever else because you do that after composer install finishes running.
What is the difference between composer install and composer update?
Running composer update will do two things.
It will update all required packages to the latest version matched.