Join Joseph Lowery for an in-depth discussion in this video Routing requests, part of Laravel 4 Essential Training.
- Routes in Laravel direct your application's traffic giving you complete control over the responses, or web destinations, if you will, for whatever requests are made. You can easily set up general requests to go, or route, to one location, your homepage for example, and more specific page requests, like to an About page to display that content. In this lesson, I'll show you the basics of setting up your Laravel routes as well as a few advanced features like passing parameters so you can perfectly map out your own corner of the information superhighway, as we used to say.
All of the Laravel routes are maintained in a central location, a file called, appropriately enough, routes.php. I'm gonna switch over to my code editor, and you can see on the left that my new app project that we installed with Composer, has all of these default files. These are all part of the Laravel framework. The routes.php file is found in the App folder. I'll expand that, and you see down at the end, routes.php.
Let's open it up and take a look. As you can see, there is a initial root defined. Let me go ahead and turn on my line numbers here. So on line 16, you can see that small bit of code responsible for displaying the default Laravel Welcome page with the logo and the message, "You have arrived." That one. This code returns what's called an html view of a basic php page.
We'll cover views later in the chapter. For now, let's start with a more basic text response to a specific page request. Let's say we want to lay the foundation for an About page. So, I'm gonna put my cursor after that initial route, hit return, just add a little bit of space, and then start by entering the keyword Route and then two colons and the method, which in this case is "get." Then we'll put in a set of parentheses.
Now there are two parameters within the get method. The first is the request term that we're looking for. That needs to go in single quotes. So I'm just gonna put in about, follow that with a comma and the start of a callback function. I wanna make sure to put a semicolon at the end there. Now within to the curly braces we add whatever processing code we need.
For now, I'm just gonna put return and then a bit of text in single quotes. I'll make this uppercase so we can see it easily. ABOUT content. Then finish that off with a semicolon. So I'll save the page with a command S. Let's go try it out in a browser. So I'm just gonna hit refresh here just to make sure nothing went wrong and that my default request is still working.
Okay, that looks good. Now let's add the About path. There, in the upper left, is our custom text. Let's add one more simple route to show you how you work with paths. Back at the routes.php file, I'm gonna just go ahead and copy my previous content there, paste in that route and just add a path within the About folder to some content called "directions." Let's change the ABOUT to DIRECTIONS.
All right, now that my return statement is customized a little bit, I'll go ahead and save that page and then try it out in a browser. Now we have our directions content. You can make the paths as complex as you need. In all of these examples that we have here, we're using the get method. The Laravel routing also supports other HTTP methods which are often called verbs in Laravel, including post, put and delete.
You can also cover all your bases through the Laravel supported wildcard verb, any. So let me create a route using "any." Again, parentheses, and the opening argument is within single quotes. I'm gonna call this submit-form. Put in my function. Let's add a little bit of text here in the return statement.
Let's say Process FORM. Okay, we'll put in our missing semicolons. Everything looks good. I'll save the file. Now let's go to submit-form. All right. Now that's just the start of what's possible with Laravel routing. We'll look at some more advanced uses in the next lesson.
Need a quick dive into Laravel? Check out this short primer, Up and Running with Laravel.
- Installing Laravel and Composer
- Routing requests
- Filtering routes
- Incorporating advanced controllers
- Creating a basic Blade template
- Developing a layout with child pages and forms
- Integrating a database
- Creating tables via migrations
- Outputting data
- Building a Laravel app
- Authenticating users
- Deploying Laravel code