Laravel Hidden Feature - Responsable Interface - No More Duplicate Code

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome to Lara tips in today's video we'll be looking at one of the hidden features of laravel that you might not know about and that hidden feature is responsible interface it can be very handy in order to remove the duplicate logic whenever we are returning a response from a controller let me show you how to use it first of all let me show you something here in the web.php I have this user create and user edit routes over here you can see here create an edit method if we go here inside we have this create method and edit method and most of the time the create and edit form will have same data only few of the data will be different but most of the time they will be same so in such scenario there can be a lot of duplicate Logics you can see here here I have only passed the rules but there can be so many other things in the real world scenario that we need to pass to this create form and this edit form you can see here we are using this create blade.php on both create and edit and I do this throughout my project here if we just see here inside the create I have this name and let me show you that over here in the users and here I am not showing the roles but let me just take that from welcome.blade let's say rows and let me just copy this thing from here and paste this over here like this and you can see this name and rows over here but you can already see that there are some duplicate code over here and using the responsible interface we can remove this duplication let me show you how to do this first of all let me show you under the hood what laravel does so here in the router dot PHP if you see here in this two response this method will be called whenever something is returned from the controller so let me show you that DD and here one and let me just refresh over here then you can see one over here which means that it will reach here and inside here laravel is checking the first thing that laravel is checking is if this response is the instance of this responsible interface then just call the two response method on this object and it will return whatever the response that is returned from this to response method of the responsible interface class whatever the class that is implementing this responsible interface it must have this two response method and whatever thing that is returned from here it will be visible on our website so now let me just go over here and let us try to refactor these two in order to use the responsible interface so here let me go here inside app and here inside the HTTP let me create so you can see her request now let us create responses okay responses slash user create response dot PHP and let me generate a PHP class and now here I'll implement responsible and you can see here illuminate contracts support responsible and if you go inside this responsible we need to implement this method and here I'll just paste it and whatever we return from here that will actually be rendered on The View so let's go to the user controller and just copy these things from here and paste it over here let me import the role and here we can see the common thing is this rolls over here you can see here these rules and these rows the common thing is this so we don't have to do anything over here but this is different here we have new user and here we have user variable the user which is already resolved by the laravel for us using the route model binding so now in order to access this data on this class we need to accept them in a Constructor so I'll say here public function construct and here let's say private user user here I am using Constructor property promotion feature of PSP we don't need to Define private user over here it will automatically Define internally so here this user will pass to this one and here instead of new user will say this user like this now we can easily return this user create response instead of this one so here I'll say return new user create response and here we'll pass the user so in this case it will be this one the new user like this and let me just remove that from here and let me go over here and let's say DD and do it here and now if I come here and rephrase then you can see the wrist here text over here which means that we are reaching inside the two response method of this user create response class since this is implementing this responsible contract we can see here in the router it will actually resolve the view data using this responsible class so now let me just remove this from here and let me come here and refresh then you can see this create one over here and now let me go over here in the user controller before we refactor this let me show you this over here one slash edit and we can see this name over here and I am not selecting the rules for now we can just check by looking at this name so here this name is automatically filled because it is coming from this loan user before whenever we are creating a new user then we didn't know the name but now here in this case we know the name of the user so it is automatically filled because we have passed here like this username so now here also we'll just do the same thing just return it from here like this and instead of new user we'll say dollar user like this and will remove all these things and now you can see here these looks so much cleaner than previously and now if I come here and rephrase then also it is working perfectly fine and you can see here how powerful the responsible interface can be and if you deep dive into laravel then you will get so many hidden features and if you want to know more about such features then don't forget to subscribe my channel and also don't forget to give a huge thumbs up because it helps in the YouTube algorithm so that's it for this video guys thank you for watching have a great day bye
Info
Channel: Laratips
Views: 12,445
Rating: undefined out of 5
Keywords: laravel for beginner, laravel beginner tutorial, laravel tutorial for beginners, laravel tutorial, laravel advanced, laravel advanced tutorial, laravel 8 advanced tutorial, php, php tips, php tips and tricks, tips and tricks, laratips, laravel, laravel tips, laravel 9 tutorial, laravel 9 advanced, laravel 9, laravel 9 advanced tutorial
Id: npbli0T9d4s
Channel Id: undefined
Length: 6min 43sec (403 seconds)
Published: Mon Dec 05 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.