Architecture Flashcards
Every relevant information for the "Architecture" topic of the certification for laravel (41 cards)
What does a facade do?
They provide a “static” interface to classes that are available in the application’s service container.
What are helper functions?
Global functions, that make interacting with some Laravel features easier.
Describe “scope creep” in the context of facades!
Classes can easily grow too large, due to the ease of implementing more facades. The scope of responsibility should be split across multiple classes and not just fall onto one.
Which testing capabilities do facades offer, that normal static functions don’t?
You can inject a mock or stub and assert that various methods were called on the stub.
List the practical differences between facades and helper functions!
There are none.
Where is the facade base class located, which all facades extend?
Illuminate\Support\Facades\Facade
Describe real-time facades and what they let you do!
Using real-time facades, you may treat any class in your application as if it was a facade, which is very useful for testing / debugging purposes.
Given the class App\Contracts\Publisher, import a real-time facade of it!
use Facades\App\Contracts\Publisher;
Create a route for /hello, which displays “Hello World”!
Route::get('/hello', function () { return 'Hello World'; });
Where are routes defined by default? (In which file)
routes/web.php
What is Laravel Sanctum used for?
To turn your project into a stateless API.
Install Laravel Sanctum using artisan!
php artisan install:api
What is routing in Laravel?
It’s the process of defining URL patterns and associating them with certain actions or controllers in the application.
What are route parameters in Laravel?
Route parameters are dynamic values that are part of the URL pattern. They allow you to capture segments of the URI. For example, in Route::get('/user/{id}', function ($id) { return 'User '.$id; });
, {id} is a route parameter.
How do you define a route with a named parameter and set a default value in Laravel?
You can define a route with a named parameter and set a default value using the ? symbol and assigning a default value in the route definition. For example, Route::get('/user/{name?}', function ($name = 'Guest') { return 'User '.$name; });
sets a default value of ‘Guest’ if the name parameter is not provided.
What is a route middleware in Laravel?
Route middleware in Laravel is a way to filter and examine HTTP requests entering your application. Middleware can be assigned to individual routes or groups of routes, and can perform tasks like authentication, logging, or modifying the request or response.
How can you group routes in Laravel?
Routes can be grouped in Laravel using the Route::group method. This method allows you to apply shared attributes, such as middleware, namespaces, or prefixes, to multiple routes. For example:
Route::group(['prefix' => 'admin', 'middleware' => 'auth'], function () { Route::get('/dashboard', function () { ... }); Route::get('/profile', function () { ... }); });
Given the User $user, create a Route that returns the users email-address (variable name is “email”) when the user tries to access the URI “/users/{user}”!
Route::get('/users/{user}', function (User $user) { return $user->email; });
What happens to a model, if it gets soft deleted in Eloquent?
Instead of actually deleting it, a “deleted_at” attribute is set on the model indicating the date and time at which the model was “deleted”.
Which method is used to retrieve data from soft deleted models?
withTrashed();
If you don’t want your page to return the default 404 page, but instead want to display a custom page in the case of an invalid URL being used, how can you define a route for that?
At the very end (!) of routes/web.php:
Route::fallback(function () { // ... });
Given an API “api” and the fact, that the Request $request includes users with an ID and the IP address of the api user, rate limit the users access to the API to 60 requests per minute!
RateLimiter::for('api', function (Request $request) { return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip()); });
Where are rate limiters defined?
In the boot() function of App\Providers\AppServiceProvider
Rate limit the requests (“uploads”) for authenticated users to 100 per minute, while guests only get 10 requests per IP!
RateLimiter::for('uploads', function (Request $request) { return $request->user() ? Limit::perMinute(100)->by($request->user()->id) : Limit::perMinute(10)->by($request->ip()); });