Really Radical Routing

Since a route is the first thing every user is going to interact with when visiting your site, it's where we're going to start our Laravel journey.

In most projects, your routes will be contained within the routes\web.php file. When you open it up after creating a brand new project (read the first lesson in this series if you need help setting up a Laravel project) you will see the generic starting route:

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

Let's break this down to figure out what's going on.

Route::get('/', function () {

First we call the get function on the Route facade. As you may have guessed, this is setting up a route that will respond to GET requests. The first parameter in this function is the actual route itself. In this case the route is set up for the root url. Assuming that our site is being run locally, this route is going to handle any GET requests that are made to localhost:8000. Any other type of request (such as POST) or any request to a different path (like localhost:8000/example) will ignore this route and keep looking for a better match.

The second parameter, the function, is what gets called whenever somebody makes a request to the server that matches this route. In upcoming lessons I'll show you how to extract this function to other files, but for right now we'll stick with this. For small projects or simple routes, using a function directly in your routes file is a viable option.

return view('welcome');

Inside the function we are simply returning this thing called a view. The view function is a special Laravel helper that takes a string which is then translated into the name of a file that it returns. Views will be covered in the next lesson, so make sure to check back in to learn exactly what this is doing. For right now, just rest assured that this is returning a view called "welcome". When you create a new project and visit it for the first time at localhost:8000, this view is what displays the large "Laravel" text and several links you see.

All in all, this route is saying that whenever somebody tries to GET the / route they should be sent back the "welcome" view.

Lets create our own route. We won't worry about other files or views and just return a simple string.

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

Now visit localhost:8000/hello and you should see "Hello, World!" output in plain old text on your screen. Congratulations, you've created a fully-functioning route in Laravel!

Of course most routes you create from here on out won't be quite this simple. In the next lesson we'll go over creating views so that you can return fully valid HTML pages. After that lesson you'll be able to use Laravel to create an entire website, albeit with limited functionality.

Although this may have seemed like an overly simple lesson, that's only because Laravel makes it so easy. We'll definitely be revisiting routes in the future as there is a lot more power hidden here, but we need to cover some more of the basics before we can use those features to their full potential!