Reputation: 9645
What solutions,patterns usually used for this?
I want to get rid of if/else statements in my controllers, models and so on.
For example:
if($domain==1) {
// do this
}
elseif($domain==2) {
// do this
}
elseif...
Want to get rid of this madness. Can't imagine what mess will be, when there will be at least 20 websites.
Currently i'm using config and routing files for each domain. But that's not enough.
Can't get rid of this mess inside models and controllers.
I was thinking about some kind of placeholders and separate static class for each domain with method for those placeholders + magic calls.
For example i have action inside controller:
public function postAction(){
$model=new Model();
$this->view->data=$model->get($placeholder_generates_and_return_settings_array); // else default is used
// custom placeholder
// execute custom class method if it's exist
// some model again
// custom placeholder
// execute custom class method if it's exist
// etc
}
Current view is provided inside placeholders Class, types can be assigned. Like data modification, config generation for model etc.
How would you resolve this issue with multiple domains, without cloning controllers, models or creating innumerous if/elseif statements for each domain inside them?
UPDATE
How to describe what i need. I'm trying to create reusable controllers with default logic in it. Just filling/MIXING controller with domain related logic in required places(placeholders), data modification etc. Something like controller-template possible, any patterns exist?
Providing placeholder with all required(CURRENT) data for it's modification if required or further processing AND returning it back.
Guess i'll have to create my own "bicycle". :D
Upvotes: 2
Views: 322
Reputation: 151654
if($domain==1) {
// do this
}
elseif($domain==2) {
// do this
}
elseif...
I'm curious what "do this" for important thing is that you need do to it for all sites. It's not possible to specifically answer the question without knowing that.
I can assume you would like to set some variables, for example set up a session, perhaps create some other (database) object with different parameters, or set some variables in a template.
You could do that using some kind of controller, but again, it depends on what you want to achieve.
class Controller
{
public abstract function DoAction1();
// Group shared functionality, call it with different parameters
public function ShowHomePage($view)
{
$template->assign('view', $view);
}
}
class Domain1Controller extends Controller
{
public function DoAction1()
{
// do this
}
}
class Domain2Controller extends Controller
{
public function DoAction1()
{
// do this
}
}
And in your calling code (the router) you simply call Controller->ShowHomePage($view)
or Controller->DoAction1()
or whatever, but only after you determined on what domain you are and what controller you want to address.
Upvotes: 0
Reputation: 72681
Want to get rid of this madness. Can't imagine what mess will be, when there will be at least 20 websites.
Why on earth would you put 20 websites in 1 project???
Just use separate vhosts
if you are on apache.
http://httpd.apache.org/docs/2.0/vhosts/examples.html
Other webservices also have this functionality (although they might have a different name).
If you want to prevent you need to copy common/shared functionality in you projects.
Just setup some form of a library and with the common functionality which you use in your different projects.
If you look into the MVC pattern http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller you could even have some default views in your library for the projects to use (if needed these can be overridden of course).
Upvotes: 0
Reputation: 218
Based on the information you provide I assume that you wish to display your data differently based on the domain. Also assuming that your data remains unchanged you could use a strategy pattern to solve your problem.
Your class structure would then look as follows:
class yourClass
{
protected $_strategy;
public function setStrategy($strategy)
{
$this->_strategy = $strategy;
}
public function showYourData()
{
return $this->_strategy->show($this)
}
}
For each domain you build a separate strategyclass as follows:
class domainStrategy
{
public function show(yourClass $yourClass)
{
// Get your classdata here
$data = $yourClass->whateverFunctionYouNeed();
// Do what you want for this domain
return $output;
}
}
I hope this gets you started, I'm sure you can find more documentation for the strategypattern when you need it
Upvotes: 4
Reputation: 1954
Have a folder for each domain.
When the user accesses the web site (maybe the contactus.php page), this page will check if there is a corrisponding contactus.php file in the domain folder. If there is, it will include that file, otherwise, it will do it's default behaviour.
Upvotes: 0
Reputation: 2547
I suggest to create a dispatcher that loads information based on domain criteria.
Something like:
dispatch.php
<?php
...
$domain = get_domain_function(); // here you may automate the domain retrieval
include ('controllers/' . $domain . '.php')
...
?>
controllers/domain1.php ... controllers/domainn.php
<?php
...
do the domain specific business logic here
...
?>
Upvotes: 2