Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8676305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:09:37+00:00 2026-06-12T20:09:37+00:00

My Codeigniter project has a long list of identical categories, each with many identical

  • 0

My Codeigniter project has a long list of identical categories, each with many identical methods.

To make it dynamic and cleaner, I have used _remap functions to load the identical methods within the controller. Now I am trying to replicate the controllers

e.g. My controllers
Antelope.php Bat.php Cuckoo.php Dog.php Elephant.php… Zebra.php all have this format below (I used _remap to condense all the similar methods into one).

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Antelope extends CI_Controller {

    function __construct() {
        parent::__construct();
    }

    private function _remap($method, $params=array()){
       $this->animal = ucwords($this->router->fetch_class());
       $allowed_methods = array("Tame", "Buy", "Sell", "Gift");
       if (in_array($method, $allowed_methods)):
           // Model zoo has been autoloaded      
           data["foobar"] = $this->zoo->get_data($this->animal, $method);
           // Stuff goes here
       else:
          $this->index();
       endif;
    }

    public function index(){
       // Stuff goes here
    }
}

/** End of file Antelope.php **/

The remapping works fine for Antelope and all its remapped methods, but is there a way for me to apply this same method to all the other files so I can just have a single Animal.php Controller file instead?

I guess I may use routes.php, but the list of Controllers is too long; I’d have hundreds of lines in the routes file if I explicitly list each “animal” routing.

Any way around this?

EDIT: The “animal types” are listed in a database, and will keep increasing over time. I don’t want to keep revisiting the project to create new controllers or add new classes for the new elements in the database! This is why I want to use a dynamic routing method. Also, the project is a site redesign, so the URLs like http://www.website.com/antelope/buy/3 need to remain the same.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T20:09:39+00:00Added an answer on June 12, 2026 at 8:09 pm

    The trick is to realize that the animal type is variable and you’re trying to map it to static files. Don’t do this. Just pass the animal as the first argument to the index function. That’s what arguments are for: variables.

    class Animal extends CI_Controller{
        function __construct(){
            parent::__construct();
        }
    
        function index($animal){
            echo "I'm the {$animal}!";
        }
    }
    

    And set up the single route:

    $route['animal/(:any)'] = "animal/index/$1";

    Now, if you head to http://localhost/yourapp/animal/antelope

    CodeIgniter will echo “I’m the antelope!”

    Edit after your comment:

    CodeIgniter goes from top to bottom in your routes file and breaks when it finds a valid one. You can place this at the bottom of your config/routes.php file:

    $route['(:any)/buy/(:id)'] = "animal/$1/buy/$2";
    $route[':any'] = "animal/index/$1";
    //etc
    

    You’ll need to re-route all other controllers above this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that requires: user owns many projects. project has one owner.
I'm doing a project about cooking recipes with PHP(with Codeigniter) and MYSQL. I have
in my project i have a table named portfolio, which has an attribute called
I have used PHP for awhile now and have used it well with CodeIgniter,
My current project has version 1.6.3 of codeigniter framework. Now there is new release
I've got a CodeIgniter project that until now has been only run remotely (it
I currently have a web project developed with Codeigniter. My production environment works as
Basically I have two controllers using CodeIgniter for a simple blog for a project
I've been working on a home project using CodeIgniter Reactor, and have come to
Zend_Auth uses PHP Session but, codeigniter has a different approach altogether. The project that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.