Laravel Flashcards

1
Q

Your own application, as well as all of Laravel’s core services are bootstrapped via

A

service providers

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

the central place to configure your application is

A

Service providers

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

All service providers extend the

A

Illuminate\Support\ServiceProvider

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

Most service providers contain methods:

A

register and a boot

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

Within the register method of service provider, you should only bind things into the

A

service container

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

You should never attempt to register any event listeners, routes, or any other piece of functionality within the

A

register method

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

Within any of your service provider methods, you always have access to the $app property which provides

A

access to the service container:

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

This method is called after all other service providers have been registered, meaning you have access to all other services that have been registered by the framework

A

The Boot Method

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

To defer the loading of a provider, implement the

A

\Illuminate\Contracts\Support\DeferrableProvider

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

Facades provide a ? interface to classes that are available in the application’s service container

A

Static

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

All of Laravel’s facades are defined in the

A

Illuminate\Support\Facades

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

Using real times facades

A

You may treat all class in your system like it was facade
For example for App\Contracts\Publisher
Use
Facade\App\Contracts\Publisher

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

You may modify the prefix and other route group options by modifying your

A

RouteServiceProvider class

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

Sometimes you may need to register a route that responds to multiple HTTP verbs. You may do so using the match method. Or, you may even register a route that responds to all HTTP verbs using the any method

A
Route::match(['get', 'post'], '/', function () {
    //
});
Route::any('/', function () {
    //
});
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

If you are defining a route that redirects to another URI, you may use the

A

Route::redirect

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

By default, Route::redirect returns a

A

302

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

You may use the ? method to return a 301 status code:

A

Route::permanentRedirect

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

If your route only needs to return a view, you may use the

A

Route::view method

Route::view(‘/welcome’, ‘welcome’, [‘name’ => ‘Taylor’]);

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

You may constrain the format of your route parameters using the WHAT method on a route instance

A

where

Route::get('user/{name}', function ($name) {
    //
})->where('name', '[A-Za-z]+');
Route::get('user/{id}', function ($id) {
    //
})->where('id', '[0-9]+');
Route::get('user/{id}/{name}', function ($id, $name) {
    //
})->where(['id' => '[0-9]+', 'name' => '[a-z]+']);
20
Q

If you would like a route parameter to always be constrained by a given regular expression, you may use the pattern method. You should define these patterns in the boot method of your RouteServiceProvider:

A

public function boot()
{
Route::pattern(‘id’, ‘[0-9]+’);

parent::boot(); }
21
Q

The Laravel routing component allows all characters except /. You must explicitly allow / to be part of your placeholder using a where condition regular expression:

A

Route::get(‘search/{search}’, function ($search) {
return $search;
})->where(‘search’, ‘.*’);

22
Q

If you would like to determine if the current request was routed to a given named route, you may use the named method on a Route instance. For example, you may check the current route name from a route middleware

A
public function handle($request, Closure $next)
{
    if ($request->route()->named('profile')) {
        //
    }
return $next($request); }
23
Q

Sub domain routing

A
Route::domain('{account}.myapp.com')->group(function () {
    Route::get('user/{id}', function ($account, $id) {
        //
    });
});
24
Q

To register an explicit binding, use the router’s model method to specify the class for a given parameter. You should define your explicit model bindings in the boot method of the RouteServiceProvider class:

A

public function boot()
{
parent::boot();

Route::model('user', App\User::class); }
25
Q

If you wish to use your own resolution logic, you may use the Route::bind method. The Closure you pass to the bind method will receive the value of the URI segment and should return the instance of the class that should be injected into the route:

/**
 * Bootstrap any application services.
 *
 * @return void
 */
public function boot()
{
    parent::boot();
    Route::bind('user', function ($value) {
        return App\User::where('name', $value)->first() ?? abort(404);
    });
}
Alternatively, you may override the resolveRouteBinding method on your Eloquent model. This method will receive the value of the URI segment and should return the instance of the class that should be injected into the route:
A

public function resolveRouteBinding($value)
{
return $this->where(‘name’, $value)->first() ?? abort(404);
}

26
Q

Using the Route::fallback method, you may

A

define a route that will be executed when no other route matches the incoming request.

27
Q

Laravel includes a middleware to rate limit access to routes within your application. To get started, assign the throttle middleware to a route or a group of routes. The throttle middleware accepts two parameters that determine the maximum number of requests that can be made in a given number of minutes. For example, let’s specify that an authenticated user may access the following group of routes 60 times per minute:

A
Route::middleware('auth:api', 'throttle:60,1')->group(function () {
    Route::get('/user', function () {
        //
    });
});
28
Q

You may specify a dynamic request maximum based on an attribute of the authenticated User model. For example, if your User model contains a rate_limit attribute, you may pass the name of the attribute to the throttle middleware so that it is used to calculate the maximum request count:

A
Route::middleware('auth:api', 'throttle:rate_limit,1')->group(function () {
    Route::get('/user', function () {
        //
    });
});
29
Q

You may specify different rate limits for guest and authenticated users. For example, you may specify a maximum of 10 requests per minute for guests 60 for authenticated users:

A
Route::middleware('throttle:10|60,1')->group(function () {
    //
});
30
Q

You may use the current, currentRouteName, and currentRouteAction methods on

A

the Route facade to access information about the route handling the incoming request

31
Q

To pass the request deeper into the application (allowing the middleware to “pass”),

A

call the $next callback with the $request.

32
Q

If you want a middleware to run during every HTTP request to your application,

A

list the middleware class in the $middleware property of your app/Http/Kernel.php class.

33
Q

When assigning middleware, you may also pass

A
the fully qualified class name
Route::get('admin/profile', function () {
    //
})->middleware(CheckAge::class);
34
Q

Rarely, you may need your middleware to execute in a specific order but not have control over their order when they are assigned to the route. In this case, you may specify your middleware priority using the

A

$middlewarePriority property of your app/Http/Kernel.php file

35
Q

Additional middleware parameters will be passed to the middleware after the

A

$next argument:

36
Q

Middleware parameters may be specified when defining the route by separating the middleware name and parameters with a

A

:

37
Q

Sometimes a middleware may need to do some work after the HTTP response has been sent to the browser. For example, the “session” middleware included with Laravel writes the session data to storage after the response has been sent to the browser. If you define a WHAT method on your middleware and your web server is using FastCGI, the WHAT method will automatically be called after the response is sent to the browse

A

terminate

38
Q

exclude the routes by adding their URIs to the $except property of the VerifyCsrfToken middleware:

A
39
Q

You may generate an invokable controller by using the

A

–invokable option of the make:controller artisan command

40
Q

Controllers also allow you to register middleware using a Closure. This provides a convenient way to define a middleware for a single controller without defining an entire middleware class:

A
$this->middleware(function ($request, $next) {
    // ...
return $next($request); });
41
Q

You may register many resource controllers at once by passing an array to the resources method:

A

Route::resources([
‘photos’ => ‘PhotoController’,
‘posts’ => ‘PostController’
]);

42
Q

When declaring a resource route, you may specify a subset of actions the controller should handle instead of the full set of default actions:

A

Route::resource(‘photos’, ‘PhotoController’)->only([
‘index’, ‘show’
]);

Route::resource(‘photos’, ‘PhotoController’)->except([
‘create’, ‘store’, ‘update’, ‘destroy’
]);

43
Q

By default, all resource controller actions have a route name; however, you can override these names by passing a names array with your options:

A

Route::resource(‘photos’, ‘PhotoController’)->names([
‘create’ => ‘photos.build’
]);

44
Q

By default, Route::resource will create resource URIs using English verbs. If you need to localize the create and edit action verbs, you may use the Route::resourceVerbs method. This may be done in the boot method of your AppServiceProvider:

A

use Illuminate\Support\Facades\Route;

/**
 * Bootstrap any application services.
 *
 * @return void
 */
public function boot()
{
    Route::resourceVerbs([
        'create' => 'crear',
        'edit' => 'editar',
    ]);
}
45
Q

If your application is exclusively using controller based routes, you should take advantage of Laravel’s route cache. Using the route cache will drastically decrease the amount of time it takes to register all of your application’s routes. In some cases, your route registration may even be up to 100x faster. To generate a route cache, just execute the route:cache Artisan command:

A

php artisan route:cache

46
Q

You may use the route:clear command to clear the route cache:

A

php artisan route:clear