Laravel is a free, open source PHP web framework that is relatively easy to get started with. It aids in the development of PHP web applications and was initially intended for use within the MVC model. It comes with a dense library of features, including utilities that aid in application deployment and maintenance, an easy-to-use interface that handles user authentication, built-in unit testing, and much more. For the purpose of this article, and assuming PHP has already been installed on your system, we will walk through the process of getting started with the Laravel framework on OSX.
This article covers the following:
- Composer
- Installing Laravel
- Laravel Dev Environment: Valet
- Database Setup
Composer/Laravel Installation
Composer is a basic dependency manager that Laravel relies on, so we must have Composer installed on our machines before being able to use Laravel. To get started, let’s run these lines of code in the terminal:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" php -r "if (hash_file('SHA384', 'composer-setup.php') === '669656bab3166a7aff8a7506b8cb2d1c292f042046c5a994c43155c0be6190fa0355160742ab2e1c88d40d5be660b410') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" php composer-setup.php php -r "unlink('composer-setup.php');"
These lines of code download the installer, and check if it is up-to-date. The code then runs and finally removes the installer.
Next, we want to make sure Composer can be accessed globally. Run the following to move composer.phar to a directory that is within your path:
mv composer.phar /usr/local/bin/composer
Lastly, open your bashrc file (zshrc if using Z shell) and add:
$HOME/.composer/vendor/bin:$PATH to your $PATH. This ensures that your laravel executable can be located by your system.
Laravel
Now that we have Composer all squared away, we can finally install Laravel running the following command in the terminal:
Composer global require “laravel/installer”
Once installed, we can use the laravel new blog command to create a fresh Laravel installation with the directory name blog and all of Laravel’s dependencies already installed. In turn, adding –dev to the end of the command will create a fresh development installation of Laravel.
Laravel Valet
Laravel Valet is a blazing-fast development environment built specifically for the Mac and uses very little RAM. We can think of Valet as a place to “park” all of your Laravel websites. Each new Laravel application created can be immediately served through Valet via the .dev TLD. This, however, is only available for Mac, and requires that PHP and a database server be installed directly onto your machine.
Valet does require Homebrew to run, so make sure that Homebrew is installed, and that it is running the latest version using:
brew install
Next, we want to ensure that we have PHP7.1 by running:
brew install homebrew/php/php71
Now, use Composer to install Valet:
composer global require laravel/valet
Finally, run: valet install.
Every time our machine boots from here on out, Valet will automatically launch.
Create a new directory anywhere on your Mac that you’d like, and run the command valet park, which is essentially “parking” all of your websites in that location. We can now access our websites at any time by using the .dev domain via our localhost. For example: if we run laravel new blog, we could access the website for that blog by typing blog.dev into our browser.
Database Setup
First, let’s ensure that our MariaDB database is running. (If you haven’t already, install and run MariaDB onto your system.) Once the database is running, we simply log into our database: mysql -uroot -p. Here we have a username as “root” and the password is blank. Next, create a database called blog: create database blog;.
Now that we have that aside, open the Laravel application you wish to connect to and go into the .env file. Here, we have a section that looks like this:
All we have to do here is change the information located within this file to match the information about our database. In our case, we will be changing DB_DATABASE, DB_USERNAME and DB_PASSWORD. We should end up with this section looking like so:
Next, run the command: php artisan migrate. This runs a quick schema template that is included within our Laravel application. Now we should be able to go back to our database and see the tables that were just created.
That’s it! This is all you need to do to get started in development within the Laravel framework.