back to all posts

Laravel 6 Quickstart

by jonathan barrios / October 12th, 2019

What is Laravel?

Laravel is a free and open-source PHP framework that simplifies the development process without sacrificing quality. Introduced in 2011, Laravel has become one of the most popular PHP frameworks using the MVC (Model View Controller) pattern. The MVC pattern allows users to make requests to the Controller which talks to the Model(database access) which in turn updates the View in your browser or device. To learn more about MVC, check out this awesome Treehouse MVC video.

The Laravel Framework offers:

  • Simple Routing
  • Baked-in Security Layer
  • Easy DB Migrations with Artisan
  • Blade Templating Engine
  • Authentication Automatically
  • Sessions / Laravel Mix / Sass
  • Files and Storage
  • Error Handling
  • Email & Cache Handling


👋 Hey there, my name is Jonathan and I'm a software development instructor at Treehouse. I'm super excited to share how you can start building apps with the most exciting MVC framework for PHP -- Laravel 6.

Learning Objectives

By the end of this tutorial, you will be able to:

  • Build a Development Environment
    • Install Composer
    • Install Laravel
    • Install Valet(optional for macOS)
    • Navigate the File Structure

  • Create a Laravel 6 application
    • Open an app in Visual Studio Code
    • Create your own Route
    • Return a String from the Route

Working Environment

We have a few methods for setting up a working environment locally:

  • MAMP (Mac or Windows)
  • XAMPP (Mac, Windows or Linux)
  • Valet for macOS only

MAMP/XAMPP for macOS/Windows/Linux

If you're working on a Mac or Windows environment, I recommend using MAMP and alternatively Mac, Windows, and Linux users can all use XAMPP. Both MAMP and XAMPP are very popular PHP development environments and installation is as easy as following the download instructions for your preferred OS. In the next section, we will cover installing both PHP and MySQL for macOS using Homebrew, if your running Windows or Linux, you can skip the following sections on Valet, Homestead, Homebrew, PHP and MySQL which are labeled (macOS only). Windows and Linux users will use the Artisan server included with Laravel instead.


Valet vs Homestead? (macOS only)

Laravel offers Homestead, an alternative local development environment for Laravel. Homestead offers an entire Ubuntu virtual machine with an automated Nginx configuration. Homestead is a wonderful choice if you want a fully virtualized Linux development environment or are on Windows / Linux.

Valet only supports Mac and requires you to install PHP and MySQL directly onto your local machine. This is easily done with Homebrew using commands like brew install php and brew install mysql which we will cover in the next section. Valet is a blazing fast 🚀 local development environment with minimal resource consumption, so it's great for minimalist macOS developers compared to developers who prefer a fully virtualized development environment.

Both Valet and Homestead are great choices for configuring your Laravel development environment. In this tutorial, we will cover Valet and save Homestead for another tutorial.

Install/Update PHP (macOS only)

Open the Terminal or your preferred CLI and check if PHP is installed and if so, what version it is. We can use this one command: php -v which will either return some version information or nothing at all. If you don't get a version, you need to install PHP and if you're running an older version, go ahead and update it. I recommend Homebrew if you're on a Mac so click on the Homebrew link and follow the installation instructions or just update an existing install with brew update. Once you've installed or updated Homebrew, try running commands like: brew search php to search for installed packages or use: brew install php to install packages. If you want to upgrade your PHP version, you can type: brew upgrade php for the most current version.


Install/Update MySQL (macOS only)

You can install MySQL just like we installed PHP but first, let's search for MySQL like this: brew search mysql which will return a list of versions or you will see no installed versions, hinting that it's not installed. If you need to install MySQL, use the: brew install mysql command or if you need to update an existing version use: brew upgrade mysql to upgrade to the most current version, similarly as we did above to upgrde PHP. If you want to install a certain version, you can use: brew install [email protected] to install version 5.7, for example. That covers what we will need from MySQL to complete this tutorial.


Valet (macOS only)

In this section, we will install Valet now that we have a local version of PHP and MySQL installed. At this stage, we need to install Composer by visiting their website and copying the code in the Downloads section then running the code in the Terminal.

Next, install Valet Globally with this command:

composer globally require laravel/valet

In order to get Laravel integrated with our system, we need to add:

~/.composer/vendor/bin

to our system's PATH using the code editor of your choice. I've included three options below with my code editor of choice, Visual Studio Code at the top of the list:

Visual Studio Code:

sudo code /etc/paths

If Visual Studio Code doesn't launch from the CLI, check out my Visual Studio Code Quickstart tutorial for more on lauching the code command in PATH.

NANO:

sudo nano /etc/paths

VIM:

sudo vim /etc/paths

Add ~/.composer/vendor/bin to the next available line and save. If using Visual Studio Code, you may need to retry as sudo so follow any pop-ups that come from Visual Studio Code while saving these changes. We can verify the changes to our current paths with this command: echo $PATH and you should see ~/.composer/vendor/bin in your PATHS, mine is on line 6 and looks like this:

/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
~/.composer/vendor/bin


Install Composer

Visit the Composer website and copy the code in the Downloads section and run it.

Notice how the installer displays: Use it: php composer.phar as a hint for us to use our composer.phar file. Let's follow that prompt with this command: php composer.phar which returns:

   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 1.9.0 2019-08-02 20:55:32

Install Composer Globally

Great! Next, we want to install Composer globally so we can type composer from any directory on our machine. First, head to the Getting Started section and follow the instructions to install Composer globally for your OS, my code looks like this:

mv composer.phar /usr/local/bin/composer

This command will move the composer.phar file into the /usr/local/bin/composer url and call it /composer. Now we can run composer by typing it from any directory on our machine. Yay!


Install Laravel

Let's head over to the Laravel website, click on Getting Started followed by Installation then follow the instructions to install.


Build a Laravel App

Make sure that MAMP, XAMPP, Valet or some kind of native LAMP stack is running, then navigate to your preferred working directory. For example, my preferred working directory is in the Documents/laravel_apps folder. Once you are in your preferred working directory, type this command: composer create-project laravel/laravel appName in the Terminal and watch the resulting app creation fireworks for a couple of minutes. 🤔 Note that appName is an example, so be sure to replace appName with whatever you want to name your project. Next, cd into your newly created appName folder and type ls to list the contents of your new app. To run the server, type: php artisan serve then visit the URL given to view the website locally, like: http://127.0.0.1.

Alternatively, you can specify custom hosts and ports like this:

php artisan serve --host=localhost --port=9090

Once you enter the above command, enter http://localhost:9090 in your browser. Using an ip address or localhost is a matter of personal preference so use the one you prefer or whichever best suits your needs. Remember to run php artisan serve or the flag version php artisan serve --host=localhost --port=9090 each time after closing the Terminal. Otherwise, you will get an error saying localhost refused to connect or something to that effect.

🙌 If you want to try something better and you're using macOS, try Valet 6.x. If your a Windows/Linux user skip Valet.

Open Project in Visual Studio Code

Now that you created your own Laravel app and started a server with php artisan serve or the flag version php artisan serve --host=localhost --port=9090, open your app folder with Visual Studio Code. If you don't know how to open project folders in Visual Studio Code, check out the Visual Studio Code Quickstart tutorial. We're not going to talk about all of the files and folders just yet, but we can go over the basic file structure that you will use regularly.

Models, Views, and Controllers

Laravel is an MVC framework with the Models, Views, and Controllers stored inside of its file structure. The Models are located in app/User.php while the Controllers are located in the http/Controllers directory. You will see a BaseController named Controller.php as well. The views are located in the resources/views/welcome.blade.php directory. Laravel uses the blade templating engine which is essentially HTML with dynamic qualities such as variables for example.

Routes

Let's explore the routes by navigating to the /routes folder, where all of your app's routes will live. It's simple, if we GET a certain URL, load this file, and that's it. Very straight forward. Notice the / is the home directory that returns the welcome.php file.

Route::get('/', function () {
    return view('welcome');
});

To view the welcome.php file, navigate to the /resources folder then jump inside of the /views folder to see the welcome.blade.php file. Open the welcome.blade.php file with Visual Studio Code and look for the <div> that contains the title Laravel near the bottom of the file and change it to your name. Save the file and refresh the browser running the Laravel app. Cool! Next delete all of the <li> elements except for one and use any URL and link name you wish. Sweet, you built and customized a basic Laravel app! 🙌

Next, let's return a string instead of a view. You can imagine how useful it might be to execute code using Routes.

Route::get('/', function () {
    return 'Hello World';
});

Refresh the browser and you should see the text `Hello World`.

Congrats! 🎉

I hope you enjoyed Laravel 6 Quickstart. Follow me on Twitter @_jonathan_codes and if you want to learn more about PHP, check out Treehouse's PHP TechDegree.

back to all posts