Introduction To Laravel 5

Introduction To Laravel 5

Laravel 5 is now officially available, and this Introduction To Laravel 5 will get us started with it. So Why Laravel? Well, technology moves pretty fast and its tough to keep up. Not only is it tough to keep up, but it is increasingly complex. Laravel strives to make moving forward with Web Development simple, and who doesn’t want simplicity? It is this unique combination of staying on the cutting edge of web based development with modern PHP, while also keeping things simple, that makes Laravel so attractive. It is with great excitement that we dive into a new Laravel series here, and learn all about Laravel and what it can offer us today.

Introduction To Laravel 5

New Folder Structure

Laravel 5 brings with it many new features, and the first one we find is the new and improved folder structure. When you first install a fresh copy of Laravel 5, you’ll notice all the new folders like app, Commands, Console, Events, Exceptions, Handlers, Http, Providers, and so on. It looks like a lot to wrap your head around, but its not too bad as you use it. In fact, it really becomes second nature, and quite comforting once you are familiar with it.

Where Is The Routes File In Laravel 5?

Ah yes, the infamous routes file. Once upon a time, in a reddit thread far, far away, the routes file almost became a deprecated feature of the Laravel framework. There is reason to rejoice however, as this calamity did not befall upon the great Laravel. We still happily have our routes.php file and it is located in \app\Http\routes.php. This is great, since it is a fantastic starting point to look at the overall application. Here is what comes in the routes file by default.

\app\Http\routes.php source

Depending on your level of experience working with Laravel, these existing routes may or may not look familiar to you. It is the first route which actually loads the splash page of the base install of Laravel. So how does this work? Without getting too far into the magic under the hood, it is safe to read it like so. When a web request is made of /, or the homepage, fire the index method of the WelcomeController. Let’s have a look at that now.

App\Http\Controllers\WelcomeController.php source

Likewise to the routes file in the prior section, this is the default controller included with Laravel when you first install it. Ok, so we are in the WelcomeController, and there is that index method we were talking about. So what does it do? It looks like it simply calls a function named view, and passes in the string of welcome. Pretty Slick! Now, this is a bit of a change from prior versions of Laravel when you were required to use something like View::make() to render a view. In any event, think of this like saying, display a webpage named welcome. So what is the string welcome referring to? Let’s find out here.

\resources\views\welcome.blade.php source

Now this is interesting. Note that this particular file is the first one that is not part of the \app directory. Why is that? Is it that views are some kind of second class citizen? Of course not! Views are important in that they are what hold the data to display to our users. Views are more like basic containers however used to only hold and display, and as such, they don’t really fit in with the logic of our app. They are an important resource however, so alas, they are contained within the resources folder. So how does this view work? Well, note that all we had to do was pass welcome to the view() function, and Laravel knows to map that to welcome.blade.php in the resources folder. You see how the convention works I’m sure. Now you probably see the html and css and find it all quite easy to decipher. What about the part where it comes to that {{ Inspiring::quote() }} ? You can consider that your first use of blade. The double curly braces tell blade, that whatever comes in between us, evaluate the code and provide the output. Cool! So it looks like this is somehow referencing an Inspiring class and a quote method. Let’s see how this works as well.

\vendor\laravel\framework\Illuminate\Foundation\Inspiring.php source

Excellent! Not only does Laravel provide fantastic features and power, it can also provide you with all the Inspiration you’ll ever need. If you don’t like the inspiration it provides, you can even hack together your own inspiration. It might look something like this.

There may be different inspirations for different people, so it’s fantastic that we can hack our inspiration! When we test it out live on our homestead server, we can see the fruits of our hacking right away.

Laravel 5 Introduction Summary

In this episode we took a quick look at the new release of Laravel 5. We covered where the famous and loved routes file now lives, how it translates to a Welcome Controller, which then rendered a view that we see upon install. In addition, we examined the powerful new Inspiration class, and how to hack it to provide even more customized inspiring quotes.