I am planning on building a lightweight simple MVC in PHP for simple projects.
I am trying to grasp a simple route of showing a View template or returning a JSON response if it is an AJAX request.
Below is a snippet I just came up with, it is all hypothetical so none of the methods actually exist yet, it is just the way I would expect them to work.
Looking at the code below and reading the comments in it, does this look like how you would probably return an AJAX request instead of a view template in a basic MVC?
/**
* Example Controller
*/
class Test_Controller extends Core_Controller {
function SomeAction($userId)
{
//GET ID from URI
$this->request->get($userId['id']);
// load a Model 'some' = Model Name
$this->profile_model = $this->loadModel('some');
// Get the Dataset from the Model
$profileData = $this->profile_model->getProfile($userId);
// Check if this is an AJAX request true/false
if($this->request->isAjax()){
//is AJAX is true then we return json instead of loading a View template
return $this->response->json($profileData);
}else{
// load view file
$this->view->load('userProfile', $profileData);
}
}
I prefer this way in your method:
As a result in my framework I have something like:
Then you simplify processing request on client side:
Hope that helps.