Introduction to Web Routes5:20 with Jonathan Barrios
In this video, we’re going to explore web routes and how to create dynamic navigation using URIs and Closures. If you're unfamiliar with the differences between the URI, URL, and URN, check the teacher’s notes below to learn more. A route maps all user requests to its appropriate controller, much like an air traffic controller would route airplane traffic. Imagine a user requests a home page from a Laravel app where each user request is routed according to a URI (or Uniform Resource Identifier) like “/” for the homepage or “/about” for an about page.
[MUSIC] 0:00 Welcome back, in this section, we're going to explore web routes, 0:04 and how to create dynamic navigation using URIs and closures. 0:10 If you're unfamiliar with the differences between the URI, URL, and 0:14 URN, check the teacher's notes below to learn more. 0:19 A route maps all user requests to their appropriate controllers, 0:24 much like an air traffic controller would route airplane traffic. 0:29 Imagine a user requests a home page from a Laravel app where each user 0:33 request is routed according to a URI, or uniform resource identifier. 0:39 Like / for the homepage, or /about for an about page. 0:45 Remember, not all URIs are URLs, because a URI could be a name instead of a locator. 0:51 If you're still not sure about the differences between a URI and a URL, 0:59 check the teacher's notes below. 1:04 First, let's open the terminal, navigate to the project directory, and 1:06 start our new Laravel application using the artisan serve command like this. 1:11 php artisan serve 1:16 --host=localhost. 1:20 Once you enter the command, you can either navigate to localhost in your browser, or 1:26 click on the link generated by Artisan. 1:31 If the default port is not 8000, 1:33 then check out the teacher's notes on instructions on how to find or 1:36 change the default port number in mamp or xampp. 1:42 Once you get your application running, the Laravel welcome screen or 1:46 splash screen should appear in your browser. 1:50 Let's take a closer look at the Laravel route files in the routes folder. 1:52 You'll notice a few files inside of our routes folder. 1:57 But the two most important files are the api.php and web.php files. 2:00 The api.php file is excellent for building an API or 2:07 application programming interface. 2:11 Which is a way to serve data and information outside of a web page. 2:14 You'll learn more about APIs and how to build one in a later course. 2:19 The web.php file is used to build web applications, and 2:24 it's the file we'll use in this course. 2:27 Open the web.php file, and let's take a look at the code inside. 2:30 Here you can notice it lists one route, /, 2:35 which routes to the current root directory. 2:38 The root directory is simply the project folder which contains all of 2:42 the application files. 2:46 In Laravel, 2:49 routes get defined in the route files located in the routes directory. 2:50 In the routes directory, you'll find a file named web.php that 2:56 automatically loads and defines your application's routes. 3:00 Routes can also get assigned to what's called middleware, 3:05 which provides security features like authentication and 3:10 protection checks for CSRF, or cross-site request forgery. 3:15 It's called middleware because it executes before the route is processed, 3:20 making middleware a fitting name. 3:24 If you're not familiar with the terms CSRF or authentication, 3:28 check the teacher's notes below to learn more. 3:32 Imagine a user requests a webpage which has a route that is attached to some 3:36 middleware, like an authentication check. 3:41 Before it can continue to its destination, the middleware checks a user's 3:44 authentication status when presenting specific content to the user. 3:49 If the user is not authenticated, the middleware will not 3:54 allow the user request to continue to the authenticated page. 3:57 Remember, in MVC, the controller sends instructions to change or 4:02 replace the contents of the view with data from the model. 4:07 By following this MVC pattern, 4:12 Laravel can efficiently direct traffic throughout an application. 4:14 Let's explore how web routes work in more detail by building a route for 4:19 each page of our app. 4:24 Head back to the project directory, copy the existing route, and 4:27 paste it below so you have a total of four routes. 4:31 The first route will be the index by default for the library page. 4:35 Next, let's add three more routes, tracks, 4:39 Community, And support. 4:45 For now, each route is pointing to the default welcome view displaying 4:51 the Laravel splash screen. 4:55 Web routing may seem simple, but 4:58 it's vital to the core of any Laravel application. 5:00 If there's no route defined in the app, then there's no way to access the route or 5:04 its destination, it's as simple as that. 5:09 In the next step, we'll dive into closures and 5:12 how we can use routes with views and controllers, see you there. 5:15
You need to sign up for Treehouse in order to download course files.Sign up