February 20, 2015

The Laravel 5 Vagrant box: Homestead 2.0

It is possible that if you followed along with the last post, you might get some errors in starting up a Laravel 5 project. The error might depend on several factors such as OS or previous package dependencies, and manual intervention is probably required. This is why a virtual machine is a great way to manage development environments, particularly via Vagrant.

Homestead is Laravel's officially pre-packaged Vagrant box that is already set up with everything needed to run a Laravel project.

Virtual Machine setup


Install Vagrant
Install Virtualbox
Add the Homestead Virtualbox:
vagrant box add laravel/homestead

Homestead source files setup


Now, from here there are two ways of getting a box running. There is no need to do both.

I prefer cloning the Github repository, which is also the officially documented way at the time of this writing for Laravel 5.0. My reason is that if you "dirty" your Homestead repository, it really doesn't take a lot of effort to just remove the dirty/broken/outdated Homestead VM, reclone it afresh, and reimport your settings file. We will just clone the Github repo:


git clone https://github.com/laravel/homestead.git Homestead
cd Homestead
bash init.sh

This will create a Homestead.yaml file, placed in your ~/.homestead directory. This settings file can easily be re-initialized by running the init.sh script again, as above.

The alternate way is using Composer to globally install Homestead. This was previously the officially documented approach, at least back during Laravel 4.2. I am not sure I like this approach, but there are articles out there evangelizing the merits of doing this. I would rather modularize my development environments as done the first way, instead of globally invoking Homestead anywhere in my system (how often do you really need to create a new Laravel box, when a single box can capably manage all of your local Laravel projects? Capriciously, running homestead up is annoying when vagrant up is already so ingrained!):


composer global require "laravel/homestead=~2.0"
homestead init

This will create a Homestead.yaml file, placed in your ~/.homestead directory.

SSH key setup


The ~/.homestead/Homestead.yaml file already sets a sane default location for your SSH key; if yours is different then update it. See here to create a key from scratch if it doesn't yet exist.

Shared folders/sites setup


In the ~/.homestead/Homestead.yaml file, map out the path you want access to in your Homestead VM. I keep all my projects and VMs (including this Homestead VM) in a Projects folder, so let's update the `folders` variable using relative paths, and also the sites (of which you can have as many as you want) with the specific Laravel projects you want to access:


folders:
    - map: ../../Projects
      to: /home/vagrant/Projects

sites:
    - map: mylaravel5project.dev
      to: /home/vagrant/Projects/mylaravel5project/public

Hosts file setup


If you want to access your project in the browser with a pretty name instead of Homestead's default IP, http://192.168.10.10/, be sure to define some hosts for easier access.

Alias setup


To add Bash aliases to your Homestead box, simply add to the aliases file in the root of the ~/.homestead directory.

Create a Laravel Project


composer create-project laravel/laravel projectname

Launch!


Simply run:
vagrant up
To destroy the VM and start fresh, run:
vagrant destroy --force
To SSH into Homestead when it is running, from the Homestead cloned directory, run:
vagrant ssh

Now we are set to visit http://192.168.10.10/ and will see the first mapped Laravel project's main page. If several apps were specified in the Homestead.yaml file's sites, and hosts file definitions were set for all of them, you can access each of them individually via those mapped URLs (i.e. mylaravel5project.dev). Enjoy!

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

January 11, 2015

Windows Network Backup does not start anymore - blame Samsung Magician

Recently I upgraded to a new Samsung SSD. Love it. The speed boost over even a last-gen SSD is amazing. But that's not what this post is about. Since I upgraded my primary drive, it was also time for a clean Windows install. I guess some people would find this tedious, but I actually enjoy setting things up, starting from a nice clean slate. The frustration, however, was that stuff that previously just worked, now didn't.

It took a lot of headscratching trying to figure out why I was unable to set up automated backups again, using Windows backup, as outlined in a much earlier post of mine. Well, turns out the old post is still accurate and up-to-date, and that the new factor is now Samsung Magician to deal with.

It is otherwise a fantastic piece of software, one that further customizes your system for improved speed or reliability. It serves as a good reminder to enable AHCI on your motherboard if you've never used it, checks that you're using SATA6, etc. You can also run benchmarks to make sure you're squeezing the performance out of your drive. But again, that's not what this post is about.


Sure enough, after having done a prior optimize, Windows backup was turned off via Samsung Magician. But not really, because I can still access "Backup and Restore" from the Control Panel, and even set up my backup location and credentials; the damn thing just won't work.


Turns out it deactivates the Windows service, but not the GUI. Hit up your Run prompt (CMD+R) and enter services.msc:


Find "Windows Backup" in the list of services, and sure enough, it will be disabled:


Now, you can either choose to leave the Startup Type as disabled, and just start the service manually this time, but that makes no sense for automated backups, as your service already needs to be on when the backup is initiated. Set the Startup Type to automatic, start the service, and save the settings:


You should once again be able to run a scheduled Windows Backup as usual!

December 7, 2014

Opening files in Notepad++ from Cygwin

Notepad++ is an improved text editor instead of standard Vim in the command line, and especially improves upon using classic Notepad. It is superior, even if only for the syntax highlighting making things more readable. This is an alias function that seeks to implement a GUI-based text editor into Cygwin shell, and to run it as a daemon in the background, instead of seizing control over the terminal window for as long as the editor is open.

np ()
{
/cygdrive/c/Program\ Files/Notepad++/notepad++.exe `cygpath.exe -w "$*"` &
}

Note that it needs to be a function, and not a vanilla alias, because aliases don't allow interpolated arguments. What this function does is open the executable Notepad++ file, then takes a path argument that has been converted from a Cygwin path to a Windows path. $* stores the aformentioned arguments that were entered on the command line, and the & lets the process run in the background. This lets you use Notepad++ from Cygwin without having the shell wait for an exit code. See here for bash scripting info.

Reload the shell:
. ~/.bashrc

Now we can open any text file by passing the filename as an argument along with our new alias function. Instead of using vim or another editor we can simply use Notepad++ like so:
np ~/mycooltextfile.txt

You can even create text files this way - by specifying a filename that doesn't exist yet, it will be created for you!

December 5, 2014

Cygwin shell setup and bash-style tweaks

We have already seen how Cygwin can bring some great quality-of-life improvements as an alternative to using the native Windows terminal. Cygwin closely emulates a Unix shell, but there are still some differences.

The Home directory for Cygwin shell is probably not where you might think it should be. It is actually located within the Cygwin install directory (i.e. C:\Program Files\cygwin\home\User):

cd ~
pwd
/home/User

Since this isn't technically your Windows user's home directory, it will be empty, aside from config files that are used to emulate the Unix environment. As an example, you would navigate to C:\Users\User\My Documents like below (note how spaces need to be escaped by using backslashes):
cd /cygdrive/c/Users/User/My\ Documents/

Packages and programs are also not installed via command line. Instead, as explained in an earlier post, you would need to re-run the Cygwin setup.exe, which doesn't actually reinstall Cygwin from scratch, but just rebuilds it from existing packages, as well as any new ones you add during this subsequent install.

Aliases are a neat way to improve your workflow, and setting some up in Cygwin is really no different than Unix. Let's add some aliases (all config files are in the Cygwin home directory):
vi ~/.bashrc

I usually uncomment the ll, ls, la aliases, and add a few, like this function to use Notepad++ as a GUI text editor instead of using Vim in the terminal. Vim isn't great in Cygwin (feels more buggy than its Unix counterparts), so a solid GUI alternative before resorting to vanilla Notepad, is the nicer Notepad++. Anyways, you can also set up simple navigational aliases if symbolic links aren't your thing:
alias pro='cd /cygdrive/d/Projects'

You can also turn off the annoying bell beep sound the shell makes when you enter something incorrect, or try to autocomplete something that doesn't exist. In your ~/.inputrc file, uncomment or add:
set bell-style none

Reload the shell, no need to close and reopen to pull in the changes:
// These two commands are identical:
source ~/.bashrc
. ~/.bashrc

And now you can enjoy your newly added shortcuts!