Closures in Routes3:38 with Jonathan Barrios
Closures are anonymous functions that don’t belong to any class or object. Closures don’t have specified names and can also access variables outside of scope without using any global variables. Closures require less code and are simpler to write, which makes them handy tools when creating simple functions, like the About response we created in the last section. You'll use closures frequently when creating routes, so what is a Closure, exactly? PHP code can execute inside of Closures, much like functions do-- for example, you can work with variables and perform math operations within a closure that’s defined within a route. That’s a mouthful, but it’s simple-- routes can execute code using Closures, which are simply anonymous functions.
Welcome back. 0:00 Closures are anonymous functions that don't belong to any class or object. 0:01 Closures don't have specified names and 0:07 can also access variables outside of scope without using any global variables. 0:09 Closures require less code and 0:15 are simpler to write, which makes them handy tools when creating simple functions 0:18 like the about response we created in the last section. 0:23 You will use closures frequently when creating routes. 0:27 So what is a closure exactly? 0:30 PHP code can execute inside of closures, much like functions do. 0:33 For example, you can work with variables and 0:38 perform math operations within a closure that's defined within a route. 0:40 That's a mouthful, but it's simple, 0:47 routes can execute code using closures which are simply anonymous functions. 0:49 Routes can also trigger an action or some code, like calling a function or 0:55 performing a math operation. 1:00 Let's create three routes to try this out. 1:03 First, be sure to comment out the existing routes, then copy and 1:06 paste the first route to build some example routes. 1:10 Let's modify the first route which returns a string like this. 1:19 Let's modify the route to perform a math operation like this. 1:36 You'll frequently encounter a route that executes many operations, 1:49 like retrieving data from a database, adding that data to a template and 1:53 returning a personalized web page to the end user. 1:58 Let's go back to the last route and add a variable named number with a value of 50. 2:01 Multiply number by 10. 2:12 Save your changes and open the browser to see the product, which is 500. 2:19 To view your current routes, you can use Laravel's built-in CLI, 2:25 Artisan, like this. 2:30 Php artisan route: list. 2:38 We'll get into Artisan later, but until then, 2:47 you can use this command to view a list of routes at anytime. 2:50 Finally, let's delete these example routes and 2:54 be sure to uncomment our application route for /track community and support. 2:57 Closures are used frequently, and the best practice is to use them for 3:12 simple tasks like web routes. 3:16 For situations that require more complexity, we use controllers instead. 3:19 In the next video, we'll revisit Artisan CLI and the essential commands you'll need 3:25 to speed up development tasks like making controllers, database migrations and more. 3:30 See you there. 3:37
You need to sign up for Treehouse in order to download course files.Sign up