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;
});
Write two get routes that use a regular expression constraint to ensure: (1) an alphabetic parameter and (2) a numeric parameter.
Route::get(‘user/{name}, $callback)->where(‘name’, ‘[A-Za-z]+’);
Route::get(‘user/{id}, $callback)->where(‘id’, ‘[1-9]+’);
Write one route that has both id and name parameters each constrained to numerical and name constrained to alphabetical.
Route::get(‘user/{id}/{name}’, $callback)->where([‘id’ => ‘[0-9]+’, ‘name’ => ‘[A-Za-z]+’);
How do you set global parameter constraints? Set one for ‘id’ to be numerical.
In RouteServiceProvider’s boot() method, add:
Route::pattern(‘id’, ‘[0-9]+’);
Automatically applied to all routes using that parameter name.
What characters may form part of a parameter’s value?
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’, ‘.*’);
What are the benefits of named routes?
Easily generate URIs and redirects.
Change URIs in your route files without having to update them in Blade files.
How to set a name for a route?
Chain the name method onto the route definition.
Route::get(‘users’, $callback)->name(‘profiles’);
How to generate a URL to a named route? Write code to set $url of the route named ‘profile’.
$url = route(‘profile’);
Write code to return a redirect to a route named ‘profile’.
return redirect()->route(‘profile’);
Write code to set $url of the route below:
Route::get(‘user/{id}/profile’, $callback)->name(‘profile’);
$url = route(‘profile’, [‘id’ => $id]);
How to determine whether the current request is a given named route?
$request->route()->named(‘users’);
Returns boolean