Basics Flashcards
The most basic Laravel routes accept a _ and a _
URI and a Closure
Write a simple Closure-based get route.
Route::get(‘foo’, function () {
return ‘Hello World’;
});
What directory are the route files stored in?
The ‘routes’ directory
Where is RouteServiceProvider.php stored?
In the ‘app/Providers’ directory
What does the RouteServiceProvider do for routes/web.php?
Applies the ‘web’ middleware group.
What does the RouteServiceProvider do for routes/api.php?
Applies the ‘web’ middleware group, and applies the ‘/api’ URI prefix.
How would you modify the default ‘/api’ routes prefix?
Modify it in the RouteServiceProvider.
What HTTP verbs can you register routes for?
get post put patch delete options
How would you register a route that responds to multiple HTTP verbs?
Route::match([‘get’, ‘post’], ‘/’, $callback);
or
Route::any(‘/’, $callback);
What does Route::match do?
Registers a route that responds to multiple HTTP verbs.
What does Route::any do?
Registers a route that responds to any HTTP verb.
Which incoming HTTP verbs are checked for the existence of a CSRF token?
post put patch delete (only applies to web middleware's routes)
How to define a route that redirects to another URI?
Route::redirect(‘/here’, ‘/there’);
What is the default status code returned with Route::redirect?
302
How to return a Route::redirect with a customise status code?
Route::redirect(‘/here’, ‘/there’, 301);
How to return a permanent redirect on a URI?
Route::permanentRedirect(‘/here’, ‘/there’)
What Route method to use if your route only needs to return a view without going through a controller?
Route::view(‘/welcome’, ‘welcome’)
How to pass data to a simple Route::view?
Route::view(‘/welcome’, ‘welcome’, [‘name’ => ‘Nicolas’]);
What is a route parameter?
It’s a captured segment of the URI.
Write a simple get route that captures and returns a parameter.
Route::get(‘user/{id}’, function ($id) {
return ‘User ‘.$id;
});
What is the syntax of a route parameter within registering a URI?
Enclosed in braces.
What characters may route parameter identifiers contain?
Only alphabetic and underscore.
How are route parameters injected into route callbacks and controllers?
Based only on their order.
How to make the presence of a route parameter in the URI optional?
Use ? after the identifier and supply a default value:
Route::get(‘user/{name?}’, function ($name = null) {
return $name;
});