Here is my controller class for my MVC pattern with all the includes
removed. From what I know of the MVC pattern it is good to have a small controller class. Mine is just a collection of public static functions which accept requests and send data to the client.
Regarding send()
. I'm just wrapping the echo command for sending text from an Ajax call. This is for consistency and to have all text exiting the same point in code.
I have a ControlEntry (Snippet 2) which help decouple Super Globals and direct entry into the Control, so the two kind of work in Tandem.
Pull_Pic just pulls a path and inserts it into HTML for display.
Snippet 1
<?php
class Control
{
public static function ajax($ajax_type)
{
Session::start();
switch($ajax_type)
{
case 'ControlBookmark_add':
$control_bookmark_instance = new ControlBookmark('remove');
$control_bookmark_instance->add();
break;
case 'ControlBookmark_delete':
$control_bookmark_instance = new ControlBookmark('remove');
$control_bookmark_instance->delete();
break;
case 'ControlTweet_add':
$control_tweet_instance = new ControlTweet('remove');
$control_tweet_instance->add();
break;
case 'ControlSignIn':
$control_signin_instance = new ControlSignIn('remove');
$control_signin_instance->invoke();
break;
case 'ControlSignUp':
new ControlSignUp();
break;
case 'ControlTryIt':
new ControlTryIt();
break;
case 'ControlSignOut':
Session::finish();
new ControlSignOut();
break;
default:
throw new Exception('Invalid ajax_type');
}
}
public static function reload() // Because reload may be triggered by File Upload (second control path)
{
if (session_id() == "")
{
Session::start();
}
if((Session::status())==='bookmarks')
{
include 'class.ViewBookmarks.php';
}
else
{
include 'class.ViewMain.php';
}
}
public static function upload($fileType, $fileName)
{
Session::start();
$fileInstance = new UploadedFile($fileType, $fileName);
$fileInstance->createImages();
Session::updatePic();
self::reload();
}
public static function send($textStream)
{
echo $textStream;
}
}
**Snippet 2**
new ControlEntry();
class ControlEntry
{
public function __construct()
{
if(isset( $_FILES['ufile']['tmp_name']) )
{
if( ( $_FILES['ufile']['tmp_name']) != '' )
{
Control::upload( $_FILES['ufile']['type'], $_FILES['ufile']['tmp_name'] );
}
else
{
Control::reload();
}
}
else if( isset($_POST['ajax_type']) )
{
Control::ajax( $_POST['ajax_type'] );
}
else
{
Control::reload();
}
}
}