Controllers for Beginners | Introduction to Controllers | Full Laravel 9 Course | Learn Laravel

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up developers and welcome back to a new video where we will dive into controllers in larval if you want to help the channel out with the content i create now head down to patreon where you've got beneficiaries as a private discord group where other developers can help you out with your coding issues if you are interested to join the link to patreon can be found in the description down below if you're familiar with mvc you know that the controller has a couple key functionalities the first one is the fact that it accepts http requests from the user interface think about forms that needs to be submitted they can also get the right data from the database think about user authentication a user enters credentials inside input fields on the user interface while a request will be done to check whether those credentials exist or not it also handles input validation to check whether an input field where numbers needs to be added are actual numbers at the end of all those functionalities it usually sends back a response to the user in larval it will pretty much work in the same exact way the end user interacts with the controller by sending an http request from the front end then the controller responds to that request by either persisting data to the database or retrieving data from the module once the data has been retrieved or persisted the controller will send back data to the view which will then be visible for the end user larval comes with a pretty cool tool called artisan which can be used to run migrations create users database records and a lot of other stuff we don't need to create controllers manually anymore since we can use artisan to create controllers for us so let's test it out let's navigate to the cli now let me write down clear alright and if we perform the php artisan command right here you'll find a complete list of commands that you can perform inside the cli i'm not going to cover all these individually but we will be using a lot of them throughout this course if we scroll up to the make section right here you'll find all php classes that you can create through artisan the one command that we need is the make colon control command right here which will create a new controller class for us the left-hand side is the action that we need to perform so make call and controller while the right-hand side is the description so it will create a new controller class for us so let's do that inside the cli let's say php artisan then we need to say what we want to do we want to make colon a controller space before we hit enter we got to make sure that we add our controller name keep in mind that this will also be the class name and the file name in our case let's call it posts controller let's hit enter as you could see artisan responded back with a message saying that the controller has been created successfully if we navigate back to visual studio codes controllers are stored inside the app folder http folder controllers folder where you will find the postcontroller.php file right here you will see a default php structure with the opening tag it has a namespace and a use statement right here which is the illuminate http request new statements are always defined above the class what you're basically saying inside the use statement as well we've got our request class copy everything inside the class so we can use it inside the controller class at the moment you'll see that our use statement has a lighter color than our namespace and our class right here this means that it has been pulled in but it's currently not being used finally we got our class called post controller which is extending the default controller as you can see right here called controller we have complete freedom to do whatever we want inside the controller but in most cases you will be creating methods right here which will then return something back this can be a string view or whatever you want so let's define one let's say public function index and a quick note we're adding an axis modifier of public right here since we're going to use this class inside the web.php file to output it inside the browser therefore we can't use a protected or private use statement since those are only accessible inside our postcontroller class so let's return something inside our index method let's say return a simple string of this is the index method inside the posts controller keep in mind that you need to close it off with a semicolon this does not magically show us the return statement inside the controller what we need to do is creating a route that knows where to look for our index method this can be done inside the rouse.php folder where we have a web.php file let's open it larval defines a default route for us and i actually don't want to spend too much time going over it so let's just remove it since there is a better way of defining a route whenever you want to create a new route you need to start off with a route facade so let's write down route is already pulled in as you could see where is it right here then we need to say colon colon followed with the http method we're trying to perform in our case we simply want to output something to the browser which will be the get method now the get method accepts two parameters the first one will be a string of our endpoint so what our endpoint should be is a forward slash block right outside of our route let's add a comma and let's define a set of brackets since we're going to pass in two values right here the first one will be the associated controller with our forward slash block endpoint in our case it will be post controller keep in mind that we need to pull in the use statement otherwise it won't work so let's hit enter it is a class so in order to make this work we need to add a double colon class then inside our array we need to add a comma because we have a second value which will be the method that we're trying to reach inside our post controller now in our case in single quotes it will be index let's save it let's navigate back to the browser let's change our endpoint to forward slash block and as you could see the index method inside the post controller has been printed out let me actually zoom in alright now the term crud is very well used when it comes to larval if we think about a basic block that uses squad functionalities we have the opportunity to first create a new block we can read information of one single blog post we can update a specific blog post and we can also delete a blog post those are the four primary operations that web applications most commonly provide on a controller those four crud operations do need to have methods inside the controller larval has some conventions for all of those routes of a traditional clutch controller which is called a resource controller it also allows you to generate one out of the box with all methods defined inside of it so what we can do is navigate back to visual studio code let's delete our post controller for a second move it to the trash all right what we then can do is navigate back to the cli and inside the cli hit the arrow up because we're simply going to perform the same command but we're going to add a new flag to it so let's say space double dash and the flag is called resource let's hit enter our controller has been created successfully so let's navigate back to visual studio code and let's open our post controller and as you could see our controller has been pre-filled with some methods and let me show you what all those methods should be doing when we're talking about our block example so the index method should show all available posts so every single one that we have inside of our database then we have our create method which obviously creates a new host we have our store method and the store values come from the http request as you can see as a parameter that's why you're seeing the request object as an argument then we have the show method which will show one post based on the id as you could see once again inside the parameter then once a user submits the edit page it will be handled inside the update method which will grab the request and it will update one specific post finally there is a destroy method which will delete one row inside the database based on a given id now let's navigate back to the index method at the top right here let's remove the comment and let's quickly return a string of index of block method if we save it and navigate back to the browser refresh the endpoint you'll see that index of block method has been printed out but this does not work for all methods we got defined there is a command that i've showed you before that you can perform inside the cli to show all routes available in your project so inside the cli let's perform the php artisan route column list command don't worry about all those rides that you're seeing right now the one that we need is this one which is a get method to our blog which is using the post controller at index so the index method inside the post controller but we're missing all those other methods that we should have right here so should we add them all manually inside the web.php file now there is actually a trick in larval to add all those methods right here so what we can do is basically defining a new route so let's say route column colon instead of giving it a cat method we can use the resource method right here now the first parameter will be the same as before so block and keep in mind that the forward slash is actually optional i always forget saying that so let's just try it out without one now then right after our endpoint let's add a comma instead of defining an array we're simply going to call the post controller column colon class which will grab all methods that we have to find inside the post controller and we'll chain it at the block endpoint so let's comment down to the first route that we had save it navigate back to item hit the arrow up and run the route list command one more time now right here you will see that we have the index store create show update destroy and edit endpoints defined right after our forward slash block endpoint before i wrap up the video i want to show you how you can easily return a view back to the browser at the moment let's open the post controller we're returning a simple string which is something that you will probably never do again so let's remove the string and what we can do right here is exactly the same as what we saw in the default route that larval added inside the web.php file is returning the view method now inside the view method we need to make sure that we return a string of the path we haven't defined the view yet for our forward slash block endpoint so let's open the resource folder views folder and let's create a new folder called blog then inside the blog folder create a new file called index.blade.php now let's write down h1 here and hit tab and let's add a text of index method of blocks then let's navigate back to the post controller one more time the view method knows that it needs to look inside the resources folder than inside the views folder but it doesn't know that it needs to go inside a block folder so first let's define block right here and you already see the option instead of adding a forward slash to access the index.blade.pc file we need to add a punctuation mark followed with the first path of our view there is no need to add the dotblade.php file extension all we need to do is say well get me the block folder dot index file if we save it and navigate back to the browser refresh our endpoint you'll see that the index method of blocks has been printed out this was it for this video where i showed you the basics of controllers in larval in the next video we will be staying on this topic because we're going to dive into single action controllers if you do like my content and you want to see more leave this video a thumbs up and if you're new to this channel please hit that subscribe button you
Info
Channel: Code With Dary
Views: 17,756
Rating: undefined out of 5
Keywords: laravel, laravel 9, laravel php framework tutorial - full course for beginners 2020, laravel 9 tutorial for beginners, laravel php framework tutorial full course for beginners, learn laravel for beginners, learn laravel step by step, laravel full course, php laravel youtube, laravel tutorial youtube, how to learn laravel, laravel tutorial 2022 - the complete developer course, complete laravel 8 course, learn laravel 9, controllers in laravel, controllers for beginners
Id: Aoqj5nuwBQI
Channel Id: undefined
Length: 12min 59sec (779 seconds)
Published: Mon Mar 14 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.