Chapter 3. Routing and Controllers

The essential function of any web application framework is to take requests from a user and deliver responses, usually via HTTP(S). This means defining an application’s routes is the first and most important project to tackle when learning a web framework; without routes, you have little to no ability to interact with the end user.

In this chapter we will examine routes in Laravel; you’ll see how to define them, how to point them to the code they should execute, and how to use Laravel’s routing tools to handle a diverse array of routing needs.

A Quick Introduction to MVC, the HTTP Verbs, and REST

Most of what we’ll talk about in this chapter references how Model–View–Controller (MVC) applications are structured, and many of the examples we’ll be looking at use REST-ish route names and verbs, so let’s take a quick look at both.

What Is MVC?

In MVC, you have three primary concepts:


Represents an individual database table (or a record from that table)—think “Company” or “Dog.”


Represents the template that outputs your data to the end user—think “the login page template with this given set of HTML and CSS and JavaScript.”


Like a traffic cop, takes HTTP requests from the browser, gets the right data out of the database and other storage mechanisms, validates user input, and eventually sends a response back to the user.

In Figure 3-1, you can see that the end user will first interact with the controller by sending an ...

Get Laravel: Up & Running, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.