Ch5-HTTP Controllers - 060915 Rev Ari
-
Upload
ari-pribadi -
Category
Documents
-
view
219 -
download
0
description
Transcript of Ch5-HTTP Controllers - 060915 Rev Ari
Chapter 5 HTTP Controllers
Outlines
• Introduction
• Basic Controllers
• Controller Middleware
• RESTful Resource Controllers
• Implicit Controllers
• Dependency Injection & Controllers
• Route Caching
Introduction
• Controller classes can group related HTTP request handling logic into a class
• Controllers are typically stored in the app/Http/Controllers directory
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
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
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
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
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
Actions Handled By Resource Controller
• The following are the actions handled by Resource Controller
Partial Resource Routes
• Specifies a subset of actions to handle on the route when declaring a resource route
Naming Resource Routes
• By default, all resource controller actions have a route name
• Overrides these names by passing a names array with the options:
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
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
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
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)
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
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
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
HTTP Controllers Recap
• Understanding?
• Implementation?