Architecture Flashcards

Every relevant information for the "Architecture" topic of the certification for laravel

1
Q

What does a facade do?

A

They provide a “static” interface to classes that are available in the application’s service container.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are helper functions?

A

Global functions, that make interacting with some Laravel features easier.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Describe “scope creep” in the context of facades!

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Which testing capabilities do facades offer, that normal static functions don’t?

A

You can inject a mock or stub and assert that various methods were called on the stub.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

List the practical differences between facades and helper functions!

A

There are none.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Where is the facade base class located, which all facades extend?

A

Illuminate\Support\Facades\Facade

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Describe real-time facades and what they let you do!

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Given the class App\Contracts\Publisher, import a real-time facade of it!

A

use Facades\App\Contracts\Publisher;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Create a route for /hello, which displays “Hello World”!

A
Route::get('/hello', function () {
    return 'Hello World';
});
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Where are routes defined by default? (In which file)

A

routes/web.php

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is Laravel Sanctum used for?

A

To turn your project into a stateless API.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Install Laravel Sanctum using artisan!

A

php artisan install:api

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is routing in Laravel?

A

It’s the process of defining URL patterns and associating them with certain actions or controllers in the application.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are route parameters in Laravel?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you define a route with a named parameter and set a default value in Laravel?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is a route middleware in Laravel?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How can you group routes in Laravel?

A

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 () { ... });
});
18
Q

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}”!

A
Route::get('/users/{user}', function (User $user) {
    return $user->email;
});
19
Q

What happens to a model, if it gets soft deleted in Eloquent?

A

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”.

20
Q

Which method is used to retrieve data from soft deleted models?

A

withTrashed();

21
Q

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?

A

At the very end (!) of routes/web.php:

Route::fallback(function () {
    // ...
});
22
Q

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!

A
RateLimiter::for('api', function (Request $request) {
        return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip());
    });
23
Q

Where are rate limiters defined?

A

In the boot() function of App\Providers\AppServiceProvider

24
Q

Rate limit the requests (“uploads”) for authenticated users to 100 per minute, while guests only get 10 requests per IP!

A
RateLimiter::for('uploads', function (Request $request) {
    return $request->user()
                ? Limit::perMinute(100)->by($request->user()->id)
                : Limit::perMinute(10)->by($request->ip());
});
25
Q

A user() can either be a vipCustomer(), or not. Create a rate limit for “uploads”, where VIP customers get no rate limit, while non-VIP customers can only have 100 requests per minute!

A
RateLimiter::for('uploads', function (Request $request) {
    return $request->user()->vipCustomer()
                ? Limit::none()
                : Limit::perMinute(100);
});
26
Q

How can you rate limit the amount of login requests to 500 per minute, while rate limiting the amount of input emails to 3 per minute in the same rate limiter?

A

By returning an array of rate limits within a RateLimiter:

RateLimiter::for('login', function (Request $request) {
    return [
        Limit::perMinute(500),
        Limit::perMinute(3)->by($request->input('email')),
    ];
});
27
Q

Which middleware should be used to attach a rate limiter to a route / route group?

A

The throttle middleware gets used for it. Here is an example of it being used on a small group of routes:

Route::middleware(['throttle:uploads'])->group(function () {
    Route::post('/audio', function () {
        // ...
    });
 
    Route::post('/video', function () {
        // ...
    });
});
28
Q

HTML does not support actions like PUT. Given the form <form action="/example" method="POST">, turn the method into PUT! You may use Blade.

A

Using Blade:

<form action="/example" method="POST">
    @method('PUT')
</form>

Without Blade:

<form action="/example" method="POST">
    <input type="hidden" name="_method" value="PUT">
</form>
29
Q

Define $route as the current route!

A

$route = Route::current(); // Illuminate\Routing\Route

30
Q

Define $name as the current route’s name!

A

$name = Route::currentRouteName(); // string

31
Q

Define $action as the current route’s action!

A

$action = Route::currentRouteAction(); // string

32
Q

Cache all routes of your application using Artisan!

A

php artisan route:cache

33
Q

Clear the route cache of your application using Artisan!

A

php artisan route:clear

34
Q

Where are all requests by the web server (Apache / Nginx) directed to?

A

public/index.php

35
Q

What is a service provider responsible for?

A

Bootstrapping all of the framework’s various components, such as the database, queue, validation and routing components.

36
Q

What is dependency injection?

A

Class dependencies are “injected” into the class via the constructor or, in some cases, “setter” methods.

37
Q

What are service containers used for?

A

Managing class dependencies and performing dependency injection.

38
Q

Where are user-defined service providers registered?

A

bootstrap/providers.php

39
Q

What does bootstrapping mean?

A

Registering things like service container bindings, event listeners, middleware and routes.

40
Q

Create a new service provider called “RiakServiceProvider” using Artisan!

A

php artisan make:provider RiakServiceProvider

41
Q

Which method is used to listen to the event, that service containers fire when resolving an object?

A

resolving();