This is a repost of my question on the official CI forums. I figured I might as well try as many places as possible:
Let’s say, for example, that I have created a model, view, and controller for displaying website news. If I later want to implement a second news module on the site that follows the same logic but resides at a different URL and pulls data from a different MySQL table, what is the best way to accomplish that?
I know that I could simply duplicate the files and modify the queries, but that will become a hassle to maintain since I will need to modify the code twice(or three, or four times, etc. depending on how many instances of the module I want) whenever I make a change.
So, any suggestions for a DRY way to do it?
[CodeIgniter] Multiple Instances of the Same Module?
Moderator: General Moderators
Re: [CodeIgniter] Multiple Instances of the Same Module?
I don't know much about CI, but couldn't each module use a different router configuration and be assigned a unique database table prefix?
Re: [CodeIgniter] Multiple Instances of the Same Module?
I am with Benjamin on this one.
Take a look at the _remap Controller method in CI.
Take a look at the _remap Controller method in CI.
There are 10 types of people in this world, those who understand binary and those who don't
Re: [CodeIgniter] Multiple Instances of the Same Module?
Thanks for the responses. I was unaware of the _remap() method, but it looks like it accomplishes the same thing that the routes config file does. Am I missing anything?
Here's what I'm thinking based on your posts:
It's obviously just some sample code, but is that basically how you would accomplish it? Is there a "better" way to do it? Would _remap() provide any additional functionality?
Here's what I'm thinking based on your posts:
Code: Select all
// config/routes.php
$route['sitenews'] = "news/index/sitenews"; // sitenews will be the "module_name"
$route['othernews'] = "news/index/othernews"; // othernews will be the "module_name"
// news_controller.php
function index($module_name) {
// we'll just say that $module_name is what we're going to name the table too
$this->db->get('news_'.$module_name);
// and the view!
$this->load->view('news_'.$module_name);
}
Re: [CodeIgniter] Multiple Instances of the Same Module?
They're suggesting having one module, with many possible routes. Depending the route, a different request parameter will be set to indicate which route was used. There will just be 1 module "news", but if its accessed by an alternate route, such as "/blog" it would set some request parameter (presumably telling the controller to use a different table prefix for those requests).
The other great thing is you can have multiple views per controller with MVC... so you could have a radically different user interface on the blog, for example (yet still be running it thru the same controller & models. The only thing different would be the route used to access it, the table it pulls from, and the view script it spits it's data into.)
The other great thing is you can have multiple views per controller with MVC... so you could have a radically different user interface on the blog, for example (yet still be running it thru the same controller & models. The only thing different would be the route used to access it, the table it pulls from, and the view script it spits it's data into.)
Re: [CodeIgniter] Multiple Instances of the Same Module?
Josh, that's what I was alluding to as well - I guess my code sample wasn't clear enough.
A little more background on this: I've extended the CI libraries to allow for a basic multi-site functionality where each site can use its own custom views. This way, I can run as many sites as I want from the same codebase, but they can look as different as they want. Now, if I have a generic blog module that I've written, it can be just as easily re-purposed to a news module with some changes to the view. However, it will still reside at the URL http://www.mysite.com/blog instead of /news. Using CI's routing config file, it's easy enough to fix this, with one caveat: The routes are going to get exponentially more difficult to manage with each new site that pops up. Some sites may need multiple versions of each module or different URLs, etc.
I know that I can accomplish what I need through the routing config, but it feels kludgey and I'd like if there were a better way. I'm still not sure I fully understand the purpose the _remap() function - any examples I can find make it look like a more complex way of writing static routes. Am I wrong?
A little more background on this: I've extended the CI libraries to allow for a basic multi-site functionality where each site can use its own custom views. This way, I can run as many sites as I want from the same codebase, but they can look as different as they want. Now, if I have a generic blog module that I've written, it can be just as easily re-purposed to a news module with some changes to the view. However, it will still reside at the URL http://www.mysite.com/blog instead of /news. Using CI's routing config file, it's easy enough to fix this, with one caveat: The routes are going to get exponentially more difficult to manage with each new site that pops up. Some sites may need multiple versions of each module or different URLs, etc.
I know that I can accomplish what I need through the routing config, but it feels kludgey and I'd like if there were a better way. I'm still not sure I fully understand the purpose the _remap() function - any examples I can find make it look like a more complex way of writing static routes. Am I wrong?
Re: [CodeIgniter] Multiple Instances of the Same Module?
In case anyone was curious, I ended up implementing a pre-system hook to query the site's DB for custom routes. That way I don't have to maintain all of them in a config file and they are localized to each site.