How can I avoid direct access to codeigniter controllers, rather than routes?

When we are defining all your URLs through the routes.php file, the direct access to your code-igniter class will make accessible duplicated pages for the website. Duplicated pages affect SEO in large extents, so better to avoid direct access to the controller functions.

Step 1: Create a controller function for 404 response page

Create a separate controller and a normal public function in the controller folder. The following code inside the function makes the 404 error response page.

Step 2: Modify your routes.php file

At this point, you already have a good idea about the config/routes.php file, otherwise please have a look at: Documentation for Codeigniter Routes. The routes.php will be similar to the below code after modification:

There are mainly two points in your routes files:

  1. Overwrite your default 404 error page with defined controller page:  $route['404_override'] = 'errors/show_404';
  2. After defining all your routes, avoid further URL access by pointing all other URLs to the 404 controller page written before:  $route['(:any)'] = 'errors/show_404';

So, pointing all requests to the 404 page after parsing all valid routes will help you to avoid direct access to the controller with controller name and its method names.

CodeIgniter Form Validation – How to get all error fields as an array

CodeIgniter by default is not providing you the error fields for a form as an array. To add this feature to your codeigniter form_valiation library, you have to extend the system library and overload as form_validation library.

Create the file: application/libraries/My_Form_validation.php file and add the following code:

In your controller use the following code to get your error fields as an array:


Sending email from your Codeigniter application with Gmail SMTP

With the Codeigniter’s default email library, we can send email through our Gmail account. This will help us to avoid spam mails, generating from the Codeigniter application.

1. Create GMAIL Configuration for the email configuration

You only have to take care of your email configuration to implement email sending properly for your application. Open your  application/config/config.php file and add the following lines of code at the end of your config file:

2. Create a function to send email from your Codeigniter application

You can create this function as a helper or library according to the usage. I am here, creating the function as a private method in the controller, assuming only one controller will share this email function. The method code is as follows:

3. Call the function and use HTML template for the email

We created the configuration to send HTML emails. So you have to create a view file that holds the complete HTML for the email template. You can create PHP variables in the file and pass dynamic values from the controller. Create a sample email template as below:

Save the file at:  application/views/email.php. In your controller page function, use the following code to initialize the email body and send the email:

Make sure that your computer is open to the port 465. Hopefully you will receive an email from your gmail account. There is chance that the email generating from your application can be considered as insecure, and the gmail will block your access. Login to your gmail account and turn on the less secure app configuration to allow insecure applications to access gmail SMTP.

Basic Codeigniter Active Record Knowledge

Following information will give you an idea about how you can use the codeigniter active record class in your models.

You have to initialize your database in your model constructor with

Select Data

You can make a simple select query like:  SELECT * FROM table_name  with the following:

A select query with where condition:

For selecting a single row from a table with the codeigniter active record class:


Looping Data

The result()  method gives you the result as an array of objects. You can parse through the array as normal. Following are the usual two scenarios for the looping:

Insert, Update and Delete with Codeigniter Active Records

Use insert()  method for SQL insertion operation.

Update operation is same like insertion. The update()  method has a third parameter as where condition with which the updating rows will be selected.

Delete method delete()  is also same as the previous two methods. Here, the second parameter is the where clause which used to select the deleting rows.

For more details, please read through the official codeigniter user guide.