Here's what happens in my framework:
-User interacts with the View
-The Controller handles this request, and decides which Command (Command/Strategy Pattern) should handle the task
-It then passes this request (an actual Request Object) to the Command, which decides which action it should perform based on information retrieved from the Request Object.
-The Command does it's action, using Domain Objects (Models I think?) to get data that the view will need. It adds this data to the Request as "feedback".
-When the Command is done, the Request is automatically sent back to the Controller which uses the Request Object to generate the correct view.
-Rinse, repeat...
And this is all not counting Ajax requests (which I feel just completely screws it up).
Does this sound ok?
I also have a couple more questions about how I should do things in general (I really want to iron out this framework):
-Should Domain Objects be able to query the database?
-Right now, my Domain Objects are just PHP representations of Database tables. So like each field would be another property of the Object. Right now I'm not using variables though, I'm just storing all of these fields in an array. Good? Bad?
I also want to just show some code showing a general case of what is going on:
Controller (A front controller file calls the run method upon user request):
<?php
namespace controller;
class Controller {
private $applicationhelper;
private $template;
protected function __construct() {}
static function run()
{
$instance = new Controller();
$instance->init();
$instance->handleRequest();
}
// could possibly be changed to only run at start up and handleRequest is run for each user request
function init()
{
$applicationhelper = Applicationhelper::instance();
$applicationhelper->init();
}
// way of deciding how to interpret HTTP request so that it can invoke the right code to fulfill the request
function handleRequest()
{
$request = new \controller\Request();
$cmd_r = new \command\CommandResolver();
$cmd = $cmd_r->getCommand($request);
$cmd->execute($request);
$template = new \view\Template();
if(!$request->getProperty('ajax'))
{
echo $template->getHtml($request->getFeedback());
}
}
}
?>
Typical Command:
<?php
namespace command;
use \base\util\MemberUtility as MemberUtility,
\base\util\SessionUtility as SessionUtility;
class ProfileCommand extends Command {
function defaultAction()
{
try
{
// member whose profile is being viewed
$arr = array("displayName", "avatar", "fName", "lName", "email", "streetAddress", "city", "state", "phone");
$idobj = new \mapper\IdentityObject (null, $arr);
$member = \domain\Member::produceOne ($idobj->field('displayName')->eq($this->request->getProperty('action')));
if(SessionUtility::isLoggedIn ())
{// user logged in
// logged in member
$loggedInMember = MemberUtility::getLoggedInMember ();
$this->setUpTopBar(true);
$this->getFriendOption($loggedInMember,$member);
$this->getMessageOption($loggedInMember,$member);
}
else// user not logged in
{
$this->setUpTopBar(false);
$this->request->addFeedback("friend_option", "");
$this->request->addFeedback("message_option", "");
}
if($this->request->getProperty('action') == $loggedInMember->getId())// on own page
{
$changeTitle = "Change Avatar";
$changeClass = "editChangeAvatar";
}
$this->request->addFeedback("user_name", $this->request->getProperty('action'));
$this->request->addFeedback("user_avy", $member->getState('avatar'));
$this->request->addFeedback("change_avy_title", $changeTitle);
$this->request->addFeedback("change_avy_class", $changeClass);
$this->request->addFeedback("number_apples", $member->getState("numberApples"));
$this->request->addFeedback("resume", \view\Template::produceHtmlFromSource ("view/html/pieces/resumePiece.html"));
$this->getResumePersonalPiece($member);
$this->getAddChangePiece($member);
$this->request->addFeedback("resume_section_pieces", $this->getResume($member));
$this->request->addFeedback("friends", $this->getFriendSection($member));
$this->request->addFeedback("friend_count", ($count = $member->getFriendCount()) > 0 ? "(".$count.")" : "");
}
catch (\base\ObjectDoesNotExistException $e)
{
// THIS WHOLE THING COULD PROBABLY BE MADE INTO A METHOD IN COMMAND!!!!!!!!!!!!!!!
$this->useAlternateMainPage("pageNotFoundLayout.tpl.html");
if(SessionUtility::isLoggedIn ())
{// user logged in
$this->setUpTopBar(true);
}
else// user not logged in
{
$this->setUpTopBar(false);
}
}
}
And then there would me more "Action" methods within the command any of which might be called. Plus of course, these commands have their helper methods like for example $this->getResume($member) in the above example.
That request object is what will be sent back to the Controller and used to generate the view using the Template class at the bottom of Controller::handleRequest.
Criticism is welcomed. Thanks!
This post has been edited by eZACKe: 09 June 2012 - 10:56 PM

New Topic/Question
Reply




MultiQuote





|