<div class="grid--cell fl1 lh-lg">
<div class="grid--cell fl1 lh-lg">
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, <a href="/help/reopen-questions">visit the help center</a> for guidance.
</div>
</div>
</div>
<div class="grid--cell mb0 mt8">Closed <span title="2012-09-23 18:49:52Z" class="relativetime">7 years ago</span>.</div>
</div>
</aside>
I am building a system while at the same time learning and implementing OOP and MVC.
The only thing I am still not sure about is how to incorporate AJAX into my MVC structure, so what I have gone with is a type of controller that responds to different "modes" from AJAX requests that accesses and returns results from the various objects in my system.
Is this a good way to do this or am I breaking the rules of MVC by having this controller that is very different to the other controllers in my system?
This is my AJAX "delegator":
session_start() ;
function __autoload($classname)
{
include('/controllers/'.$classname.'.php') ;
}
// Deligator for contact operations.
if($_POST['mode'] == 'OPPORTUNITY_COUNT')
{
$contactManager = new ContactManager($_SESSION['userid']) ;
echo $contactManager->fetchOpportunityCount() ;
}
elseif($_POST['mode'] == 'PROSPECT_COUNT')
{
$contactManager = new ContactManager($_SESSION['userid']) ;
echo $contactManager->fetchProspectCount() ;
}
elseif($_POST['mode'] == 'CUSTOMER_COUNT')
{
$contactManager = new ContactManager($_SESSION['userid']) ;
echo $contactManager->fetchCustomerCount() ;
}
else
{
echo 'Error: Unknown mode.' ;
exit() ;
}
FYI This is how I send data to the router:
socket.on('UPDATE_OPPORTUNITY_DATA', function(date){
$.ajax({data: 'mode=OPPORTUNITY_COUNT', success: function(data){
$('#opportunityCount').html(data) ;
shaker('#opportunityCount') ;
}
})
})
</div>
There are many ways to do this but the most simple is that in your controller you have actions that are only for ajax request. So instead of return a html it returns a json string.
I am not sure that framework you are using but I give a generic example.
class yourController {
function updateAction()
{
// do an normal page view
$this->renderView('view'); // this will be greatly different for different frameworks
}
function ajaxUpdateAction()
{
//do the ajax processing
echo json_encode($data);
die;
}
}
Looks not too bad for an MVC architecture, but the thing you're doing not sweetly is to do your routing and your actions in the same file. I would suggest you :
session_start() ;
function __autoload($classname)
{
include('/controllers/'.$classname.'.php') ;
}
// Routing for contact operations.
$out = null;
switch ($_POST['mode'])
{
case 'OPPORTUNITY_COUNT':
$controller = new ContactController;
$out = $controller->opportunityCountAction($_SESSION['userid']);
break ;
case 'PROSPECT_COUNT':
$controller = new ContactController;
$out = $controller->prospectCountAction($_SESSION['userid']);
break ;
case 'CUSTOMER_COUNT':
$controller = new ContactController;
$out = $controller->customerCountAction($_SESSION['userid']);
break ;
default:
break ;
}
if (is_null($out)) {
echo 'Error: Unknown mode.' ;
exit();
}
echo $out;
And then you implement your ContactController with the manager method calls.
Some interesting documentation about the difference between routing and controllers.
As a slight alternative to Ninsuo's routing, which definitely works.. I feel it violates the DRY principal, as you're going to have to add something to an ever growing switch statement, while also creating a new function that corresponds when you can have some code do this automatically.
if(method_exists($selected_controller,$_POST['either the url end or something')
{
//I typically use reflection to tell if a method is public or private
$reflection = new ReflectionMethod($this, $function);
if($reflection->isPublic())
{
call_user_func_array(array($selected_controller,$function), $_POST['blah or url']);
}
else
{
//Render 404 - The method is protected or private
}
}
//Render 404 - no such method exists in the controller
Essentially, after you're selected which controller you're going to use, you simply check to see if the corresponding method exists, and then run it ;].
I also prefer techniques that take advantage of the fact that your class is already parsed, function names are indexed, and calling the function is going to be a simple hash call with a time complexity of 1. As opposed to an ever growing list of things that doesn't take advantage of the tools you already have in place.. that are causing you to repeat yourself in the code multiple places.