Multiple views in MVC-like PHP Frameworks

Take a look at this post:

Specifically this code for the router:

else if ($_SERVER['REQUEST_URI'] === '/new' && $_SERVER['REQUEST_METHOD'] === 'POST' && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XmlHttpRequest')  {
	$model = new Model;
	$controller = new CreateController($model);;
    $controller->doAction($_POST);
    $view = new JsonView($model);	
} else if ($_SERVER['REQUEST_URI'] === '/new' && $_SERVER['REQUEST_METHOD'] === 'POST') {
	
	$model = new Model;
	$controller = new CreateController($model);
    $controller->doAction($_POST);
    $view = new View('newThing.html.php', $model);

}

You’ll see it routes slightly differently depending on whether it’s an ajax request, the only difference is the view being used, the same model and controller can deal with all the data processing, all we want from an ajax request is a different output: I want {"success": true} or {"success": false} rather than another HTML page or a redirect.

Essentially you can route to the same controller/model (to handle the same post data and process it). An ajax request should return {"success": true} whereas a non-ajax request will redirect. Putting that redirect in anywhere apart from the view limits the reusability of the code you place it in (in this case, if the redirect happened in the controller, I’d need to write a new controller action and a new view for the ajax/json request and the only real difference would be that the ajax request returns a different response: json rather than a redirect)