CodeIgniter 4 Episode 2 Working with Models Views and Controllers in Codeigniter 4

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to another episode uh in this episode we're going to be looking at the model view and controller aspects of coordinator this is the second episode in the codignata series for beginners and uh if you haven't watched the first video uh you the link will be showing somewhere in the top right hand corner so please click on it watch it first before before watching this one now in this episode we're going to look at uh how to create a simple model how to create a simple controller and how to create a view and in this case we'll be simulating a simple sort of login in quotes login uh setup where we load the first page we click on the button it logs us in takes us with uh takes us to the backend we simulate how we get data from a model and then display that data on another interface to do this we will start so i have already set up my codeigniter folder framework there is a folder called sample which is what i'll be opening it up in today i'll be using sublime text so i will simply drag it in into sublime text then at the same time i'll make sure that uh this project is running by opening uh terminal the same folder if you're on a windows platform uh you can do this cd for change directory and navigate cd user's desktop then because mine is on my desktop then navigate to your folder the name of the folder in my case it's sample so this folder was created by having by downloading the zip file which i think is a pretty straightforward as i indicated last episode so in this case once i am in the folder i can do php spark sub which will take us to give us a url that you can access and just so that we are sure that it's working we can do localhost put 8080 and we should be seeing this same page here now we're going to do a number of things first as configuration so if in your project folder if you downloaded using composer you'll notice that we have a vendor folder and you will be missing our system folder here that is perfectly okay because they still serve the same purpose what i'll be more interested in here is this env file the env file so we're going to click on it select everything copy right click on sample we're getting a new file we'll paste it now we're going to save the same file however we'll save it with a dot env extension all right so those extensions are normally reserved for the system and that's where you'll see a warning and this allows us to set some configuration settings or have some configuration settings for our program itself or our software itself the purpose of this environment file and that's why we're having it as a system environment file is that when you are working as a team you can have a different environment file for the server for the live server one for the testing server with one for your development server as we'll be configuring and this file will have different configurations so in your database for your local environment you could have it as test and your live server could have a different database name um so you could have different credentials for purposes of security uh if you're using git for source versioning or code versioning then you may want to have separate files or separation environment files because as you're sending data on git then it's it's a bit insecure so the env files will help you in that in this environment files you'll notice a number of things um all of these configuration elements are no uh by default commented out by this bash sign so we're going to change to uncomment out by removing the shop sign or bash sign and set this as development so we wanted to set it to development so that we can be able to see the errors in our system this part from line 42 line 45 we will be using our database files then we will be uh working on this database site and uh you can actually configure this environment file to add different uh entities so for example if you're having if you want to integrate and pesa or any payment system then you could add a section for payment system and you could have the secret um the secret and the key yeah in this part so that will allow you to have those configurations if you are doing a mailing server or if you have access to a mailing server then you could also have it here now for our env file so we we are more or less done with our env file now in the app when you open the app itself here and a sample app you notice a number of configuration elements we have configurations we have controllers so we'll be interested in writing the controllers uh we have models so for now we don't have any models and we have views okay we have one view now we will be creating a number of files uh in the views uh we'll also be creating a number of files in the cont one file in the controller and also be creating one model so that we can be able to have a sort of simulated complete system now we're not going to be touching anything to do with the routing routing we'll look at it next time in the next episode but for now we want to look at how do we create a new controller how do we set the values in the controller to load a new page and how do we configure this to make sure everything is working correctly so to do this we are going to create a new controller so an easy way of creating a new controller is to simply copy what is there what is there in home then right clicking on controllers create a new file now please make sure that you have to be very careful as the names are very sensitive so if you have it in our case we'll be testing an auth controller for login logout management and user management essentially so in this case we will be having uh the first letter of the controller the class itself being uppercase and the other letter is being in lowercase here so i'm going to change this to oauth auth and i'm going to save it as auth dot php right so this gives us our first controller now we will be uh our first file will be the login so we'll our index file will allow for us the login page so we'll get a few stamp files so public function uh process login so this will handle the backend for the login we'll also have a public function home page which will will handle our home page and and we can we can simulate one for register public function register i am using a tool called top nine photo completion uh if you will leave the link below if you are having an educational account then you could get a free free account for that we have a public so i'm just getting the functions to load the page and handle the pager process and registration so in this case our index file our index will be one that will handle our login so this will be the same as login here we will be handling handle the login back end functionality our home page load the user home page we will be expanding this in the later later videos to make sure that we have the different users being taken to different home pages register loads the register page and process registration handle the registration backend functionality now we'll be creating very rudimentary pages an interesting thing about uh so once we have the mod of the controller we want to go to the views so in our case when when you're developing a system it's always good to have what you call modularization so in this case i'll create a new folder sorry i wanted to create a new folder a new folder and i'll call it auth because it's handling the authentication and inside it i will create two files three files actually so i'll have login.php i will have homepage.php and i will have register php now i'll make sure the files are valid so i'll say here we have the title being login page and i can have a heading one element in login just simply saying that this is the login page the same connect can be for registered register so that i have this register this is the registration page so what i've done i've just copied whatever we had in login and pasted it in register and i can do the same for home page and i simply say this is the user's home page now we we want to try and see whether or not we can see when i load auth slash index or just simply auth in the url uh so localhost full full colon 8080 forward slash auth which is the controller here uh if we can be able to get the value what we are we are trying to see here now we so here what i'll do i'll give the folder name remember it's instead of folding the views so i'm running the view inside auth folder and i'm calling it login.php right so we can now go to the browser so if i say that slash oath it should simply take you to and you can see the same as saying of index yeah so this is the login page all right so we in the in the home page we can do the same so we can return the view uh oath on page dot php and in the register we can also do the same we can return so just make sure that the files do exist inside the folder both register.php so again make sure that if you're not doing the processing then you have it in a single singular form now in the auth in the auth controller we can see that we have the default which is the index we have the process login we have the home page you have the register you have the process registration now what we will be doing here is that we will create uh we'll be creating we'll be creating a link from the login page so when i click on the login page i can i want us to simulate by having a link when i click on it it takes me all the way to it simply calls uh process login backend then here we will also simulate the model so we'll pretend we have a model that has some data and once we pretend that we have a model that has some data we will get some data from that model and use that data in the home page itself so let's do that in the login we can create an anchor tag [Music] we're going to create an anchor tag and in this case we're going to use and say this is a process login here we are going to be using the site url site url and we will be calling we will be calling so this is essentially the site url we will be calling the oauth controller so we'll say auth and what functioning author we're calling process login so we want this to be to be the path so process login so let's see process login let's see let's see this has to be an issue with the code um echo site url yeah i haven't closed the php that's fine so make sure that your php tags you've opened it here you have a php opening snippet and a closing snippet then you can echo the site url and you see that however this will still not work uh we want to be able to use the url and therefore we need to load it as as a helper help is simply a file that allows us to do some advanced functionality so in this case we will go to base controller and you will see that we have uh in the base controller which the home controller and the earth control are boring from or inheriting from you can see that we have helpers here and here protected helpers and i'm going to simply have it as url okay so just to make sure that this function is running we have had we have added a link in the login.php page view we are sending some data and this later on we'll replace this with a form that we'll be sending with the username and password and we'll also do [Music] we'll also convert this into an ajax request where we'll send the data from this form all the way to the backend this we'll call process login and this process login here will handle some some some items so firstly let's just echo works right so if i reload the page and i click on process login you can see that process login works so that means that we are able to reach to this part now once we are here we want to be able to do something what essentially we'll do in a typical scenario is that we will query the data uh they will have we'll capture the username and password from the form and we'll create the database and in this case we want to simulate that so we'll assume that we have a user username and password and our database our model essentially has some data that will give us that result so in the models folder right click and create a new file call it user model dot php now it's always important to ensure that you have the data in the name the name of the file exactly as the class name would be so here we are creating an opening php tag and we're declaring that uh will be the namespace when the namespace up models so in this case the app models is the app folder here and we're inside the models and we will be creating so here we will say use codeigniter model so coordinator gives you access to a model and in this case we're going to create a class called user model and this will extend model so here we will be creating our uh here we'll be defining a way of inserting network database and getting data from database a model's task is primarily to get data from a database and uh insert data from uh to a database so we're inserting data and getting data from our database so that is the task of this model but because we don't have any database introduction already what we will do is we are going to simply create a mock data database and in this case we're going to have a function called get one user so we'll get a function public function get one user which we're going to create an array so i will get an array called user user well and i'm going to say well i can say user details this goes to an array that has let's call him tom peter and we're going to return user details so when someone calls this function they're going to get user details now here in the controller here we're going to create a new model so we'll say user model is equals to new [Music] user model and because we'll be using this we want to be able to allow this controller to have access to the model itself so how do we do that we'll say use up models as we're saying in the folder we want user model so we're essentially giving it make sure this is an uppercase giving it access to the folder all right so to that particular file so that we can now create an instance of that class so first we want to see if we can get the details so we can say user details is equals to user model and we're going to be calling get one user so in this case we can echo this just to see what what is being displayed and when i reload this page you can see that we have an array that has tom and peter so the first name is tormund the second name is peter which means that essentially when we clicked on that [Music] that link on the index of in the login.php file it took us all the way to this function we have loaded the model from here it has taken us to this model we have gotten data and later on as i mentioned you're going to get this data from a database we can pass some parameters so maybe username and password we get an sql segment to run return the data if it's empty if it's not empty then we can use it for something else now what we want to do here is we want to now once we have this process process login we want to be able to to create this in a session so we want to be able to use what you call sessions to uh a session essentially is um a connection in the server side that allows us to have uh active or get some data that are moved from file to file so in this case we want to move this data from oauth controller to homepage.php now but before we do that because we know up to this point we're getting the data we want to be able to redirect this to the home page how do you do that now codeigniter has a very nice function for you so we can use the function direct and we can do what you call function chaining so when when we are chaining we are saying that you do a redirect and we are changing this function to another function so where are we changing it to we are going to have both and we are calling page so we are calling the function homepage itself here so if we reload this let's see oh yeah we should return we should return a redirect so make sure that we are returning and coding nato will always returning values and you can see that it has taken us to the home page now what you want to do is these details that are here you want to be able to send it to the home page such that here we can say welcome tom peter all right and i think that would be what we will do for today so what i can do is we can call an inbuilt function so collinator has another inbuilt function so we can say my session session is a variable that we're just defining and whenever you call session session is an inbuilt function that is global in nature it allows you to access the session variables i am going to create another a session so we're going to say session and i'm going to set now this set function accepts two parameters the first one is the name of the session which in our case we're going to say uh user details and the second one is in my case the array so i'm going to copy this array and put it here this global function is the same way as saying session start and search so it allows us to be able to [Music] to create from the services a session that we're able to do session start and session stop right so in this case um i mean oath in this case we can set the session user details and we're having it as user details so let's go back make sure we're in index process login it will take us to home page now in this home page we want to be able to capture that session so remember we are working with php so we can create a php snippet here and we can simply say that we want to first see what data is in the session so again we're going to say session is equals to we're calling the global function session then we can use a session get function so i can say here user details is equals to session and i'm going to use a functional helper function called get and what am i getting user details um i don't know why i'm getting some we had errors and let's see this is a php file on page.php perhaps let me just close it and reopen it to see why it's misbehaving yes this should be fine now uh sometimes you need to just refresh the element or the cache so again i'm going to echo just to see uh it's always good to make sure that you're getting the data and in this case i'm going to show the user details and stop execution on that point so if i reload this page you can see that we have an array that has tom and beta and now because i have tom and peter and you can see that index zero index one i can simply say um first name is equals to user details at index zero and last last name is equals to index 1 and therefore i can say full name is equals to first name i'm going to change it to um to concatenate it with a space and i want to add the last name and therefore i can say um like an echo welcome full name and in this case because we are using the double quotation marks then this variable here will be expanded so if we do this you can see that we have welcome tom peta all right so if i do process login it takes me to the login page so that is essentially how you can you can do a simple mvc approach of course in the next episode we'll be breaking down this authentication and having a template file where we'll now have logouts and such but for now we have created an index file that takes us to auth login.php in the login.php when we click this link which is called process login it takes us to oauth process login so auth controller here has a function called process login which like we mentioned we are using user model from the app models so up this namespace here is to allow you to be able to access the files from app models in this case we're creating an instance of a model class that we have defined in app models called user model which extends a model and needs to have a function that uh get one user which returns details about one user a simple array that we have simulated here then from that we are creating a session uh we are setting the user details as that and we are redirecting this page to earth home page now essentially calling this function so which loads for us the home page for this user of course in future we'll expand it and this home page allows us to we are displaying that this is a user's homepage you are getting from the session array the details which we set as user details and we're getting the first name the last name creating a full name and simply echoing out the full name so that's it for this episode uh it's a bit longer than expected but i hope that you have been able to set set up the model the view and the control and understand how the three interrelate if you enjoyed this video please leave a like you can leave a comment you can share with your friends you can subscribe so that you can get notifications on the next episode when i release it until next time have a good time see you then bye
Info
Channel: Unhinged GT
Views: 396
Rating: undefined out of 5
Keywords: Codeigniter 4, Tutorials, PHP
Id: IzXBcb9__jM
Channel Id: undefined
Length: 29min 32sec (1772 seconds)
Published: Sun Nov 07 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.