Next up we have @bobbybouwmann who will be talking about Laravel Router.
Thread ๐งต
And now he's updating Zoom in the background ๐
That's a good menu
We'll be spending most of the time here in web.php
Different options in Laravel to register routes.
Multiple routes for "one" resource.
Different HTTP methods available. We can reuse same URI but with different methods.
route:list command output
with --compact option
Route::resource() method. It is a time saver but its also PITA if you need fewer methods/routes from it.
There's also a way to handle with above situation using only() and except() methods.
Downside of this method is if you want to see all the routes, you will need to run route:list command. You can't tell by looking at Route::resource in web.php file
Other options like prefix, middleware, domain. You can also put these in a group so the options apply to all routes in a group.
Groups can also be nested. New groups with different conditions.
route:list command also accepts options to display on the table.
Every route has a middleware called web. But we didn't apply it anywhere inside web.php file.
RouteServiceProvider located in app/Providers.
In previous versions ( <8) of Laravel ) we needed to provide $namespace property on provider.
boot method applies 'web' middleware.
Route::match method, accept multiple HTTP methods based on the method being posted.
Route::redirect() and Route::permanentRedirect()
These are handled by RoutingController , woah, didn't know this.
Route::view() is also handled by a Controller.
Route::fallback() method can be used to provide a fallback url if none route matches. It should be at the end.
Route names
You can change the URI but since the name is still. same you don't need to change it everywhere else.
Pass arguments to route() helper method when using route names.
Adding extra parameters "will break" the route.
redirect to certain route using its name
Route::current()
Route::currentRouteName();
Route::currentRouteAction();
Some other helpful methods on Route class.
How do we get from Request to Response?
P.S.
Is Pizza coming? ๐
First file that request touches is index.php,
From there it goes to Kernel.php
Some middlewares are being registered.
First array of middlewares are applied on all requests.
middlewareGroups apply on grouped routes.
Looking handle method of HttpKernel inside laravel/framework.
handle method does one thing, send response to request.
First sendRequestThroughRouter
Resolve 'request' and then call $this->bootstrap()
Part of booting, RouteServiceProvider is booted and routes() method is calld
If routesAreCached using the route:cache command, load all the routes from there. Otherwise load the routes.
Digging more into loadRoutes()
It will call all those methods when we do $this->app->call
Generic action for all routes in las screenshot.
where statements can be seen being applied at the end.
prefixes are being applied.
Now that routes are loaded, last method calls to refreshNameLookups register the name of routes and also a method for Actions.
Next is the Pipeline ( interesting stuff )
Request is being passed through middlewares.
Request is being bind again in dispatchToRouter as it has been modified.
We first findRoute and then runRoute
match() method gives the first route that matches.
matchAgainstRoutes() method, which loops over all routes and checks for fallback first.
Laravel routing depends on SymfonyRoute which at the end finds the "route".
Damn, that's a lot of things happening. Hard to keep up ๐
First we bind current request to route. NotFoundHttpException if route is not found.
if we found the route, we put it in Container.
Then we go to runRoute() after that runRouteWithinStack , "onion" instance.
That's a lot of layers.
prepareResponse method is responsible for preparing the response that needs to be sent.
That's a lot happening in `toResponse()` method ... :mind_blow:
RequestHandled event. You can use it do something you want like, how many Requests handled by your app.