Laravel Secrets - Bobby Bouwmann

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
do you want your online store to be ultra fast fully customized to your needs and scalable to more than 1 billion items I'm EOS is an ultra-fast PHP e-commerce framework I'm EOS next level ecommerce try it now [Music] [Music] you if you're running an application at scale in public or private clouds you have to tame almost boundless complexity data dog brings together all this observability data with infrastructure metrics traces and logs in one integrated platform footage is a cloud communication platform that lets you integrate voice video and messaging into your applications we have a load of helper libraries including a laravel package and a client library for PHP you can find out more about us at developer next mo comm [Music] [Applause] [Music] hi I'm Bobby Bama it's so awesome we can still have a lark only you this way so you can find me on github and our cast and Twitter by using this handle and I've been contributing for the past five years to Lehrer cast by answering questions on the forum I'm one of the top contributors there and I really enjoyed doing that so if you want to learn more about laravel or get your questions answered you should go there I work at a company called n rise where we build high quality internet applications and I'm part of team artisans where we mostly develop API syn level which are connected to mobile apps built in react native and you can guess who came up with the name let's talk Larry for sequence so let's dive into the editor so Larry fo has a built in exception handler this course is very powerful because it handles our exceptions for us so it either make sure we get a whoops page or we get a nice error message or a 404 or we get the stack trace if we're in local development if we go to this clause itself in here we have the exception handler so this is the base class that we use and in here we have the report and a render method so let's create our own exception so PHP artisan make exception and let's call this not all bills paid exception what this has been created now if we're going in here we can see that we have this clause now and it's just an empty class which extend the default exception from PHP so if we go to our rep routes we can create an exception here so let's just draw an exception here so we can say true new not all bills weight exception so whenever we draw this exception we can see what kind of ever we get in a browser so let's go to the browser and in here if I go to the new route we credit so we call it exception we get this nice error exception and it's very clear what the exception was and we can provide a message to it if you want to but in this case it just says this exception has been true so this is being handled by the render method in this case because it sees an exception and it knows okay we need to do something with this so let's render it in a browser we also have a payment route available so if I'm going to payment you can see that there is a payment page and this one does nothing at this point because we have this custom exception we can handle it different ways so in here in our handler Clause we can say we want to do something else while rendering this exception so in this case we can say if the exception is an instance of not all bills paid exception if that is the case what action do we want to perform so this case we say return redirect and we redirect to the payment route redirect payment so that's the right way we want to go so if I'm going back to the route here and if I go to the exception Clause the exception route and I press Enter I'm reader I'm being redirected to the payment page so this works all fine but what happens if you have ten of these exceptions or you have 20 different exceptions and they all have a different case well in this case this will get really messy if you need to duplicate all of this all the time so laravel has something really cool built in and that is the render method on the exception clause itself and the same goes for the report method we can do for the reporting stuff the same thing so the random effort is for displaying the exception in the browser or in your API so that can be the JSON response that belongs to that exception and the report method is about what you want to do with the exception so do you want to write it to your log files or if you want to write it to an external party like century or flare so let's go to this exception clause and let's our let's add our method here so we can say here a public function render in here we can do whatever we want in here so in this case I can say return redirect to a certain route which is payment so we enter the same functionality and let's make this final so in that case we can remove this so this is not needed anymore and as you can see we'll see that this will do the same thing so if I'm going to exception I'm being redirected to payment again so if I remove this method again and we go to exception again we get the exception so that work this works the same as defining it in your handler method and as I said before it's really important to decide if you want to do this in your exception Clause be specific in your exception clause or in your handler clause and one benefit of using it in your exception clause is also that you are removing all this logic from one place I'm moving it to the explicit exception clause and in here we can also define another method called report and you can do in here whatever you want so write to a log sentry maybe or even flair so you can do whatever you want in here and this probably shouldn't return anything so we can clean this up again and now we have the same action working out here so if I'm diving into the render method itself we can see that lera file has a lot of logic in here but the first thing is it checks if the exception itself has a render method and then it will convert that render method to a response and you can also implement the responsible interface so in here instead of defining all of this let's just try that we can say implement implements responsible interface and this method requires us requires us to add the two response method and in here we get a request and we can return something in here in this case it should also be a redirect so let's see if we do this one we return to redirect route again and to the payment so if I'm going now to slash exception or B redirect it again so this works the same so there are different ways of doing this and just depends on your needs on how you want to do this the interface itself helps you to enforce that you have this method available so the render method isn't required but the to response method in this case is required because of the interface so if you have a lot of exceptions that are that look alike but they have a different render method then the responsible interfaces can be really useful here so let's go back to the narrow file handler and in here you can also see that there are other exception classes being catched by laravel and they render in a certain way for example the validation exception is being rendered in a different way where it let's see we can click through here to invalid where it always returns you to the previous page with the input and with errors so that's why it always works that if you submit a form and you're being redirected back to the previous page that you can see the previous input using the old method because it's available because the exception Clause helps in this case to add this data to to the redirect let's go to the next sequence so I'm just going to clean up here I'm just gonna check out the other branch we call it validation exception and I just need to make sure that everything is has been cleaned here it's fresh and see it again so I'm just gonna close all of this up ok and we're ready to go so validation exceptions we already showed you just a minute ago that the validation exception itself is being caught by larvae however the validation exception itself can be really useful because whenever your form input fails it will return back to the previous page and also return the messages that has been sent along so it gives you the translations of these messages for example if you have a username which is required and needs to be at least 6 characters if you fail five characters you get an array back with data with the username as key and then there's an error message which as you need to delete six characters this function is really useful and we can use this validation inside our controllers but sometimes you need something more expressive or you need to check something in a different way and that's where the validation exception comes in so let's open up this post controller that I created post controller and in here we have just a default controller we can create a post we have the storm effort we can create a new post and in here we have the validation inside the storm effort and whenever this validation fails it will throw and validation exception redirect you back to the previous page and fill in the data for you automatically so this is just the basic stuff and we have different ways to validate so we can also use a post request post request that's also possible then we don't need to do this anymore and then we can just say request oh so this works the same way so either we do the validation inside the post request clause as you can see here we have the rules here or we do them in the controller itself so these are some basic ways to validate a request or in this case creating a post however sometimes you need something more expressive in our case we have post and we can publish them so there is a publish publish post controller and this method has a show method so we can show the post itself and publish it we have a store method to actually publish the post and this will just publish the post for us and redirect us back to the previous page now if I'm going to this route so there's a route available for this post so let me just go to the browser and open up this publish page so we have published slash the ID of the post so we have one post called larrikin EU becomes Lehrer come online and there's this big button to publish it and if we press it it will be published however we want to add some extra validation which cannot be done in the normal request so normally we would check on certain things in this case you want to add something else so in this case we can for example say if the post is not reviewed if that is the case then we want to throw a validation exception so this is a laravel exception and in here we can say with messages and we can provide an array with a key value array where we can say what what the problem is so for example we can say the published add field so that's just a name we gave it published ad and then we can say message or the post needs to be reviewed before it can be published I see ok so that's it so this is some extra validation we can do another place and that's why we use this validation exception and normally I would use this this approach whenever I'm using an extra API call to calculate something well we don't create an custom rule to do that so that's an alternative you can create custom rules to check these things as well in our case this is a really specific case only for publishing the post so creating a rule for it for itself is also an option what we choose to do it this way and another benefit of this is that we don't have to actually post to this field to validate it because there's no within the day there's no fields posted here there's nothing posted here we just say hey there's a validation exception you can continue and draw this exception and the lower one handler will ultimately get exception and return you back to the previous page so if I'm going back to the page and I'll press this button we get the error message here as expected right because the post is in refuge yet that database column hasn't been set so now we get this error message so if I'm going to the publish page publish the plate of peach pee and in here we have some logic so if the post is published we get the option to publish it so there's this small form that just post to this form and nothing else there's no fields posted here so one important thing here is that we to display the error message we check if the published add field is available in the errors array it's really important that we check on this field because if we don't check on this field we don't see the error message so whatever field we define in here will also be available in here as one of the validation errors in this case that's just a message we show so it's really important to always catch the exception in the front end if you use this approach to do some extra validation in your controllers and note that you can perform this action anywhere you're in a surface class or in a different place where you are actually handling the requests or doing something with it so nothing jabs but handling the requests somewhere else you can use this approach to throw a custom exception in this case the validation exception provide the messages and you will be readable redirected back to the previous page or it will be converted to a JSON response automatically for you I hope that's an approach you can use in the future let's just go to the next secret so let me let me clean everything up now we're going to check out the next one so and let's run PHP artisan migrate fresh seed okay awesome so models in larva are really cool so a model is a cluster that represents a table row in the database so each field that's available in your table is also available in your model so let me just open up the one of the dumps of the user so in here we have a user object and as you can see all these fields are available inside the database and they are just the regular values that that are showed in the database as well so the birthday field in this case is just a daytime field in the database the phone field is a text string in a database and so on and then we have the array presentation of this object and this one uses already different syntax for the values as you can see here the dates itself they have a different format there's now an extra T in there and there's the milliseconds and time cell and as you can see here for the birthday column which is here a date here it's an integer with the age of the user so if I'm going to the user model we can see here that we have different attributes here so we have all the fillable fields we have to cross array so the email very right and the birthday are being cost to date time objects and then we have this accessor so there's this syntax get field attribute so in this case birthday where we will parse the days and return the H as an integer we have the phone relationship here and we have the phone attribute here so as you can see there's already something happening here because our user model returns a string here it's just a string but we also have a relationship phone created and this is not being returned we only get this phone field back so this is the mystery we're going to figure out today so if I'm opening up the user page in here we have just a user object and if I'm just doing the user birthday here so let me just echo that out and i refresh here we just get the number back because that's the accessor behind it and as you can see we also have a cost available for this one and we have an attribute here available it is returning the H here and not the daytime so what's going on there so every model in nervo extends the base model clause so in this case Larry flow extends here authenticated wool for some extra stuff that a user needs so if we're going in here we can see that even this clause also extends the base model clause and this is the one we want and this base model Clause makes all these models really accessible and adds all these awesome features and as you can see here there's a bunch of features you can see already but just looking at the trade names has attributes events relationships time stamps that's all in there so the method we're looking for is the get method and the get method is a static PHP method that can be used to access any property on your object so let's go there to get method so whenever we call for example user phone it will pass this phone as the key here so this will be phone and then it will be passed to the next method and so that also goes for the other one so if I do birthday 10 birthday will be passed and so on so if you have a relationship for example usually has posts if you just do post then post will be put into here and then that will return the relationship eventually and all the magic is in this get attribute method which lives in the has attributes trait in here we can already see how laravel dusties so it checks if the attributes exist on the model then it checks for the get mutator and in level length we call at them an accessor and then it checks if it's Costabile or not so data checks if it's in cosmo array or if there's a possible Clause associated with this attribute and if that's the case then we return that value if no attribute exists or no cost exists or there is no accessor then we simply move to the next part where we determine if it's a method and then we do we don't do anything and if that's not the case then we look for the relationship so then it will either do one too many or one to one and many to many and so on and in here we can see the order so in the get attribute value method we can see how the model will return the value so in here we have this transform model value method and in here we grab the value from the attributes array that's on our model so if we would put in here so so if we put in here phone then phone will be retrieved from the model that value will be used and passed to the transform model value method and this method decides the order so first it checks for a mutator then it checks for a cost and eventually it checks for the daytime and otherwise it just returns the value so in here we can already see what the actual order is so accessors always go first then cost then the attribute itself and if none of these attributes exist then the relationship is tried and if none of those exist then you probably get an exception because PHP can't find the property so we're going back to our user model and as you can see in here we have this birthday cost and we have this accessor so this accessor always goes first so if we remove this accessor and we can see that the birthday now is being transformed to a day time instead so if i refresh here we can see that the birthday is being returned to a day time now and as you can see here as well so that's where we echo out the birthday itself and the same goes for the phone field so let's say we have we've previously had the phone number stored on a user and now we decided that we want to make this a separate database our database table and move this to a relationship so if we don't remove the phone number from the user object itself so there the attribute still exists then the relationship will never work because the attributes always goes for before any relationships so it will work if you just call the phone so let's say if we do user phone and let me just remove all of these here and let's do a dump so we can just dump out what this will return so if we directly ask for the relationship it will work but if we just directly ask for the phone just as a property and won't work so if i refresh here you can see here that we get the number here so that's the number stored on the user model and in here we get the relationship so it actually works there actually is a relationship working here but only if we use the method directly but I said we just want to use phone to retreat the relationship so we need to remove this column from the database let's do a PP aartsen make migration delete users table so this has been created now and in here we can say table drop column and in here we pass the phone and let's not do anything is down methods okay so please be artisan migrate so the column has been removed if I now refresh here we can see now we get the phone relationship back so now we get the phone relationship it has a user ID and it has a number so this works completely differently from using an accessor or a relationship so it's important to know this order so if something is wrong with your application are you expecting weird behavior understanding this approach is really important to prevent those issues so the order is always accessor cost attribute relationship let's go to the next secret so so I went ahead and did all the changes for you so you don't have to watch it for me okay let's open up the event search provider so the event search partner and this is the heart in larvae or for event and listeners so whenever you have an event and you want to perform an extra action for it it's gonna either be cute or you just want to perform extra actions but you don't want to define all of them in your controllers or in your serfs closest that is this is the perfect place to define them and therefore automatically comes with the first event listener so that's the registered clause and the send e-mail verification notification so this is the perfect example of doing an event and listener so whenever someone is registered we want to send them an email and we can either cue them queue the notification clause or not that just depends on you however level itself also fires a lot of different events while using the application so we're not going over all of them just a few of them so don't worry we won't be sitting here for the next two hours so the first one is so let me just show you a few of them so for example whenever you log in into the application level will fire this login event and by default therefore itself doesn't fire any list for that and for most of these events level doesn't fire any listeners they're mostly there for other developers that want to hook into these actions inside the application so for example if you're using the cash a lot and whenever you write something or put something in the cash an event is being triggered which is called key written and whenever you forget a key from the cash there's this event called key forgotten so the same goes for sending emails or when everyone and evil has been sent there's there are events for that if you have a scheduler running and the event has started there's an event for that or if there is a task finished in the scheduler there's an event for that and the list goes on and on for those all those cool things one thing I won't show you today are all the events that are triggered within larvae around models so there's a lot of a model events as well when creating them or deleting them or telling them but that can use its own separate talk I just want to finish up with the command events so larvae also has these events whenever you start a command or you finish it you can hook into that so in this case whenever a commander starts I want to add the output I want to add a logo output to this command and after and whenever it's finished I want to add a finish message to it as well so how does this work so laravel fires this event for us whenever we start the command and then we have this clause so in here we have a handle method so installation has its handle method and we get the actual event so the command starting event and we can add something to the output so the event has the output and we can write something to the output and it will be added before the command execute so let me just turn off this one and then I can show you how this works so in here if I type PHP artisan version you can see that we now echo out the logo and then we added that equals the level framework number and this works now for all the commands so if I do few clear it just works or even pitch charts and migraines nothing to migrate we always show the logo so this is a really bold logo but you can at like a company name or another message so let's clear this and at the other message as well so we're going to add this one as well and if we're looking into this one it has a handle method method as well and it gets a random message and writes it to the line as well so this will be so in here we have the output as well and whenever we write a line we will append it to the already current output that's available so whenever the command started will be fired then we write something to the output which will be given to the command itself which will write something to the output and then in the finished command we get the output that's already been generated with from before and we just write something to that one so let's perform another query here so let's repeat UART inversion and now we get an extra message here thank you for using laravel or another message and right now we're doing boring stuff right we're just echoing out something and it's pure fun to do it but there's also other things you can do with it if for example you have a command that has been finished so in here we have the event we can get the command string as well and in here we can do some checks if it's this command then I want to write something to slack or if it's this command I want to perform another query or another action so this way it's really powerful and you can put all this logic in just one or two listeners and you just have to find to find it here and there's no extra logic needed in your commands itself so I hope that was really interesting for you and that you learned something new today definitely go dive into the framework and see if you can find other cool stuff that's going on in the background my buddy Steve and I are writing a book called laravel secrets and it contains a ton of these things and so we're still working on it a lot of work but we're already making some progress if you want to learn more about saz well you can sign up at Lera for secrets calm thank you for listening I'll see you next time you do you want your online store to be ultra fast fully customized to your needs and scalable to more than 1 billion items I'm EOS is an ultra-fast PHP e-commerce framework I'm EOS next level ecommerce try it now [Music] [Music] you you if you're running an application at scale in public or private clouds you have to tame almost boundless complexity data dog brings together all this observability data with infrastructure metrics traces and logs in one integrated platform footage is a cloud communication platform that lets you integrate voice video and messaging into your applications we have a load of helper libraries including a laravel package and a client library for PHP you can find out more about us at developer next mo comm [Music] [Applause] [Music]
Info
Channel: Laracon EU
Views: 10,028
Rating: undefined out of 5
Keywords: laracon, laravel, php, laraconeu, tips and tricks
Id: QSUGFJYMPvI
Channel Id: undefined
Length: 33min 50sec (2030 seconds)
Published: Mon Jul 20 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.