Routing is really the first thing to look at when working with a Laravel application. When we say routing, what we mean is that when a hyper text transfer protocol request is made of a site, the router is what determines how to handle that request. In this simple tutorial we will look at how Laravel handles the incoming http request, and then in turn how it presents a webpage to a site visitor. Let’s see it in action now.
Begin by installing Composer
Before we can look at a new install of Laravel, we need a few tools to work with. The first is that of Composer, the wonderful package management tool that has revolutionized PHP development.
Next you install Laravel
With composer installed, you can now install Laravel. There are a few ways to do this, but we’ll go with the
create-project command built into composer from the command line interface api.
composer create-project –prefer-dist laravel/laravel 54
Simply typing that at your terminal then hitting enter will install the latest version of Laravel for you. What does each section of this command actually mean? Let’s break it down.
This command launches the Composer software and tells it your ready to do something. If you only type this command at the terminal, you will see something like this. It gives you a chance to view many of the available commands you can use.
This command is the equivalent of using git to clone a project, then running a composer install to download all dependencies in the project. So it’s a shortcut in essence, and it works really well.
First we must know what dist stands for. dist is short for distribution, and in open source software, this is usually the location that contains distribution or ready to use software. There is no compilation step or build process needed as in contrast to source. By using the –prefer-dist flag, we are telling composer that we want this ready to use software.
This portion of the command is denoting that we want the laravel vendor and the laravel package. It is in the format of
This tells composer that we want a folder named 54, and that we want to place the entire contents of this project in that folder. We could name this anything we like. We only chose 54 since we are using Laravel 5.4 in this example.
So now that we understand exactly what this command does for us, we run it and watch the magic happen. You might see something like this in your terminal.
When everything finishes, you will have all the files you need in your folder to begin a Laravel Project. We now navigate over to PHP Storm and choose File->Open to open the directory we just put all of our code in. So in our case, we are opening the 54 directory. Behold, your Laravel Installation.
There’s a lot going on here! Fear not, all we need to worry about right now is that routes folder. Since we are looking at basic routing and views in this article, that is what is highlighted in the image. If you open this folder up, you’ll see four files. These include api.php, channels.php, console.php, and web.php. It is the web.php file that we want to look at now.
Looks good! What we see here is a method which accepts two arguments. Don’t let the code formatting confuse you. Route::get( ); is the method being called, and the two arguments are inside the parenthesis separated by a comma. Think of it like Route::get(arg1, arg2 ); The first argument determines where an http request is being made to. We can see one forward slash here. This represents someone making a request to view your site at the root or domain level. The second argument is the action to take when this request is made. You can use a Closure like shown here, of a controller. We’ll stick to the simple Closure for now. This is an anonymous function which runs immediately when a request is made. What does this function do? It returns, or renders, the welcome view. If we visit the root level of our new project in a browser, we can see this working in action.
Create Your First Route
We are now equipped with enough knowledge to create our first route in Laravel. Let us have a goal of simply visiting the url of /hello, and load a view to be seen in the browser. Ok, let’s add a route to our routes file now.
You can see our new route in place. It simply says that if someone makes request to view http://mysite/hello, then the Closure will execute and it will load a view named hello. Try it in your browser now.
Whoops, looks like something went wrong.
We take this step of trying to visit our new route prematurely to show that Laravel has some great error handling built in. As you develop your own applications, you are going to be used to seeing that error: Whoops, looks like something went wrong. Nothing to fear however, as this is simply how you build things out. Build, create error, fix error, success. We see this error: InvalidArgumentException in FileViewFinder.php line 137: View [hello] not found.
Create your first view file
Of course! We tried to visit our new route which loads a view, but we never created the view file. Let us do that now. You will need to look in the resources folder as we show here.
In the resources folder, there are three additional folders named assets, lang, and views. We will create a file named hello.blade.php inside the views folder. Here it is, in all of it’s glory.
Let us now try visiting our /hello route once more in the browser.
We have created our first custom route and view in Laravel, and it is working like a champ!
Basic Laravel Routing and Views Summary
Let’s review what we accomplished in this basic routing and views in Laravel tutorial. First, we installed the framework, and got everything ready to rock. Then we had a look at our Laravel installation and made note of the various directories. We saw that it makes sense to begin by looking in the routes folder to start. We had a look at the default / route, then decided to make our own route that lives at /hello. We created a closure to handle this request and tried to load a hello view. We found out that if you try to visit a route in the browser which expects to load a view, if that view is not created yet, you will get an error. We then created our new view named hello.blade.php and tried again. Everything then worked. Also, note that Laravel is smart enough to load the correct view. Even though we specify return view(‘hello’); in the routes file, Laravel correctly loaded hello.blade.php in the browser.