Skip to content
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 171 lines (125 sloc) 5.66 KB

Basic flow

Traditional web application flow:

  • Visitor requests http://foobooks.loc
  • Server responds by loading /foobooks/index.php


  • Visitor requests http://foobooks.loc/books/great-gatsby.php
  • Server responds by loading /foobooks/books/great-gatsby.php

Throw away this traditional “URL maps to specific file” thinking. Modern web frameworks use something called routing.

With routing, all requests load index.php which will parse the URL and load the appropriate content.

So our new flow looks like this:

  • Visitor requests http://foobooks.loc/books/great-gatsby
  • Due to a config in .htaccess (project specific Apache config file), server responds by loading /foobooks/public/index.php
  • index.php sets up everything the application needs to run, looks up the URL parameters (/books/great-gatsby) in a route map file, and executes the appropriate action to satisfy the request and return a response.

Request Lifecycle

Laravel request Lifecycle diagram

Read more: Laravel docs: Request Lifecycle

Example 1: Edit an existing route

Knowing this, let's trace how we're seeing this page on our fresh Laravel installs:

The URL in this example is http://localhost/, so the path is simply /.

Load /routes/web.php and you'll see a single route has been defined:


| Web Routes
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!

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

This route matches the URL pattern /, and it returns a view called welcome.

In Laravel, view files are stored in /resources/views so navigate to that directory.

You should see a file welcome.blade.php, and if you open it, you'll see the underlying HTML of that Laravel welcome screen.

<!doctype html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">


        <!-- Fonts -->
        <link href=",600" rel="stylesheet">

        <!-- Styles -->
            /*[redacted for brevity]*/
        <div class="flex-center position-ref full-height">
            @if (Route::has('login'))
                <div class="top-right links">
                        <a href="{{ url('/home') }}">Home</a>
                        <a href="{{ route('login') }}">Login</a>

                        @if (Route::has('register'))
                            <a href="{{ route('register') }}">Register</a>

            <div class="content">
                <div class="title m-b-md">

                <div class="links">
                    <a href="">Docs</a>
                    <a href="">Laracasts</a>
                    <a href="">News</a>
                    <a href="">Blog</a>
                    <a href="">Nova</a>
                    <a href="">Forge</a>
                    <a href="">GitHub</a>

Clear out this welcome.blade.php file and replace it with the following:

<!doctype html>
<html lang='en'>
    <meta charset='utf-8'>

Refresh the page and you should now see this:

You've just made your first customization to your new Laravel application!

Example 2: Add a new route

Update your web.php routes file so it has a new route matching the url pattern /example that will return a string rather than a view:

# New route
Route::get('/example', function () {
    return 'hello there!';

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

Save your work then test it out by visiting http://foobooks.loc/example; you should see this:

Example 3: Test a non-existent route

Observe what happens when you visit a route that has not be defined, for example, http://foobooks.loc/xyz.

Pending questions

  • How exactly does the Route method work?
  • Why do the view files end with a .blade.php extension?
  • A route can return a string, or a view... what else can it return?
  • How can we respond to different request types, e.g., POST instead of GET?

These questions will all be answered as we continue to dig deeper.

You can’t perform that action at this time.