Welcome!
@christophrumpel
Going on a journey through three parts. Request lifecycle, facades and Eloquent
Common excuses for not going through the source
- It's a tool!
- It takes a lot of time!
- It can be overwhelming
Why should you care?
It speaks to you, become better at debugging, learn from the best, become a better developer
Lifecycle
Let's begin in the public/index.php file.
- defines constant of LARAVEL_START...which does nothing and isn't used anywhere else
- Require the composer vendor autoloader
- Turn on the lights and require bootstrap/app.php and assign it to
$app
- $app is the application instance, also services as DIC, bind kernels to the container, register base providers, set base paths
- Run the application by making an HTTP Kernel class
- Set the response to the result of
$kernel->handle($request = Illuminate\Http\Request::capture())
; - Send the response
- Notify the kernel to terminate given the request and response
Send the request through the router (and Facade::clearResolvedInstance so we get a fresh instance). Then bootstrap (load environment variables, config, service providers)
Return a new pipeline with the app, send a request, then process middleware, then dispatch to the router
In the request and router (Illuminate/Routing/Router), findRoute. Only one route gets matched though, and we need to run it!
Facades
People are passionate about their reaction towards Facades (magic! proxies! misleading! tightly-coupled! "Bad practice"!)
Example
Route::get('conference, function() {
$year = Request::get('year');
});
The request facade just has a getFacadeAccessor that returns 'request'
Base facade calls static::getFacadeRoot(). If it's not there, runtime exception. Otherwise, return $request->$method(...$args)
There are some advantages and disadvantages.
Misleading? Maybe, especially at the beginning with how it works. Hard to test? Nah. Tightly-coupled? Sure. Bad practice? Well, what is bad practice? We're not sanitizing bad input or stuff like that, we're passing everything faithfully along. You don't have to use them, but they have their advantages. And Taylor does like using Facades!
Eloquent
Object-Relational Mapper, Active Record, Models, Builders
Example today: "speakers", "conferences", "conference_speaker" with many-to-many relationship
No static "where" method in base Model, but there is a __callStatic($method, $parameters) function that returns (new static)->$method(...$parameters);
In model, well, there's a __call method that checks if the method is increment or decrement, and if not, return $this->forwardCallTo($this->newQuery());
Found the where method! It was in Eloquent/Builder.php!
What about first? Eloquent/Builder through a BuildsQueries trait
What about the relationship between the conference and speakers? I mean, we have a belongsToMany response from speakers(), but there's another magic __get($key) call that gets returns $this->getAttribute($key);
getAttribute checks if the array key exists between the key and list of attributes...speakers isn't there, but maybe it's a relationValue? Call getRelationValue() and there we go!
Eloquent is super powerful and cool.
Master your tools. Laravel is one of them.
https://laravelcoreadventures.com...and now with Pro! Turns out you can't make a living giving videos for free :p