laravel

Getting Started With Laravel: A PHP Web Framework

222 VIEWS

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:

  1. Composer
  2. Installing Laravel
  3. Laravel Dev Environment: Valet
  4. 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:

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:

Lastly, open your bashrc file (zshrc if using Z shell) and add:

Laravel

Now that we have Composer all squared away, we can finally install Laravel running the following command in the terminal:

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:

Next, we want to ensure that we have PHP7.1 by running:

Now, use Composer to install 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.

Do you think you can beat this Sweet post?

If so, you may have what it takes to become a Sweetcode contributor... Learn More.

Michael is a junior software developer currently studying at The Learners Guild based in Oakland, CA. He is always interested in learning new technologies and widening his scope of knowledge.


Discussion

Click on a tab to select how you'd like to leave your comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Menu