Interfaces in Laravel: Practical Example of Exchange Rates

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys yesterday on this channel I demonstrated a quick Live Wire component application for exchange rates calculation if you haven't seen that one I will link that in the description below yesterday's video and today I want to emphasize one of its parts so for conversion of rates so when you click for example convert this currency to that currency that conversion is happening via external service which is actually here so in this example I want to demonstrate how interfaces work in laravel or in PHP in general why would you use that and how you could create your own implementation of the same interface so if we go to exchange rate contract we just click here and we'll land here on top and now we need to click again this is interface exchange rate interface is basically a set of rules for the methods and properties and everything so we would need to create a class that implements that interface to convert and get allowed currencies and and it is in the namespace of app Services contract contract is basically the same equivalent word to interface they are almost interchangeable and if we go into that folder app Services there's contract with the interface and then there are two implementation classes exchange rate host is actually what is being used in this repository implements that contract then has API URL for exchange rate dot host to actually perform the conversion and then implements those two methods that are defined in the rules so method convert and Method get allowed currencies for this video it doesn't matter that much what's happening inside of those methods but I will briefly run through that so from config we get how much to cash so cash remember the conversion results because they likely won't change for an hour or so then we call that conversion URL and if it is all successful then we return the result and then the other method of of get allowed currencies also cash also calling the external HTTP to get allowed currencies and that's basically it the implementation of that interface now in our Live Wire component which is actually like laravel controller it could be a larval controller if you wouldn't use Livewire but in this case it's Live Wire component which calls that service in This Way Mount exchange rate contract so mounting the interface and similarly convert this is the interface so how does laravel know that we need to use exchange rate host file this implementation of that interface so that binding is happening usually typically in app service provider so you can Define bind abstract interface to the concrete implementation of the class now why would you do that in this case it makes perfect sense because there may be other service provider to get the exchange rate it may be external provider it may be internal provider the same provides glider may have version 2 or something different API so in your applications if you feel there will be another implementation of the same class it could be email provider with a different provider other third-party tools may be exporting PDFs with different packages something like that so something that could be interchangeably reused then you could create interface with the rules and then everyone else that in the future would need to implement that would need to implement them by the rules that you have set so this is a strict way to make sure that your application would work anyway no matter the implementation in theory I guess the implementation could be of course bad or invalid but I guess the rules of what methods there are should be enforced and then in the same Services folder namespace there's dummy implementation of the same contract dummy implements and then convert and get allowed currencies so now in app service provide letter we can change that this interface could be implemented with dummy class from services and now if we refresh our page and select the currencies the result should be 90 I guess yes because it comes from different class from different implementation this one so this is great work by the author jigar here not only with creating that live wire component for exchange but perfect example for interfaces and contracts and why would you do that because quite often on Twitter especially I see quick tips from people that are promoting some architectural decisions but not really showcasing what would be the benefit of such structure in this case that dummy service which isn't actually used anywhere is provided as an example for future potential different implementations and if someone wants to implement something differently all they need to do is change this line in app service provider great example I will link the full repository in the description below so you can play around yourself and also if you want to learn more about interfaces in general in laravel among my courses I have one solid code in laravel it is a bit older from 2021 but solid principles are even older from 90s and this is the course where I showcase solid kind of inside laravel but also outside laravel because in my experience and in my understanding interfaces are more a PHP thing so if you need interfaces you kind of go outside of laravel skeleton and laravel way and start thinking architecturally more in terms of object oriented structure in PHP so that's solid code in laravel is kind of 70 of content is about PHP interfaces and how to use them within those solid principles and then a bit how it can be applied in laravel so if you're interested in that I will link the course as well in the description below that's it for this time and see you guys in other videos
Info
Channel: Laravel Daily
Views: 7,580
Rating: undefined out of 5
Keywords:
Id: 5cSTC3AG3tQ
Channel Id: undefined
Length: 5min 58sec (358 seconds)
Published: Wed May 10 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.