<?php // Front Controller require_once '../libs/PHPTAL-1.3.0/PHPTAL.php'; require_once 'init.php'; loadScripts(); if (!Utils::isGET()) { $data = array("status" => "error", "msg" => "Only GET allowed."); } $parameters = new Parameters("GET"); $showUserProfileAction = new ShowUserProfileAction(); $showUserProfileAction->setParameters($parameters); $profile = $showUserProfileAction->getProfile(); // here's where we can choose how to render: AJAX or non-AJAX // this might affect how we output the data (i.e., JSON vs HTML) if (Utils::isAJAX()) { $data = array(); // AJAX means that we'll send it as JSON - at least for this call if ($profile == null) { // didn't find a profile with that name $data = array("status" => "error", "msg" => Messages::getMessages()); } else { $data = array("status" => "success", "profile" => $profile); } echo json_encode($data, JSON_FORCE_OBJECT); return; } else { // render the whole page using PHPTAL // finally, create a new template object $template = new PHPTAL('index.xhtml'); // now add the variables for processing and that you created from above:
<?php // Business Delegate // load all scripts into memory //require_once('init.php'); require_once 'Parameters.php'; require_once 'ShowUserProfileAction.php'; require_once 'Utils.php'; //loadScripts(); $data = array("status" => "not set!"); if (Utils::isPOST()) { if (Utils::isAJAX()) { $parameters = new Parameters("POST"); $userProfileAction = new ShowUserProfileAction(); $userProfileAction->setParameters($parameters); $response = $userProfileAction->getProfile(); if ($response) { $data = array("status" => "success", "user" => $response, "msgs" => Messages::getMessages()); } else { // error message $data = array("12status" => "error", "msg" => Messages::getMessages()); } } else { $data = array("13status" => "error", "msg" => "AJAX Required.", "msg" => Messages::getMessages()); } } else { $data = array("status" => "error", "msg" => "Only POST allowed.", "msg" => Messages::getMessages()); } // lastly send JSON response echo json_encode($data, JSON_FORCE_OBJECT); // for objects do this: