Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 168 lines (122 sloc) 5.51 KB

Basic flow

Traditional web application flow:

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

Or...

  • 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:

<?php

/*
|--------------------------------------------------------------------------
| 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="{{ app()->getLocale() }}">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <title>Laravel</title>

        <!-- Fonts -->
        <link href="https://fonts.googleapis.com/css?family=Raleway:100,600" rel="stylesheet" type="text/css">

        <!-- Styles -->
        <style>
            [...styles redacted for brevity...]
        </style>
    </head>
    <body>
        <div class="flex-center position-ref full-height">
            @if (Route::has('login'))
                <div class="top-right links">
                    @auth
                        <a href="{{ url('/home') }}">Home</a>
                    @else
                        <a href="{{ route('login') }}">Login</a>
                        <a href="{{ route('register') }}">Register</a>
                    @endauth
                </div>
            @endif

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

                <div class="links">
                    <a href="https://laravel.com/docs">Documentation</a>
                    <a href="https://laracasts.com">Laracasts</a>
                    <a href="https://laravel-news.com">News</a>
                    <a href="https://forge.laravel.com">Forge</a>
                    <a href="https://github.com/laravel/laravel">GitHub</a>
                </div>
            </div>
        </div>
    </body>
</html>

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

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset='utf-8'>
</head>
<body>
    <h1>Foobooks</h1>
</body>
</html>

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.