Download - Ch5-HTTP Controllers - 060915 Rev Ari
![Page 1: Ch5-HTTP Controllers - 060915 Rev Ari](https://reader036.vdocuments.net/reader036/viewer/2022062407/563db8e5550346aa9a97f871/html5/thumbnails/1.jpg)
Chapter 5 HTTP Controllers
![Page 2: Ch5-HTTP Controllers - 060915 Rev Ari](https://reader036.vdocuments.net/reader036/viewer/2022062407/563db8e5550346aa9a97f871/html5/thumbnails/2.jpg)
Outlines
• Introduction
• Basic Controllers
• Controller Middleware
• RESTful Resource Controllers
• Implicit Controllers
• Dependency Injection & Controllers
• Route Caching
![Page 3: Ch5-HTTP Controllers - 060915 Rev Ari](https://reader036.vdocuments.net/reader036/viewer/2022062407/563db8e5550346aa9a97f871/html5/thumbnails/3.jpg)
Introduction
• Controller classes can group related HTTP request handling logic into a class
• Controllers are typically stored in the app/Http/Controllers directory
![Page 4: Ch5-HTTP Controllers - 060915 Rev Ari](https://reader036.vdocuments.net/reader036/viewer/2022062407/563db8e5550346aa9a97f871/html5/thumbnails/4.jpg)
Basic Controllers
• All controllers should extend the base controller class
• We can route to the controller action like so:
– When a request matches the specified route URI, the showProfile method will be executed
![Page 5: Ch5-HTTP Controllers - 060915 Rev Ari](https://reader036.vdocuments.net/reader036/viewer/2022062407/563db8e5550346aa9a97f871/html5/thumbnails/5.jpg)
Controllers & Namespaces
• We did not need to specify the full controller namespace when defining the controller route
• We only defined the portion of the class name that comes after the App\Http\Controllers namespace “root”
• Use the following code to register a route if full controller class is (kenapa ga pake kalimat aslinya)App\Http\Controllers\Photos\AdminController
![Page 6: Ch5-HTTP Controllers - 060915 Rev Ari](https://reader036.vdocuments.net/reader036/viewer/2022062407/563db8e5550346aa9a97f871/html5/thumbnails/6.jpg)
Naming Controller Routes
• Like Closure routes, you may specify names on controller routes
• Once you have assigned a name to the controller route, you can easily generate URLs to the action
• To generate a URL to a controller action, use the action helper method
![Page 7: Ch5-HTTP Controllers - 060915 Rev Ari](https://reader036.vdocuments.net/reader036/viewer/2022062407/563db8e5550346aa9a97f871/html5/thumbnails/7.jpg)
Controller Middleware
• Middleware may be assigned to the controller’s routes
• It is more convenient to specify middleware within your controller’s constructor
• You may even restrict the middleware to only certain methods on the controller class
![Page 8: Ch5-HTTP Controllers - 060915 Rev Ari](https://reader036.vdocuments.net/reader036/viewer/2022062407/563db8e5550346aa9a97f871/html5/thumbnails/8.jpg)
RESTful Resource Controllers
• Resource controllers make it painless to build RESTful controllers around resources
• Use the make:controller Artisan command to create a controller– The Artisan command will generate a controller file at
app/Http/Controllers/PhotoController.php
– The controller will contain a method for each of the available resource operations
• You may register a resourceful route to the controller– This single route declaration creates multiple routes to
handle a variety of RESTful actions on the photo resource
![Page 9: Ch5-HTTP Controllers - 060915 Rev Ari](https://reader036.vdocuments.net/reader036/viewer/2022062407/563db8e5550346aa9a97f871/html5/thumbnails/9.jpg)
Actions Handled By Resource Controller
• The following are the actions handled by Resource Controller
![Page 10: Ch5-HTTP Controllers - 060915 Rev Ari](https://reader036.vdocuments.net/reader036/viewer/2022062407/563db8e5550346aa9a97f871/html5/thumbnails/10.jpg)
Partial Resource Routes
• Specifies a subset of actions to handle on the route when declaring a resource route
![Page 11: Ch5-HTTP Controllers - 060915 Rev Ari](https://reader036.vdocuments.net/reader036/viewer/2022062407/563db8e5550346aa9a97f871/html5/thumbnails/11.jpg)
Naming Resource Routes
• By default, all resource controller actions have a route name
• Overrides these names by passing a names array with the options:
![Page 12: Ch5-HTTP Controllers - 060915 Rev Ari](https://reader036.vdocuments.net/reader036/viewer/2022062407/563db8e5550346aa9a97f871/html5/thumbnails/12.jpg)
Nested Resources
• To define routes as a “nested” resource use “dot” notation in your route declaration:
• This route may be accessed with the following URLs photos/{photos}/comments/{comments}
“dot” notation
![Page 13: Ch5-HTTP Controllers - 060915 Rev Ari](https://reader036.vdocuments.net/reader036/viewer/2022062407/563db8e5550346aa9a97f871/html5/thumbnails/13.jpg)
Supplementing Resource Controllers
• Define the routes before your call to Route::resource; to add additional routes to a resource controller beyond the default resource routes
![Page 14: Ch5-HTTP Controllers - 060915 Rev Ari](https://reader036.vdocuments.net/reader036/viewer/2022062407/563db8e5550346aa9a97f871/html5/thumbnails/14.jpg)
Implicit Controllers
• To define a single route to handle every action in a controller class– Define the route using the Route::controller method that
accepts two arguments• The base URI the controller handles• The class name of the controller
– Add methods to your controller• The method names should begin with the HTTP verb
![Page 15: Ch5-HTTP Controllers - 060915 Rev Ari](https://reader036.vdocuments.net/reader036/viewer/2022062407/563db8e5550346aa9a97f871/html5/thumbnails/15.jpg)
![Page 16: Ch5-HTTP Controllers - 060915 Rev Ari](https://reader036.vdocuments.net/reader036/viewer/2022062407/563db8e5550346aa9a97f871/html5/thumbnails/16.jpg)
Assigning Route Names
• Pass an array of names as the third argument to the controller method if you would like to name some of the routes on the controller (muter2 kalimatna)
![Page 17: Ch5-HTTP Controllers - 060915 Rev Ari](https://reader036.vdocuments.net/reader036/viewer/2022062407/563db8e5550346aa9a97f871/html5/thumbnails/17.jpg)
Dependency Injection & Controllers
• The Laravel service container is used to resolve all Laravel controllers– You are able to type-hint any dependencies your controller
may need in its constructor
– The dependencies will automatically be resolved and injected into the controller instance
![Page 18: Ch5-HTTP Controllers - 060915 Rev Ari](https://reader036.vdocuments.net/reader036/viewer/2022062407/563db8e5550346aa9a97f871/html5/thumbnails/18.jpg)
![Page 19: Ch5-HTTP Controllers - 060915 Rev Ari](https://reader036.vdocuments.net/reader036/viewer/2022062407/563db8e5550346aa9a97f871/html5/thumbnails/19.jpg)
Method Injection
• To constructor injection, you may also type-hint dependencies on your controller’s action methods– For example, let’s type-hint the Illuminate\Http\Request instance on one of our methods
• Lists your route arguments after dependencies if you want to use a route parameter
![Page 20: Ch5-HTTP Controllers - 060915 Rev Ari](https://reader036.vdocuments.net/reader036/viewer/2022062407/563db8e5550346aa9a97f871/html5/thumbnails/20.jpg)
![Page 21: Ch5-HTTP Controllers - 060915 Rev Ari](https://reader036.vdocuments.net/reader036/viewer/2022062407/563db8e5550346aa9a97f871/html5/thumbnails/21.jpg)
Route Caching
• Using the route cache will drastically decrease the amount of time (100x faster) it take to register all of your application’s routes– Your route registration may even be up to!
• Execute the route:cache to generate a route cache– The cached routes file will now be used instead of your app/Http/routes.php file
– If you add any new routes you will need to generate a fresh route cache
• To remove the cached routes file without generating a new cache, use the route:clear command
![Page 22: Ch5-HTTP Controllers - 060915 Rev Ari](https://reader036.vdocuments.net/reader036/viewer/2022062407/563db8e5550346aa9a97f871/html5/thumbnails/22.jpg)
HTTP Controllers Recap
• Understanding?
• Implementation?