Transcript for:
Understanding Controllers in CodeIgniter

hello everyone this is Alex from Alex Lansford comm and in our previous tutorial we went through folder structure of coding metaphor project in this tutorial I would like to go through controllers so let's waste no time and I will turn to my screen and I have opened the first CI for project that we have created in our previous videos so let's open up folder and let's go to our controllers so first of all let's start with the definition that code igniter gives to the controllers so CodeIgniter edits official page states that a controller is simply a class file the that is named in a way that it can be associated with the URI simply what did what they mean by that is that you can see here in our app controller we have our home dot PHP file so this file is a home controller so we can access it by localhost / home and we will see the exactly same page the the reason we see the exactly same page is because the home controller happens to be our default controller whenever we go to the localhost without specifying any URL so let's go and create now our own controller and see in and then see how we can navigate to it so let's duplicate home dot PHP let's name it shop and of course change the class name also to shop so one important thing here your controllers the first letter the first letter of your controller should be always uppercase at the rest of the letters should be always lower case so let's go now and instead of returning view welcome page let's return view shop and of course to see and hear we have to go to the views folder and create a view with a name with a file name shop dot PHP so let's go here new file is great shop dot PHP awesome so let's give some a let's give an h2 tag a name this is a shop so if we go now back to our browser window and we go to slash shop but you can see that we have here this is a shop title that we have created so what happens whenever we access we specify the URL that matches the controller's name whenever the first segment and by first segment we mean that the first part after the domain name so shop is ours is our first segment whenever it matches the the name of a controller then the index method of that controller will be executed let's go now and create another method inside of our shop class and let's say this will be public function product and let's return view return review let's say product you let's go to our views create this file new file product dot PHP and let's say this is a product so if we go back to our browser and we say shop slash product you can see the that now we are navigated to the specific method of a shop controller just by specifying the the names inside of our URI so the first segment goes for the controller name the second segment goes for the method name of that controller if you don't specify the second the second method the second segment the default method to be used is index method ok so let's say that now we want to access a specific product let's say we want to access product for instance laptop let's say we are in a in an e-commerce store so we have shop product laptop as you can see now nothing nothing is changed so the reason nothing is changed is because if we want this product page our product page to be dynamic we have to specify also inside of our product function that we are expecting to get some parameters through our URI so let's say we will add type here and let's not use the videos for now we will see how we can pass our data inside our our views let's just echo the information here so let's say echo and what did we have there h2 tag h2 so this is a product and let's specify let's say the type print it out so now if we go again to our page and refresh it you can see that this is a product laptop or let's say if we specify PC this is a product PC okay so if I want to get PC with ID o24 let's say like that guess what we can just pass that is our second parameter so let's say product ID and we can take this product ID so we have this is a product laptop with an ID or product ID so if we come back to our browser window and refresh that you can see that this is ABC 24 or episode 22 whatever was specified in our URL so what happens if inside of your controller you want to have a method that you don't want to be you don't want it to be publicly available for instance this is a login method for your users or some sort of validation methods that no one should access it although you need it inside of your controller to run it through other to use it in other methods so let's say we have we have here our functions actually the only thing we have to do here specify instead of public function new product so specify protected function let's say admin let's say I don't know admin check whatever echo this is protected area for whatever reason I want to echo something here so if we go now to our admin shape and try to access it so we go to the controller name shop slash admin shed as you can see we get 404 error because this method is not is not allowed by the routing by by automatic automatic routing of CodeIgniter because we have specified it as a protected method one more thing I want to show you that we can do with our controllers keys how we can create new folder structure inside of our controllers method controllers folder so let's say we have here our shop that we have created with our product and let's remove this last private protected function we don't need it and but we want to create an area for the administration for the backend so let's go inside of our controllers and create a new folder this folder we can name it as as we like but for this tutorial let's create admin let's say admin area so inside this admin folder let's create a new file or actually let's duplicate a file with the same name of yeah let's duplicate the shop file here so let's go and duplicate this and duplicate it to the admin folder so as it isn't right now this will not work so even if we go to instead of shop we will specify admin and our shop we will get 404 error so whenever we change the folder the folder structure so we go from within our controllers folder we create some subcategories we have to change also the the namespaces of the file or the namespaces for each class new class that we create so as you can see here inside of our home dot PHP controller the default controller that we have the namespace is up controllers so what it means that this file is located inside the app folder and then inside the controllers folder so in our new shop dot PHP file this is not exactly true because yes we are located in up and controllers but now we are also located in the admin folder so we have to specify this folder here and if we want to continue to extend the base controller we have to specify also now the path of this base controller because it is not anymore in the in the same in the same folder as it was before so to do that we have to specify it with a use you statement here and we save up controllers and we have our base controller here so now base controller again is accessible and our our our shop dot PHP Trotter knows from where to load it so let's test now on this and see if it works so we have admin shop and this is actually let's change the name because how can we know that this is a different shop let's return here or let's echo okay echoing is not the best way to finish a method but this is just for a demo so let's say this is an admin shop area and let's give it an h2 tag I'm using h2 because it's not that that huge like an h1 so let's try and reload and this is an admin shop area so if we would want to also another useful feature and actually the main feature of namespaces for example is you can see now that we have to shop dot PHP files so what happens if we go to our home dot PHP and we want to use these controllers inside of our home home controller and let's say we create function that will be named let's say validation whatever and we want to add here two new instances or let's say we want to echo the the the product the product method we want to run product method from this shop dot PHP that is publicly we have publicly available and the other one that is from inside of the admin page so let's change it also here and say this is an admin product let's see what happens in this case so now we are here we are so first of all we have to create these two new classes let's say product I know option let's see shop equals new shop yeah new shop and we want to echo shop product and let's give the two parameters that it accept so the first one will be laptop and the ID will be I don't know let's say Lenovo why only who I use only numbers so let's try and run this validation travel browser this validation method so we go to our home controller and validation so this is a product laptop with an idea of Lenovo cool what happens if we want to create another shop instance that we will pull from our admin shop class so first of all our home controller does not does not know from where to get this class so what you have to do we have to say I use up controllers admin shop okay so let's see what happens here first of all if I will comment this out and let's just run it again so now it says this is an admin product so why that why is that happening so if we remove this and run this is a product laptop with idea of Lenovo so this is the class in the controller outside from the admin but once we have specified the use of up controllers admin shop you see that the message has been the class that the validation here was using has been changed but what happens if you want to use both of the classes we want to use end the shop inside of our admin and outside of our admin so what we can do here we can live our shop as it is so this is the let's say this will be our shop object outside of the of the admin area and for this one we will say as admin shop and what we can do now is we can do here we can go here and say that we have admin on shop equals to our new admin shop and again we can echo sure it is just let's change to this and also let's add here a line break because otherwise it will be the same line so now you can see that the practical use of using namespaces because we can avoid the conflict in case we have some class names named in the same way so that's it for our controllers tutorial and in the next tutorial we will go through route and we will see how we can create our own URLs or how we can create dynamic URLs also so see you in the next video