How to change the routes to set the urls in codeigniter

I have created a codeigniter 3 project but I have set the name of controller as “exampleController” which is showing on my URL. I want to remove the word “controller” to setup custom routes. How can I do it?

$route['alt-text'] = 'AltTextController/index'; // Route for the index method (loading the view)
$route['alt-text/upload'] = 'AltTextController/uploadPdf'; // Route for uploading a PDF
$route['alt-text/delete'] = 'AltTextController/deletePdf'; // Route for deleting a PDF
$route['User/alt-text'] = 'UserController/AltText';// $route['']

I have set these in routes. Please help me to understand

In CodeIgniter 3, you can use custom routes to control how URLs map to controllers and methods, and hide or replace certain parts of the URL. To remove the word “controller” from the URL while keeping the functionality intact, follow these steps:

1. Use Custom Routes in application/config/routes.php

Define routes to alias your controller and methods. For example:

$route['alt-text'] = 'exampleController/index'; // Maps 'alt-text' to the 'index' method
$route['alt-text/upload'] = 'exampleController/uploadPdf'; // Maps 'alt-text/upload' to 'uploadPdf'
$route['alt-text/delete'] = 'exampleController/deletePdf'; // Maps 'alt-text/delete' to 'deletePdf'
$route['user/alt-text'] = 'UserController/AltText'; // Maps 'user/alt-text' to 'AltText' in UserController

Here:

  • The left-hand side ('alt-text') is the URL segment users will see.
  • The right-hand side ('exampleController/index') specifies the controller and method.

2. Update Your Controller Names (Optional)

While exampleController works, using PascalCase or meaningful names like AltTextController is recommended in CodeIgniter for readability and consistency. Example:

Rename exampleController.php to AltTextController.php and ensure the class name matches:

class AltTextController extends CI_Controller {
    public function index() {
        // Your code for index
    }

    public function uploadPdf() {
        // Your code for uploading PDF
    }

    public function deletePdf() {
        // Your code for deleting PDF
    }
}

3. Removing the “Controller” Word

If you want to hide the “Controller” word from URLs entirely:

  • Use custom routes like the ones defined above, and alias them without mentioning “Controller.”

For example:

$route['alt-text'] = 'exampleController/index';
$route['alt-text/upload'] = 'exampleController/uploadPdf';
$route['alt-text/delete'] = 'exampleController/deletePdf';

These routes map alt-text, alt-text/upload, and alt-text/delete to the methods of exampleController. The “Controller” part is omitted from the URL.

4. Setting a Default Controller

If you want a particular controller to load by default (e.g., exampleController), you can define it in routes.php:

$route['default_controller'] = 'exampleController';

When users access the base URL (e.g., http://your-domain.com), it will load the index method of exampleController.

5. Debugging Routes

You can debug the routes to ensure they’re working as intended by accessing http://your-domain.com/alt-text, http://your-domain.com/alt-text/upload, etc.

6. Clean URLs with .htaccess

To remove index.php from the URL (if it’s still visible), add an .htaccess file in the project root:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    # Redirect all requests to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

Ensure mod_rewrite is enabled on your server.

7. Final Routes Example

Your routes.php might look like this:

$route['default_controller'] = 'AltTextController'; // Default controller
$route['alt-text'] = 'AltTextController/index'; // Maps to index
$route['alt-text/upload'] = 'AltTextController/uploadPdf'; // Maps to uploadPdf
$route['alt-text/delete'] = 'AltTextController/deletePdf'; // Maps to deletePdf
$route['user/alt-text'] = 'UserController/AltText'; // Maps to AltText in UserController
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

8. Testing the Routes

  • Access http://your-domain.com/alt-text to load the index method.
  • Access http://your-domain.com/alt-text/upload to load the uploadPdf method.
  • Access http://your-domain.com/alt-text/delete to load the deletePdf method.

This setup will hide the “Controller” word from the URL while preserving functionality. Let me know if you encounter further issues!