Getting Started with Laravel Framework

0
1695
Why-Laravel

Why-Laravel

Today we’ll be taking a look at a very popular PHP framework, Laravel. In particular, we’ll be looking at Laravel 5, which is the latest release of the framework. We’re going to go through installing it, and some basic setup. This will likely be a series on building a full app in Laravel, so this is a great place to start and get acquainted with the codebase to prepare for that.  Before we jump into the setup, let’s take a look at why we might want to use a framework.

Why Laravel?
You might be wondering to yourself why even bother learning a framework and how does this benefit you?

  1. Code Organization

Laravel is an MVC framework, that’s model-view-controller. This may sound fancy, but it’s just a way of developing applications and separating out which files code goes in. Without getting too much into what that means, the important thing to know is that Laravel uses this methodology, which in turns helps in writing cleaner and more readable code. PHP has a terrible reputation of being horrible code, but Laravel helps fight this.

  1. Common Tools Built In

Laravel, along with most other modern frameworks, comes with a bunch of built in tools that help streamline common tasks. Things like authentication, routing, and caching are all baked into the framework, and will save hours on rolling a custom solution. All these things really add up, and save a bunch of time in the long run.

  1. Great Community

Laravel has behind it a fantastic group of supporters. If there is ever an issue or an error, it’s highly likely that a quick google search or a glance over the documentation will be more than enough to solve it. Speaking of documentation, Laravel’s is top notch. They have a lot of great examples and everything is written really clearly, more than I can say for a lot of other code bases out there.

In the end, it really boils down to speed and ease of use. Using a tested framework like Laravel can seriously improve your productivity. Being able to write code faster means getting those cool apps done quickly, and everyone loves that! Now what we know some of the benefits of using Laravel, let’s install it.

Composer
Laravel works with a package manager called Composer. Composer is a convenient tool for managing your dependencies. If you’ve ever worked on a project that uses multiple libraries or external code bases, you’ll know some of the pains of managing them all. Composer provides some easy to use commands that allow you to check for updates and install updates onto your own code base.

Head on over to https://getcomposer.org and follow the download instructions. It’s a pretty straightforward process, and is equally easy for both mac and windows.

It’s also perfectly fine to use composer on its own, even without Laravel. It’s a great tool to use, so even if you don’t use Laravel in the future I still highly recommend looking into how it works in more depth.

Installing Laravel
Now that we have composer installed, let’s download and configure Laravel. First, I’m going to navigate to my desktop, and build the file there. You can choose any location that works best for you. Take note that the following list of build commands are run from the command line. By default you’ll likely either be using terminal on a mac, or cmd/powershell on a windows machine.

Next, we need to download the laravel installer using composer

Now, we will be able to run the “laravel new” command to create a fresh install of Laravel. The new command takes an additional parameter, which will be the directories name. For example.

This command will generate new Laravel installation in a directory named “listProject”. Once you run this command, Laravel will create a fresh installation and install all of its dependencies. If you open it up through a local server, you should see the Laravel welcome page! You’ll likely have to navigate to the public folder in your browser, this where your index.php file is located that takes care of calling the framework.

Learn PHP Fundamentals From Scratch

Basic Routing
To start let’s look at some basic routing. Inside of your installation folder navigate to the following file:

app/Http/routes.php
This is the core file that controls all the routing of your application. There’s only a little bit of code right now in this file. The main part we’re interested in:

This is actually the code that displays your welcome screen. Go ahead and try and reload the page, you should get an error.

At a basic level, this is saying that on the default route (/ as in nothing after the index) run a function. This function has only one operation to return a view called welcome. Go ahead and navigate to the view folder:

This is the view file that’s being served from the above function call. Don’t worry about the “.blade” for now, will get into that later. This would work exactly the same had it been named “welcome.php”, the blade isn’t required. If you open this file, you’ll find it’s all the HTML and CSS for the welcome screen. Let’s try and make our own route.
In the same “views” folder, create a file named home.php. Add some code to it, I put in the following.

Great, now let’s go back to the routes.php file and add in below the existing route:

That’s all there is to it. Go ahead and navigate to the newly created route:

And you’ll see your hard work! Now, this is a pretty trivial example, but there’s much more you can do by applying middleware and binding controllers to your routes. That’s all a little more advanced, so will leave it at this for now.

There you have it, you’ve created a brand new installation and learned about the V in MVC. The V, view, is what is displayed on the screen, and what we place in our views folder. This may seem a little cumbersome right now, but believe me; once you start building more complex applications, you’ll be happy you took the time to do it right. We’ll be looking at the M and C in another article, and from there we’ll start building out our first app. Stay tuned!

LEAVE A REPLY

Please enter your comment!
Please enter your name here