The Laravel Core - Demystify The Beast

Christoph Rumpel

Demystifying the Laravel core: discussing the request lifecycle, facades, and eloquent.

Transcript

00:00:00 okay hello everybody how are you doing good perfect are you ready for some love or core stuff yeah a little bit louder yes good I like that so my name is Christoph Oompa so he was quite close but you can also say Rumple it's a little bit more funny I'm a web developer from a city called Vienna in Austria we maybe have been there as well and you can tweet about this talk with my twitter handle which is just christophe oompa my name in one word to get up very easy to remember you can also stay in contact

00:00:38 with me like that you can also do some pictures if you want I always appreciate that so yeah please use it and yeah let's start this talk so it's called the leather cord demystify the beast and yeah we are all here because Louisville is an amazing framework and amazing product it brings us all together here so I think that's already quite quite something good but yeah when we talk about the code can be a little bit complicated we hear things like it's magic and magic is good when we are kids because I'm we like magic

00:01:15 but when we grow all the magic is not so fun anymore because maybe we're afraid of it and actually we want to demystify it and that's why I'm here today so I'm going to take you on a little journey where we talk about three parts of Louisville so there is the request lifecycle we're talking about facades and about eloquent and of course I don't have time to tell you everything about these products because they are quite big but I hope I could give you a little introduction into all of these today ok

00:01:45 so before that we need to talk about why you should care about something like the core of levels and there are some common excuses like what do you have it's a truth so like my presenter here today it's a tool that I'm using I'm capable of using it at least it seems like that but do I need to know how it works I don't know how it works I think it has something to do with infrared I don't know I don't care just want to use it then of course it takes a lot of time digging into things like probably this but also the cold

00:02:21 elbow and it can be overwhelming little ball it's quite a big application it's quite complex and yeah it's not so easy but I think still there are some reasons why you should care so first it speaks to you of course this percent that doesn't speaks to you at least not to me but the core that speaks to me when I take a look at the classes how can I use this class is what are the properties what are the arguments what are methods it's like documentation but in real so I really like digging into the core to

00:02:53 check out how I can use stuff then the more you know about the core the better you'll become at debugging because with level and other frameworks of course sometimes you get error messages they are saying there was an error here but you're working here and how are these things connected and the ll was quite complex and the more you know about the core the better you become at connecting these dots and yeah it will help you then of course level is an open-source framework and it has almost 2,000

00:03:23 contributors so a lot of people put their heads together to solve a lot of complex problems and by looking at the code we are actually looking yeah their solution so we can only learn from them in the end I truly believe by knowing more about the core you will become a better developer all right so let's that we have the request lifecycle so what is that the request lifecycle starts with the browser sending an HTTP request to your application then we need to put up the framework we need to handle the

00:03:57 requests so all your business logic is happening here and then we send it back to a browser and this is what we talk about when we say the request lifecycle and it's all starts in this index dot PHP file which you will find in your public folder and yeah let's take a look at that because it's not too long and it's quite good shows what's happening here okay so the funny thing here now this first line here we're defining a constant called level start with the current timestamp and it's never been

00:04:26 used in the entire again so you could delete it and everything should work I would still keep it there but it's not my fault if you delete it but it should still work because it's not used but of course packages likely elbow telescope or lever debug bar are using this constant to make assumption about the performance application so maybe let's keep it there but yeah I find it funny that that's the first thing that's happening you okay now it gets more interesting we are requiring the auto load file from

00:04:59 composer so from this point and we are capable of spinning up new instances because before that peach PIN level don't know where to find these PHP classes so this is why we're making use of this composer functionality okay good let's move on now it gets more interesting we're going to turn on the lights so that's what Stella said here in this comment and what we're doing here is we're getting application instance by requiring this bootstrap f5 and there are some a bunch of important things happening in the back so what do

00:05:33 we have we get an application instance back ok we already know that but we are also going to get the dependency injection container and allowable the application instance is also the dependency injection container so every time you use the container you're also making use of the app helper or the application instance then we need to bind so it's too fast here Europe we also need to bind the main kernels so we have the HTTP kernel and the cons worker non-level today we are mostly talking about the HTTP kernel then we are also

00:06:08 going to register the main base providers so f1 for the logging system one for the event system and one for the routing system and these are free features of lava that we need quite early and this is why we load them here ok what else do we got Anya we set some base paths or the level knows where it lives inside your machine ok we're good to go let's move on now we're going to run the application we are getting an instance of the kernel from the service container this is now the HTTP kernel and

00:06:41 we can do now is we can run this handle method and every colonel in Louisville has this one public method and disc handle method and he were putting in some content and we're getting back a response and what we put in here is this request instance and this instance has all information about the incoming requests like the endpoint the HTTP verb the global wearables everything that we need and we put it in into this kernel method and then we get back a response and this response has already all information that we need to send it back

00:07:12 to the browser so that's the next thing that's happening here is so maybe we have some HTML we need to send back to the browser maybe some JSON data maybe we need to set some HTTP headers and that's all done here by the send method and now I'm the request lifecycle is almost finished there's just this terminate method here because some tasks need to run after the request has been sent back to the browser so for example you can define middle words that run here afterwards and one example would be

00:07:42 the session where some session information is stored here and that's already the request lifecycle we get this incoming request we're booting up the framework we're handling it through the kernel and then we send it back to the browser of course behind this handle method a lot of things are happening like all the code that you write is happening behind this method and as mentioned I don't have time to show you everything but I got two examples I want to show you one from the kernel and one from the router all right so this is a

00:08:14 method called send request through the router and it's inside the HTTP Chrome and here we are preparing the request before we send it further on to the router and the first thing that we're doing here is binding this new request to the container so the next time someone needs this request it gets the fresh instance of it then we also need to clear our already resolved facades for the request facade so we're going to talk about facades in just a minute but good to know here ready is the first time we use it for solid like to request

00:08:47 facade the instant gets stored so the next time we don't need to get it from the container again so this is why we need to clear it out so again that we get a new fresh instance of the request facade when we use it then also inside the kernel we have some kind of bootstrapping to do some stuff so what's happening here is we are lowering the environment files we are loading the configuration files we are loading the rest of the service providers so all the others from the framework but also the ones that you

00:09:16 have created other ones from packages they all are running here with the register and the boot method and this way we're putting in more features to the framework that we then can use in our business logic a little bit later okay and here at the end we have a so-called pipeline pattern Bobby would be proud and not a pattern here so where is it yeah so have you already used the pipeline pattern or do you know about it okay just a few good that I'm here today so that you can a little bit learn about this pattern it's

00:09:51 a really cool pattern you see several times in the framework so you have like some kind of content and you send it through so-called pipes and every pipe has the possibility to change the content in some way and then you get a modified content back and the best example is already the one here because we're sending the request instance through Elvis clover middle words they are finding the HTML class and they are responsible for checking the maintenance mode trusted proxy or validate the post size and at the end we get a new request

00:10:24 incident or it's the same but it's modified and now we send it further on to the router class and this is also where I got a second example from the router class and here I've got two things that show you what's yeah what were the most important things happening in the router so first we're going to find the route and we can do it with the request instance because it's mentioned in this object we have all the information about the endpoint the HTTP verb and this helps us to find a matching route and allow only one rod is

00:10:54 being matched and this is what we get here and when we get around we also need to run it and in level you combine the route to control a method but you can also use a callable connected to route it doesn't matter what it is the core inside is now being run so for example you're going to return a few object or maybe some json data and level is smart enough in the back to know when it gets this information to create a response object of it with all information then for the browser and then sends it back

00:11:28 okay so this is already it for a request lifecycle just remember index dot PHP file is the main entry port for every HTTP request hitting your application check it out it's quite interesting and very small good to read then we have two HTTP kernel and also a router is very important ok let's talk about facades funny topic to talk about there are a lot of opinions about facades people love them people hate them I'm not sure if there anything in between bird yet always it's always risky to talk about

00:12:05 facade and I think that's why I like it so much so when we talk about facade to hear something like they are kind of match your proxies their helpers to use some kind of static interfaces whatever that means and they're somehow connected to the service country ok fair enough but you also hear something like they're misleading hard to test tightly covered to the framework and my favorite one bad practice and first I'm going to explore if you together how facades are working and then we come back to these

00:12:39 statements and I will give you my humble opinion about that so the example that we have today is a route for the conference endpoint and we're using the request facade together get the Year parameter from the UL so maybe the route is Conference Year 2019 and want to get this 2019 through a request facade ok so how can we do that but looking at the code we see we're using this request for south class and a static add method call ok so the first place to would be the request for south chairs and here you see it it's pretty empty we

00:13:16 only have this get facade accessor method where we return some kind of string but there is no static gab method so where does it come from so what we see here is we extending a base facade so maybe in its in their arm so yeah let's try to find out but actually it's not in there but what's in there in the space facade is this magic PHP method called static and it's responsible for a static method cause where this method does not exist and if this method is defined in this class now this is being

00:13:47 triggered and here something interesting is happening we're getting a new instance from a facade Rude and this is important to understand because every time we use a facade we're actually using an underlying different class or you can also call it a service class and we're getting bills from the facade route by this get facade accessor method so this is telling Leila when someone wants the request facade actually we are calling and the service container and with the key request we get a different

00:14:18 class back that we can use them okay so now we got a different instance we are first checking if this instance is given and then we also run now the same method so we're still talking about this get method but now on a different class and this different class for the request facade in this case it's this here and now you will find the request class inside illuminate HTTP namespace and here we have to scan method that we are now actually calling and now we're getting the URL parameter and we send it back ok

00:14:53 cool so this is how facades are working we're using this static interfaces aesthetic methods here and the back we're using a facade route to get the actual each class that we want to use we create a new instance of it and then call the method that we want to use basically it's very similar to just asking the service container which we can do with the helper method for this class but it's not basically the same because there are some advantages when you use facade and there are some disadvantages

00:15:24 okay let's go back to these statements alphas are misleading I would say yes at least they could be especially at the beginning is difficult to understand how they are working what are the classes that it's being used how it's disconnected that's not so easy so I think there are misleading or could be re hard to test I don't have enough time to show you but they are not they provide some nice helper methods that you can use to easily swap out implementation and sometimes they even make testing a little bit easier with

00:15:55 providing some cool helper methods example for the notification system so I wouldn't say they're hard to test out it tightly coupled to the framework yes of course you can only use them inside the framework and yeah are they bad practice that's a funny one because first we need to talk about what is bad practice I think we are good to go when we say not sanitizing any data from the user we put in maybe that's bad practice we shouldn't do that but using facade is not something that I would say it's bad

00:16:28 practice it comes with some decisions about Yahoo tech chure so the there are some advantages if you want to do some more complex architecture if you want to use more dependency injection then maybe facade not what do you want to use but there are also some advantages it's very easy to use it's good to read Taylor likes using facades I think that that's one thing but I think I could use it as well and I like using them so I would say they are not bad practice but of course there are advantages and disadvantages and you're

00:17:01 smart people put your heads together with your team and your colleagues and yeah make it up to yourself if you want to use them or not I like them I'm sorry okay what else do we have eloquence so what is eloquent eloquent is the OM of level which stands for object relational mapper and it uses the active record pattern basically it does this just means we like to use with objects in our application and we have a model for every database table and we can save update or delete the model so that's basically what that means and we

00:17:37 some kind of builders to make us help do these things for example today we have free tables we have speakers table we have a conference table and we have an intermediate table called conference speaker so this means we have a many to many relationship between speakers and conferences and we're extending the example from before so now that we got the year from the UL we want to get a specific conference only the first one and then get the speakers okay so that's pretty simple eloquent code you probably

00:18:11 have already done something similar but now we want to explore again how it's working and we're going to start here again with the first method called and its aesthetic we're method on the conference model okay fair enough let's take a look at the conference model it's pretty basic again we only have our relationship define but there is no static we're meant to define okay good but we're seeing again we extending a base model so maybe this static where method is inside the base model do you think it's in there raise your hands

00:18:45 okay just a few and sorry it's not in there but again we're making use of this magic PHP method so this is also defined inside the base model class and this is why it's being triggered because the static where matter is not define okay and now we're saying okay we'll try this again but now we're creating a new instance and we're running the same method the where method on this instance again but this time it's not a static method okay so maybe this non static where method is being defined inside the

00:19:16 conference model or a base model but we've already seen it's not inside the conference model and yeah it's also not inside the base model this is why we make use of another magic PHP method called underscore underscore call so this is responsible for cause to a method where the method does not exist but this time it's not a static method and first we're checking if it's about incrementing or decrementing which is not the case here and then we're going to forward this call to another instance so this is also a nice pattern that you

00:19:48 see in level quite often in the core where we move this called forward to another instance and this different instance now is what we get here from this new career method and here we get back our first bill and that's the eloquent biller so now we're running this we're method on the eloquent biller and of course we also have our parameters and inside the eloquent biller we now find this where method so finally we found it it's in there and now we can make our condition so in this case we're adding a

00:20:18 constraint which looks like give us the conference's were the year is 2019 it's being stored inside the eloquent pillar and we're returning the same opportunity again the eloquent below and we do this because now we can change other methods so maybe we have another where method or in our case what we have is another first method okay so what's happening with this first method and yeah it's also being found inside the eloquent builder through a trade which is called builds queries and this method translates to free other

00:20:53 methods so the first one is to take method I'm pretty sure you have used it before we want to limit our query to get only one result back then we have this get method and this is not responsible for executing the query and this always gives us back a new collection instance and this is why we need to run now another first method but this time it's not the same method like before not the same method that we have defined here it's now a different method from the collection class because we of course we

00:21:25 don't want to get the collection bag just one instance ok cool so now we got our conference back here we've used the first method so we have a conference instance and now we want to get the speaker's ok again pretty basic level code it's a relationship it just works but again we want to know more we want to dive into that and check out how this is working because it yeah it's quite cool hot as it's working so it looks like we're calling a property called speakers on the conference model and as you've seen in our conference model we

00:21:59 have no properties defined again the only thing we have is we extending the base model so maybe the speaker's properties inside the base model but hear anything about it it wouldn't make sense at all because Louisville has no idea about the relationships that we do but in there in this base model there is another magic PHP method and I think we now got almost all magic PHP method inside level score and this is now called underscore and O's cross ket and it's responsible for property calls where the property does

00:22:32 not exist okay and 11 well nothing's okay I think it's about attributes so let's try to get this attribute for the key and the key in our case is the speaker's because we want to get the speaker's okay so when we check out this method here we're first checking if the key exists and then we are trying to get this speakers from the attributes and what it needs to non level with every model you have you have an array of attributes where all the fields of the database table are inside so for a conference model in this

00:23:07 case that would be the ID the name and the year but speakers is not in there because pikas is not a field of the conference table it's a relationship and now level is small enough so checks first if the method exists and we want to stop that but here at the end level of things okay we're trying to get a property it's not an attribute maybe maybe it's a relationship and now with this method level checks if the relationship is defined we have a defined and this is how we get back the speaker's here and when you look at the

00:23:42 code it's it's super simple but what's happening in the bag is pretty amazing we're making use of the model we're making use of the eloquent builder we're making use of the collection and then we're making use of the relationship and all this in one line of code and this makes eloquent so super powerful and I find it super interesting looking at how this is implemented because yeah it's it's pretty cool and eloquent is one of my favorite features of level thank thanks I didn't create eloquent I

00:24:17 wish I did maybe Jonathan raining did it I'm not sure there are some rumors okay so what do we have here so we talked about the request lifecycle we talked about facades and we talked about eloquent and I hope I could give you a good introduction into all of these topics of course there's so much more to learn by diving into them in detail and you really should do that and I think this is super interesting because yeah we should all master all tools especially the tools that we use every day and I'm pretty sure that most of you

00:24:53 use level everyday it's a tool and it's important to master them because at first it will make you a better developer and you will feel much more comfortable while using it and yeah if you want to move on this journey learn more about the core of course become a better developer then I recommend my video course level chord ventures who if you have already watched at least one video ok a few here we see in the back not too much in the back yet we need to change it when you go home today or maybe two more after partying and please

00:25:32 check it out and I have the videos about lower score we dive into these topics like today but in more detail and a lot of video of on different topics and today I have also something new for you called love our core adventures Pro yeah so this is now a new paid version for love accord when sure it turned out that I can't make a living from providing everything for free I have no idea that nobody told me before yeah so I think we made a good deal here that yeah about half a little bit less of the videos are

00:26:11 still free for the rest you need to pay me a little bit and I give you more content on the way so I think it's a good deal please check it out if you like what I've done or if you like the talk please go to love all Co adventures come check out the free videos check out the pro and also with this with this new version I also recall some new videos about the service container it's about 1 hours of content I think about 6 or 7 videos where I teach everything about why you want to use the container when you want to use the

00:26:45 container how you can use the container and of course we're checking out how it works in the bed and for me I really think it's one of the best explained NT explanations of the service container because I struggled with the topic myself a lot and I think it's quite good so please check it out I also have some fancy stickers here so please come to me or they also lying around I got enough for everybody and yeah let's all keep exploring thank you very much you


Highlights

00:00-00:55 👋 Introduction and explanation of the talk’s topic.
02:10-03:05 🗺️ Importance of understanding the Laravel core for better development and debugging.
06:12-07:24 🔄 Request lifecycle and how it starts with the browser sending an HTTP request to the application.
11:50-12:50 💡 Advantages and disadvantages of using facades in Laravel.
16:48-17:55 🌟 Explanation of how eloquent works in Laravel and its power in simplifying complex database operations.
21:30-22:10 💼 Promotion of Christoph Rumpel’s video course, Laravel Core Adventures, for further learning.
23:00-23:30 🎁 Introduction of a new paid version, Laravel Core Adventures Pro, and the additional content available.

Key Insights

👀 Understanding the Laravel core helps developers in understanding how classes, methods, and properties are used, improving their ability to utilize the framework effectively.
⚙️ Knowledge of the request lifecycle in Laravel enables developers to better understand how the framework handles incoming requests and how to troubleshoot issues.
🤝 Facades in Laravel provide a convenient way to access services and classes, but they can be misleading and require careful consideration in terms of architecture and maintainability.
🌐 Laravel’s eloquent ORM simplifies database interactions by using object-oriented models, making it easier to perform CRUD operations and handle relationships.
💡 Exploring the Laravel core and learning from the open-source community can enhance a developer’s skills and contribute to their growth as a professional.
🎥 Christoph Rumpel’s video course, Laravel Core Adventures, offers in-depth learning on Laravel’s core concepts, including the service container and more.
💼 The introduction of Laravel Core Adventures Pro provides additional paid content for those seeking a deeper understanding of Laravel’s core and supporting the creator of the course.