Creating controllers and actions in CodeIgniter

CodeIgniter is a powerful PHP framework that follows the MVC (Model-View-Controller) architectural pattern. Controllers play a crucial role in handling the logic and flow of the application. In this article, we will learn how to create controllers and define actions in CodeIgniter.

Prerequisites

Before we start, make sure you have CodeIgniter installed and set up on your local development environment. You should also have a basic understanding of PHP and MVC concepts.

Step 1: Create a new controller

To create a new controller in CodeIgniter, navigate to the application/controllers directory. Let's say we want to create a controller called Products. Create a new file called Products.php in the controllers directory.

class Products extends CI_Controller {

}

Step 2: Define actions

Actions in CodeIgniter are methods defined within the controller class. These actions will handle the requests and define the logic for different functionalities. Let's add a few dummy actions to our Products controller.

class Products extends CI_Controller {

    public function index()
    {
        // Default action
        // Logic to display a list of products
    }

    public function show($id)
    {
        // Action to display a specific product
        // Logic to fetch and show the product with the given ID
    }

    public function create()
    {
        // Action to create a new product
        // Logic to handle form submission and insert the new product into the database
    }

    public function update($id)
    {
        // Action to update an existing product
        // Logic to handle the form submission and update the product with the given ID
    }

    public function delete($id)
    {
        // Action to delete a product
        // Logic to remove the product with the given ID from the database
    }

}

In the above code snippet, we have defined five actions within the Products controller. The index action is the default action that will be executed when the Products controller is accessed without specifying a specific action. The show, create, update, and delete actions are used to handle various functionalities related to products.

Step 3: Routing

To access the actions defined in the controller, we need to set up appropriate routing. Open the application/config/routes.php file and define routing rules for the Products controller.

$route['products'] = 'products';
$route['products/(:num)'] = 'products/show/$1';
$route['products/create'] = 'products/create';
$route['products/update/(:num)'] = 'products/update/$1';
$route['products/delete/(:num)'] = 'products/delete/$1';

These routing rules will map the URLs to the respective actions in the Products controller.

Conclusion

Creating controllers and defining actions in CodeIgniter is an essential step in developing your application. The controller acts as the intermediary between the data and the views, handling the processing of requests. By following the steps outlined in this article, you should now have a good understanding of how to create controllers and define actions in CodeIgniter.


noob to master © copyleft