Mastering Laravel Action Classes: Organize Your Code and Boost Your Productivity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up guys today in this video I'm going to talk about a package called level actions it is a package which allows us to write our code in the form of action classes and then those action classes can be used to serve a controller or maybe a command that we have created and things like that so basically this package allows us to write code where the approach to our application does change for example I will show you a scenario where a user wants to change the password we will see how the same action class can be used in different scenarios to serve different requirements of the application and still we are keeping the business logic same so without wasting much time let's get started now obviously to get started the first thing that we will have to do is install this compose a package so I'll just copy this here and inside my terminal I'll just add this once this is done I will create a new action class for some reason this package doesn't allow me to create actions through a console it would have been a nice thing but doesn't matter actions now let me quickly stop the basic code and here we are the action class would ideally need a handle method and we can pass any parameters to this handle method for it to execute so why don't we expect that the handle method will get the user for which we want to change the password and the password itself with this done why don't we first log something wants to change password something like this now obviously for this let me see if I have anything inside my Cedar I don't so why don't I create a new user because this is almost clean laravel installation so I can do something like this and then if I run this command which is Mrs that is a shortcut for PHP Artisan migrate refresh and then seen it should give me some basic data and then I should be able to use this as an action class why don't we go to our web.php and try to do something like calling this function I'm passing user find 1 that is the dollar user that it is expecting and the new password itself now the first thing we will notice is it should fail and it does because it is saying that the Run function doesn't exist and the reason for this is this is a normal class for it to be used as an action we need this straight this in place it now doesn't complain about the Run function not being available because the trade does give us that okay it will have something inside it uh as an object it's make and run right so this is how it is doing that hence if I now go to my logs folder clear everything out and refresh I can see user amitab Roy wants to change password that basically means I was able to call this function just by running update password run and this thing fair enough this is kind of okay but we haven't made any major change in the way we could right it can be as similar to calling a service class then what is the big deal well that's where I'm going to show you certain features about this product package which is really the interesting part so let's just say I'll create a new route called user profile I'll need a controller as well so maybe PHP artisan make controller user controller and then add the class and maybe a method so show profile like so and then over here why don't I return a view and the view which I will create will be user profile Dot blade.php I have the code in place so I'll just add this in here user profile width I need to pass the data because I am printing out a few user properties okay this is going to be an array that's why it's complaining and now let me go to that particular URL user slash profile and it says route you update.profile is not defined yes my bad if I show you the blade file it has some basic you know welcome message things like that then it has a form which expects update profile to be a route because that's where the action post action is going to happen I have a csrf field I have the name email the password is being printed over here and the save button so why don't I quickly create that route as well it is a post route user profile save profile and the name of this route is going to be what exactly I have mentioned over here and let me make it update profile just to keep things similar okay now request and why don't we do a return redirect back straight forward now if I refresh I have this form and if I save right now nothing happens keep a look at this password what we will do is just for the sake of understanding how things work we will do user update password run something like this now if I refresh my bad come over here why don't we quickly do dollar user password equals bcrypt dollar password dollar user saved with a small s and this so if I hit save can you see the password is changing which basically means that the new password is being hashed and hence this value is changing well this is fine again the controller is calling the action method what's the big deal right well now the magic starts let's just say we have this but instead of this controller method we have a method called as controller we will get right as controller so we have this and what we will do is again these are hard-coded it's only for the sake of the demo you will not typically do this as in you will pass the user in a better way but for now this does work okay so this is done and why don't we also return dollar user now in here so far we have been using the controller but now instead of that what I can do is do update password class let's first see what is happening can you see this thing returned a response let me come over here maybe I'll do return redirect back and if I hit save can you see what just happened the password is changing again so how is this happening well this is the format of something called as you know a controller which has only one method which is the invoke method so this is an invokable controller kind of a thing which we can do and we just configured this route to use the action class as the controller to handle the request instead of an actual controller why is this happening obviously because we are using the as action trait and that as action trade has as controller method so it has this invoke thing and even middlewares so I can Define quite a few things for example my validation rules my middleware rules my um any kind of thing which a controller would typically do but certainly what is happening is the business logic of update password is in this handle function okay even I can do a private function and stuff like that whatever is possible inside a class and I am kind of giving away the responsibility of displaying this any anywhere on the application using this method so that's it definitely one very nice thing about the action classes because now I can have the management of the controller in the same class as well but there is also one more thing have you been in the situations where you have an app you have a web View and you also have a mobile app now you want that the update password when it is being requested through a browser does the same thing which has changed the password and then return the user back to the route where he came from versus when the API or when when the request for update password is coming from a mobile app password but after that let's just say it should send the user object back now this can typically happen where you can put a condition inside the controller but the action class allows you to very gracefully handle that and that is something which I'll show you so I have a method called HTML response what HTML response does is it will take whatever is being returned from the controller this as controller method so I'll just give you an example if I do return redirect back or maybe route redirect route user dot profile and I put the width thing over here why don't I simply actually yeah I don't need to do that I can just call the back function the only thing here is I don't return a response but I just return the user so now when it is an HTML response let's see what happens I click save something changes but if I do Json response let me do response Json and this is where the beauty comes in which is I do get whatever I am returning as the argument so I can do something like user dollar user and request dollar request so over here something like this I know this is a string instead of a Boolean but and now what will happen is let me open up rapid API so I have API slash this right what I will do is inside my api.php I'll copy this part where is my api.php so inside my api.php I have a route called API slash user profile that's correct and in here I am requesting the user update password class to handle that so now if I refresh okay it says there is an error let me see why there is an error route update profile no no that's not the one let's see invalid route action update password I think what has happened is the import hasn't happened Let Me Clear My log file once more and yeah I have received the Json response with the user object and the success true this is the response that I'm getting my spawns yes can you see I get that now what just happened I have a controller which is doing something when it is an HTML response something which is coming from the browser it does a redirect back but if I am making a request with an application Json right it understands that it needs to send a Json response as an API so when we created this thing what it will do is it will follow this method so your business logic is here your basic Constructor logic is here and then you are diverting your responses based on whether it is an HTML response or a Json response how cool is that and then although there are a lot of things one that is my personal favorite is that it can be used as a command as well so for example I have a public method called as command so let me import the command although I don't need no I I would need that to show it on the console so let's just say we have user again user dot find okay and I will obviously do something like this because I need to tell the console what it needs to do and then I'll have command info user changed the password obviously this is a command which we have created manually unlike the console commands which if they are inside the console folder you know they will be automatically picked up but because this is outside that folder we will need to Define that so protected commands is an array and then as expected I just import the class with this in place let me try and do PHP artisan okay I need to pass the signature as well otherwise how will it understand that so maybe there is a protected oh no it's a public string public string dollar signature equals users update profile just copy this and run it over here okay it's command signature right right I'll just copy this so that I don't make a mistake and now it did show me the username which basically means that this thing was executed change the password right so as you can see with this thing you know this action class is really a neat way of you writing code or the business logic once and then use the same class to literally take the responsibility of returning the different kinds of responses or behaviors that is being requested by the app now there are certain pros and cons to it but personally I have seen a lot of action classes being used in the laravel's 45 package and other places as well I really like this approach because the whole concept is that the application is performing in action everything every request at some point is doing something and so if the action in itself is independent enough our action class does make sense and if I have something which is repeating across multiple action classes then I can choose to create a service which helps me reducing the code and putting it in one particular place that's why I love this whole concept of action classes let me know what you think about it it's a debatable issue definitely some people may like it some may don't I am open to your suggestions your views about this package and yeah if you like this video then do click on the thumbs up icon and don't forget to subscribe to my channel
Info
Channel: Amitav Roy
Views: 2,632
Rating: undefined out of 5
Keywords: laravel, laravel tutorial, laravel actions, laravel actions pattern, laravel packages, laravel architecture, laravel action class, laravel actions tutorial, software architecture, laravel tutorial action classes, laravel development pattern, laravel package walkthrough, action classes laravel, laravel php, laravel project
Id: _TmvElGEBGc
Channel Id: undefined
Length: 19min 36sec (1176 seconds)
Published: Thu Feb 16 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.