|

Controllers in Codeigniter

codeigniter-controller

The MVC pattern is popular in all languages and in particular PHP. MVC is the model, view, controller paradigm in programming. We’ll take a look at controllers now, as they are the portion of the MVC approach that handles incoming HTTP requests, and direct your application to take action. Controllers are used in all flavors of frameworks, and each has their own attributes. For this demonstration, we’ll focus on codeigniter.

What is a Controller?

A Controller follows a naming convention such that a class file is loaded based on the name of the first segment of the URI.

For example if you have the URI http://www.site.com/index.php/sayhi/ this will load a controller named sayhi.php.

Let’s have a look at some code that would show what the sayhi.php controller would actually look like.

This controller file is saved in the application/controllers directory. Now to execute this script you would simply visit your URL similar to http://www.site.com/index.php/sayhi/ and you should see the output I will now say hi!

Some key points to remember when dealing with creating your own controllers is that the name of the controller should be uppercase and it extends the parent controller class. This is so that your new controller automatically inherits all of the built in methods and functionality of the parent class. This is the benefit of using a framework, you get access to tons of functionality that has already been written for you.

What is a Method?

A method is also called a function in object oriented programming. So far we have only seen an index() function in our code. This is the default action that takes place if no action is specified in the second segment of the URI. Remember the format of MVC follows http://www.site.com/controller/method/

The second segment of the URI maps to the names of functions within your controller. Let’s say we want to not only say hi, but also, ‘nice to meet you!’. We’ll create a function named greet that will do just that!

Just like we did before you can now load http://www.site.com/index.php/sayhi/greet/ and you should see the output nice to meet you!

What are function arguments?

In order for our functions to actually do some cool things and operate on the data they are given, we need to pass them arguments. These are pieces of data that are handed off to the function so that the function can do things. To do this, you use additional segments in the URI typically after the 1 segment (controller) and 2 segment (function). We can modify our greet function so that it can greet a person by name.

nice to meet you tom! should now be the output when visiting http://www.site.com/index.php/sayhi/greet/tom

Making a function private

Sometimes you will need to have function that are not available for public use. So far, our functions have been defined as public and this is what allowed us to visit our URL such as http://www.site.com/index.php/sayhi/greet/ and our greet function executes happily. What if we need some functions that can run, but shouldn’t be triggered via a public URL?

In this case we declare them as private which will accomplish our goal.

If you visit http://www.site.com/index.php/sayhi/_ninja/ the code will not run.

Using Constructors in your Controllers

Constructors are a great way to load up some helpers or libraries, as well as set some default values. If you would like to do this you need to follow the following convention.

Thank you for reading Controllers in Codeigniter – If you found this post helpful, Please do share using the buttons below!