Basics Flashcards

1
Q

The most basic Laravel routes accept a _ and a _

A

URI and a Closure

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

Write a simple Closure-based get route.

A

Route::get(‘foo’, function () {
return ‘Hello World’;
});

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

What directory are the route files stored in?

A

The ‘routes’ directory

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

Where is RouteServiceProvider.php stored?

A

In the ‘app/Providers’ directory

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

What does the RouteServiceProvider do for routes/web.php?

A

Applies the ‘web’ middleware group.

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

What does the RouteServiceProvider do for routes/api.php?

A

Applies the ‘web’ middleware group, and applies the ‘/api’ URI prefix.

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

How would you modify the default ‘/api’ routes prefix?

A

Modify it in the RouteServiceProvider.

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

What HTTP verbs can you register routes for?

A
get
post
put
patch
delete
options
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How would you register a route that responds to multiple HTTP verbs?

A

Route::match([‘get’, ‘post’], ‘/’, $callback);
or
Route::any(‘/’, $callback);

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

What does Route::match do?

A

Registers a route that responds to multiple HTTP verbs.

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

What does Route::any do?

A

Registers a route that responds to any HTTP verb.

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

Which incoming HTTP verbs are checked for the existence of a CSRF token?

A
post
put
patch
delete
(only applies to web middleware's routes)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How to define a route that redirects to another URI?

A

Route::redirect(‘/here’, ‘/there’);

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

What is the default status code returned with Route::redirect?

A

302

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

How to return a Route::redirect with a customise status code?

A

Route::redirect(‘/here’, ‘/there’, 301);

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

How to return a permanent redirect on a URI?

A

Route::permanentRedirect(‘/here’, ‘/there’)

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

What Route method to use if your route only needs to return a view without going through a controller?

A

Route::view(‘/welcome’, ‘welcome’)

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

How to pass data to a simple Route::view?

A

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

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

What is a route parameter?

A

It’s a captured segment of the URI.

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

Write a simple get route that captures and returns a parameter.

A

Route::get(‘user/{id}’, function ($id) {
return ‘User ‘.$id;
});

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

What is the syntax of a route parameter within registering a URI?

A

Enclosed in braces.

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

What characters may route parameter identifiers contain?

A

Only alphabetic and underscore.

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

How are route parameters injected into route callbacks and controllers?

A

Based only on their order.

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

How to make the presence of a route parameter in the URI optional?

A

Use ? after the identifier and supply a default value:

Route::get(‘user/{name?}’, function ($name = null) {
return $name;
});

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

Write two get routes that use a regular expression constraint to ensure: (1) an alphabetic parameter and (2) a numeric parameter.

A

Route::get(‘user/{name}, $callback)->where(‘name’, ‘[A-Za-z]+’);

Route::get(‘user/{id}, $callback)->where(‘id’, ‘[1-9]+’);

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

Write one route that has both id and name parameters each constrained to numerical and name constrained to alphabetical.

A

Route::get(‘user/{id}/{name}’, $callback)->where([‘id’ => ‘[0-9]+’, ‘name’ => ‘[A-Za-z]+’);

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

How do you set global parameter constraints? Set one for ‘id’ to be numerical.

A

In RouteServiceProvider’s boot() method, add:

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

Automatically applied to all routes using that parameter name.

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

What characters may form part of a parameter’s value?

A

All characters except forward-slash. You may include forward-slash as well (in ONLY the last segment of the URI/route) using:

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

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

What are the benefits of named routes?

A

Easily generate URIs and redirects.

Change URIs in your route files without having to update them in Blade files.

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

How to set a name for a route?

A

Chain the name method onto the route definition.

Route::get(‘users’, $callback)->name(‘profiles’);

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

How to generate a URL to a named route? Write code to set $url of the route named ‘profile’.

A

$url = route(‘profile’);

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

Write code to return a redirect to a route named ‘profile’.

A

return redirect()->route(‘profile’);

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

Write code to set $url of the route below:

Route::get(‘user/{id}/profile’, $callback)->name(‘profile’);

A

$url = route(‘profile’, [‘id’ => $id]);

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

How to determine whether the current request is a given named route?

A

$request->route()->named(‘users’);

Returns boolean

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

How to assign middleware to all routes in a given group?

A
Route::middleware(['first', 'second'])->group(function () {
    // routes go here
});

The middleware method must go before the group method, and the middlewares are executed in the order they are listed in the array.

36
Q

Write code to prefix a group of routes with the URI segment ‘admin’.

A
Route::prefix('admin')->group(function(){
   Route::get('users', $callback);
   // matches '/admin/callback'
});
37
Q

Write code to prefix the names of a group of routes with the name ‘admin’.

A
Route::name('admin.')->group(function(){
   Route::get($url, $callback)->name('users');
   // is assigned the name 'admin.users'
});

Note: you must provide the trailing ‘.’ character within the prefix string.

38
Q

What is the benefit of route model binding?

A

A controller action will often query to retrieve the model of the ID it was passed. RMB relieves that by automatically passing/injecting the entire model matching the ID given in the route.

39
Q

What happens if during route model binding, the model’s ID cannot be found?

A

A 404 response will automatically be generated.

40
Q

How is implicit route model binding set up?

A

The model injected into the controller must be type-hinted and its identifier/name must match that of the route segment parameter.

41
Q

How to once-off customise the key used during route model binding?

A

Specify the column to search in the route parameter definition:

Route::get(‘/posts/{post:slug}’, $callback);

42
Q

How to ‘globally’ customise the key used during route model binding for a specific model?

A

Override the getRouteKeyName() method on the Eloquent model:

public function getRouteKeyName() {
return ‘slug’;
}

43
Q

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

A

Unhandled requests will automatically render a 404. However, you may define a fallback route (as the last route in web.php) with:
Route::fallback($callback);
All web middleware will still apply.

44
Q

Explain form method spoofing.

A

Because HTML forms do not support the PUT, PATCH or DELETE verbs, a hidden field is added to the form who’s value determines the request method.
In Blade, use the @method() directive.

45
Q

In Blade, what does @method(‘PUT’) expand to?

A

< input type=”hidden” name=”_method” value=”PUT” >

46
Q

In Blade, what does @csrf expand to?

A

< input type=”hidden” name=”_token” value=”{{ csrf_token() }}” >

47
Q

What is the practice of including the @method directive called?

A

Form method spoofing

48
Q

What does middleware do, in a nutshell?

A

Middleware filters incoming HTTP requests.

49
Q

Where are all provided middleware located?

A

In the ‘app/Http/Middleware’ directory.

50
Q

How to make a new middleware?

A

php artisan make:middleware MyMiddleware

Will place a new class in ‘app/Http/Middleware’ directory.

51
Q

When you are writing custom middleware, what code will pass the request deeper into the application assuming your tests pass?

A

return $next($request);

Will call the supplied $next callback with the $request object.

52
Q

What allows you to type-hint any dependencies you need within a middleware’s constructor?

A

All middleware are resolved via the service container.

53
Q

What code determines whether middleware runs before or after the application handles the request?

A

In your handle method, this code runs before:

public function handle($request, Closure $next) {
   // code here
   return $next($request);
}

This code runs after:

public function handle($request, Closure $next) {
   $response = $next($request);
   // code here
   return $response;
}
54
Q

What are global middleware?

A

These middleware are run during every request to your application. They are listed in the $middleware property of the app/Http/Kernel.php class.

55
Q

How to register a middleware you’ve created as global?

A

Add it to the list of middleware on the $middleware property of the app/Http/Kernel.php class.

56
Q

Outline the method of applying non-global (route-specific) middleware.

A

First assign the middleware a key within the $routeMiddleware property of the app/Http/Kernel.php class, then use that key when specifying middleware at the route level.

57
Q

Write code to assign the ‘auth’ middleware to one route.

A

Route::get($uri, $callback)->middleware(‘auth’);

You may also pass the fully qualified class name, rather than the key.

58
Q

Write code to assign two middleware to one route.

A

Route::get($uri, $callback)->middleware(‘one’, ‘two’);

You may also pass the fully qualified class name, rather than the key.

59
Q

Write code to apply ‘auth’ middleware to a group of routes.

A
Route::middleware('auth')->group(function(){
  // routes go here
});
60
Q

When assigning middleware to a group of routes, how to prevent the middleware from being applied to an individual route within the group?

A

Chain ->withoutMiddleware(‘name’); onto that route.

This can only apply to routeMiddleware and does not apply to global middleware.

61
Q

What does the VerifyCsrfToken middleware do?

A

Checks that the token in the request input matches the token stored in the session.

62
Q

How would you exclude a set of URIs from CSRF protection?

A

Place them outside of the web middleware group, or, add the URIs to the $except property of the VerifyCsrfToken middleware class. (You may use wildcards in the URLs, eg: ‘stripe/*’.)

63
Q

When is CSRF protection automatically disabled?

A

When running tests.

64
Q

What directory are controllers stored in?

A

app/Http/Controllers

65
Q

What is the main benefit to using Controllers?

A

Controllers group related request handling logic into a single class.

66
Q

All Controller classes extend which class?

A

They all extend the app/Http/Controller.php class, which itself extends the BaseController class.

67
Q

Does a Controller have to extend the BaseController class?

A

No, but then you will not have access to convenience features such as the middleware, validate and dispatch methods.

68
Q

How to define a route to a controller action?

A

Route::get(‘user’/{id}’, ‘UserController@show’);

Here, the show method on the UserController class will be executed. The route parameter will be passed to the method.

69
Q

Is it necessary to specify the full controller namespace when defining a controller route?

A

No, the RouteServiceProvider loads your routes within the App\Http\Controller namespace.

70
Q

If you nest your controllers deeper into the App\Http\Controllers directory, how to specify their namespace when registering a route?

A

Route:get(‘foo’, ‘Photos\SomeController@method’);

Assuming your full controller class namespace is:
App\Http\Controllers\Photos\SomeController
71
Q

How to structure a controller that only handles a single action?

A

Place a single __invoke method on the controller.

72
Q

How to register a route for a single action controller?

A

You do not need to specify the __invoke method:

Route::get(‘user/{id}’, ‘ShowProfile’);

73
Q

How to generate a single-action controller using artisan?

A

php artisan make:controller SomeController –invokable

74
Q

A single-action controller is also known as?

A

An invokable controller.

75
Q

Briefly, what are two ways to assign middleware?

A

Either to routes:
Route::get(‘users’, ‘UsersController@index’)->middleware(‘auth’);

or in the __construct method of a controller:
$this->middleware(‘auth’);

76
Q

Give examples of assigning middleware to a controller and how to restrict the middleware to only certain methods.

A

public function __construct()
{
$this->middleware(‘auth’);
$this->middleware(‘log’)->only(‘index’);
$this->middleware(‘subscribed’)->except(‘store’);
}

77
Q

Write the artisan command to create a controller to handle all “CRUD” HTTP requests for your app’s photos.

A

php artisan make:controller PhotoController –resource

78
Q

How to register a resourceful route to a resource controller?

A

Route::resource(‘photos’, ‘PhotosController’);

79
Q

How to register routes to many resource controllers at once?

A

Pass an array to the Route::resources (plural) method:

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

80
Q

When would you use the flag –model in artisan like this:

php artisan make:controller PhotoController –resource –model=Photo

A

When you want to use route model binding and would like the resource controller’s methods to type-hint a model instance.

81
Q

When declaring resource routes, how would you specify only a subset of actions the controller should handle?

A

Define partial resource routes be chaining on either the only or except methods to Route::resource:

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

82
Q

What if you need to add additional routes to a resource controller beyond the default set of resource routes?

A

Always define those routes BEFORE your call to Route::resource to avoid unintentional clashes.

83
Q

How does Laravel resolve controller calls?

A

Using the service container. Declared dependencies are automatically resolved and injected into the controller instance.

84
Q

If your controller method is expecting route parameters, but you also want other parameters injected, how to list them in the method header?

A

Always list your route parameters AFTER other dependencies like $request eg:

public function update(Request $request, $id)
{

}

85
Q

How to generate a route cache?

A

Run the artisan command:

php artisan route:cache

(You must exclusively use controller-based routes, no closures.)