Controllers Flashcards
Where are controllers stored? (by default)
app/Http/Controllers
Create a controller using Artisan!
php artisan make:controller NAME
Given a controller named “UserController” and the route “/user/{id}”, create a Route::get that uses the show method of said controller!
Route::get('/user/{id}', [UserController::class, 'show']);
What should the method name be if you have a controller dedicated to a singular action?
(in class of said controller)public function \_\_invoke() { // ... }
Given the route “/server” and the single function controller “ProvisionServer”, make a Route::post that invokes the function!
Route::post('/server', ProvisionServer::class);
Assign the middleware “auth” to this route:
Route::get(‘/profile’, [UserController::class, ‘show’]);
Route::get('/profile', [UserController::class, 'show'])->middleware('auth');
Which interface should you implement to specify middleware within your controller class?
HasMiddleware
Create a controller using Artisan, that assigns the “create”, “read”, “update” and “delete” routes to said controller!
php artisan make:controller ExampleController --resource
Register a resource’s CRUD (create, read, update and delete) methods in a route!
Route::resource('example', ExampleController::class);
Which method should you call while defining a resource route to not let the page go to the default 404 page when a resource model is not found?
missing();
Include soft deleted models in this resource route:
Route::resource(‘photos’, PhotoController::class);
Route::resource('example', ExampleController::class)->withTrashed();
Make a controller using Artisan, that type-hints a model instance!
php artisan make:controller ExampleController --model=ExampleModel --resource
Which option do you need to add to your Artisan command for the generation of a form request class for a controllers storage and update methods!
--requests
How can you limit the actions of a resource route to a specific subset of actions?
INSERT ROUTE->only([ 'action one', 'action two', ... ]);
How can you exclude specific actions of a resource route?
INSERT ROUTE->except([ 'action one', ... ]);
Declare a resource route that will be consumed by APIs!
Route::apiResource('example', ExampleController::class);
Register multiple API resource controllers at once!
Route::apiResources([ 'example1' => Example1Controller::class, 'example2' => Example2Controller::class, ]);
Generate an API resource controller using Artisan!
php artisan make:controller ExampleController –api
What do you need to do to define a route to a nested resource?
What will the URIs look like?
Route::resource('examples.beispiele', ExampleController::class); /examples/{example}/beispiele/{beispiel}
Which method enables automatic scoping for a nested model binding?
scoped()
What is shallow nesting?
Often, it is not entirely necessary to have both the parent and the child IDs within a URI since the child ID is already a unique identifier. When using unique identifiers such as auto-incrementing primary keys to identify your models in URI segments, you may choose to use “shallow nesting”
Which method do you need to use to use shallow nesting?
INSERT ROUTE->shallow();
How can you name the actions of controllers?
INSERT ROUTE->names([ 'example' => 'examples.beispiele' ]);
Create a scoped nested resource!
What will the URIs look like?
Route:.resource('examples.beispiele', ExampleController::class)->scoped([ 'beispiel' => 'spezifischesbeispiel', ]); /examples/{example}/beispiele/{beispiel:spezifischesbeispiel}