Showing posts with label composer. Show all posts
Showing posts with label composer. Show all posts

February 17, 2015

Getting started with Laravel 5 and Composer: Initializing a new project

Laravel 4 had largely changed the way that PHP development happens. It saw the addition of package management through Composer, an overhaul of pagination routes, and some of the best "out-of-the-box" security seen in any PHP framework. The level of care and attention to detail made using Laravel 4.2 a ton of fun.

Laravel 5 changes things again. It was supposed to be a simple upgrade to 4.3, but a slew of new features were enough to call for a new major release. There were changes made to the directory structure, blade templates allow for escaped variables, facades are taking a front row seat, routing receive the addition of middleware, and Artisan has tons of new generators. I plan on covering some of these new feature in upcoming posts.

For this post, however, how does one even get started? Well, let's set up a basic Laravel 5 PHP project from scratch.

Install Composer

Composer configures packages from Packagist. Add a package like so:
composer require "vendor/package": "version@branch"

Package dependencies are automatically included and installed as listed in the composer.lock file. These are installed into the {project root}/vendor folder.

Install Laravel 5 with Composer


composer create-project laravel/laravel mylaravel5project

Boot up Laravel 5 homepage


Can be done with PHP's built-in webserver, document root set to the Laravel project's public folder:

cd mylaravel5project
php -S localhost:8888 -t public

Now open http://localhost:8888 in your browser. If this doesn't work, it is likely a PHP5-mcrypt dependency that's missing, so be sure to add that. Alternatively, use a virtual machine such as Homestead.

February 7, 2015

Installing PHP Composer globally on Windows

Composer is a dependency manager for PHP. PEAR used to be the go-to tool for managing PHP dependencies quite some time ago, but it was always cumbersome, and not much fun to use. Composer makes it very easy!

On Windows, you can use the .exe installer; note you will need PHP installed, and have the php.exe accessible. In this particular case, I will be using Cygwin shell, and have already installed PHP as a Cygwin package for it:


When installation is done, close and reopen your terminal:


You can now run the composer command from anywhere in your shell.

composer
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 1.0-dev

Installing PHP Composer globally on Mac & Linux

Composer is a dependency manager for PHP. PEAR used to be the go-to tool for managing PHP dependencies quite some time ago, but it was always cumbersome, and not much fun to use. Composer makes it very easy!

Installing Composer is easy on Mac or Linux:
curl -sS https://getcomposer.org/installer | php

If you want to run it globally, simply move and rename the downloaded .phar file:
mv composer.phar /usr/local/bin/composer

It might even be possible to set it all up in a single command, though I cannot test it right now:
curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

You can now run the composer command from anywhere in your shell.

composer
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 1.0-dev

October 29, 2014

First introduction into Laravel 4 PHP Homestead, getting started from scratch

Update: This post is now deprecated with the release of Homestead 2.0, which runs Laravel 5. A new post is here, and this one will be kept for historical reference.



I've been enjoying using the Laravel PHP framework over the past year or so, but I always found myself referring back to documentation when I start up a new local project. There are always changed dependencies, and lots of other miscellaneous changes, and I would need to update my Vagrantfile and provisioner software accordingly. This Laravel-Vagrant blog post by culttt was a staple of information for me for a long time, but as of the time of this posting, has been deprecated for a while. Luckily for us, there is a new kid on the block, and this one is officially supported and documented!

Enter Homestead, a pre-packaged Vagrant box with all the dependencies provisioned for, to run a new Laravel project. This was a breeze to get going even on my Windows machine (and I prefer using Cygwin with a Console2 shell as well, which usually requires extra effort finding dependencies than Ubuntu's apt-get or OSX's homebrew), and everything simply worked. Just be sure to have the latest version of Vagrant and Virtualbox installed on your computer!

Let's add the Homestead Virtualbox:

vagrant box add laravel/homestead

If you have an older version of Vagrant, you will likely get the error below:

This command was not invoked properly. The help for this command is
available below.

Usage: vagrant box add <name> <url> [--provider provider] [-h]

        --checksum VALUE             Checksum
        --checksum-type VALUE        Checksum type
    -c, --clean                      Remove old temporary download if it exists.
    -f, --force                      Overwrite an existing box if it exists.
        --insecure                   If set, SSL certs will not be validated.
        --cacert certfile            CA certificate
        --cert certfile              The client SSL cert
        --provider provider          The provider that backs the box.
    -h, --help                       Print this help

To fix it, I simply upgraded from the failing version 1.4.3, to the most up-to-date version 1.6.3 as of this writing. Just download the newest installer for Vagrant and set it up, else via Unix command line set it up for a global install as below (substituting for whatever version is most current for you), and then re-run the box add command from before:

wget https://dl.bintray.com/mitchellh/vagrant/vagrant_1.6.3_x86_64.deb
sudo dpkg -i vagrant_1.6.3_x86_64.deb

Continue by cloning the official Homestead repository into your folder of choice:

git clone https://github.com/laravel/homestead.git Homestead

Now, in the Homestead.yaml file, you need to set your SSH key info. Chances are, if you've ever used SSH everything is already good to go (it comes with some sane defaults), but if not, check out the documentation on how to do that. Folder mapping is assigned to nginx here as well. If you already have a Laravel project you wish to use, you can just tweak the default folders map, as cloned from the repo, but since I am starting from scratch, I had to do this:

folders:
    - map: ../Homestead
      to: /home/vagrant/Homestead
sites:
    - map: homestead.app
      to: /home/vagrant/Homestead/laravelAuth/public

I'll be making a new Laravel authentication app once this is all set up, so I'm gonna call my project laravelAuth, and preemptively set up the public sites map for it, as done above. Note that you can map several different apps in this development environment.

From here, it is simply vagrant up, and in a short while, you'll have a working Laravel dev environment! In the Homestead.yaml file, our box's ip address is set as 192.168.10.10 by default, so opening http://192.168.10.10/ in your browser should yield... an error? "No input file specified." ...what?

That is because we haven't actually created our Laravel project, so there is nothing for this development environment to show yet! This is easily remedied by entering your box via vagrant ssh, and running:

cd /vagrant
composer create-project laravel/laravel laravelAuth --prefer-dist

Reload your page, and now you have arrived at a brand spanking new Laravel home page. Code away!

June 9, 2014

PHP Composer: Adding new required package to the lock file

Composer's lock file is a great way to keep your project's packages locked to a specific tested/working version. If it wasn't there, your project's dependencies would receive the latest package versions every time you spool up your dev environment, or whatever you are currently deploying your project to. Therefore, the command composer install is your friend, and will only install the versions specified in the lock file - which, I would argue, is a good thing to version control if you are working in a team to ensure everyone is using the same packages and dependencies. composer update is a nasty command to run on a regular basis - it bypasses your lock, updates the lock file, and specifically updates all packages in the project to their latest version. This would likely require a round of testing to make sure nothing broke.

So how do you add a new package to the lock file without running a project-wide composer update - i.e. without updating the other packages? From the command line:

composer require "phpoffice/phpexcel":"1.8.*"
composer update --lock

This adds the new package require to the composer.json. Running the update --lock command after that will literally only add the new package info to the composer.lock file.