Ok, here is simplified code of how i would try implement an MVC pattern to the above app.
Basically the links and forms in the app contain _OBJECT and _METHOD values either via $_GET or $_POST.
So in this example if i clicked on the "edit" link, i would create an "ArticleViews" object and call the "articleForm" method from that object.
and by submitting the form i would create a "ArticleModel" object and call the "editArticle" method.
Would you consider this MVC? I've used this approach and it and have not had any problems, but i feel like i'm missing the boat on this concept.
PHP Code:
// Model
class ArticleModel{
function getArticles()
{
$sql = mysql_query("SELECT * FROM articles");
return mysql_fetch_array($sql);
}
function getArticleById($id)
{
$sql = mysql_query("SELECT * FROM articles WHERE id = '$id'");
return mysql_fetch_array($sql);
}
function createArticle()
{
$id = $_POST['id'];
$article = $_POST['article'];
mysql_query("INSERT INTO articles (id, article) VALUES ('$id', '$article')");
}
function editArticle()
{
$id = $_POST['id'];
$article = $_POST['article'];
mysql_query("UPDATE articles SET id='$id', article='$article'");
}
function deleteArticle()
{
$id = $_POST['id'];
mysql_query("DELETE FROM articles WHERE id='$id'");
}
}
// View
class ArticleViews{
function displayArticles()
{
$a = new ArticleModel;
// create link
echo "<a href=\"index.php?_OBJECT=ArticleViews&_METHOD=articleForm\">Create Article</a><br>";
while($article = $a->getArticles())
{
// edit link
echo "<a href=\"index.php?_OBJECT=ArticleViews&_METHOD=articleForm&id=".$article['id']."\">Edit</a><br>";
// delete link
echo "<a href=\"index.php?_OBJECT=ArticleModel&_METHOD=deleteArticle&id=".$article['id']."\">Delete</a><br>";
echo $article['article'];
}
}
function articleForm()
{
// edit
if($_GET['id'])
{
$a = new ArticleModel;
$article = $a->getArticleById($_GET['id']);
$id = $article['id'];
$article = $article['article'];
echo "<input type=\"hidden\" name=\"_OBJECT\" value=\"ArticleModel\">";
echo "<input type=\"hidden\" name=\"_METHOD\" value=\"editArticle\">";
}
// create
else
{
echo "<input type=\"hidden\" name=\"_OBJECT\" value=\"ArticleModel\">";
echo "<input type=\"hidden\" name=\"_METHOD\" value=\"createArticle\">";
}
echo "<form action=\"index.php\" method=\"post\">";
echo "<input type=\"hidden\" name=\"id\" value=\"$id\">";
echo "<input type=\"text\" name=\"article\" value=\"$article\">";
echo "<input type=\"submit\">";
echo "</form>";
}
}
// controller
class AllowableCalls {
function getLoadedClassMethods()
{
$classes = get_declared_classes();
foreach($classes as $class){
$class_methods[] = get_class_methods($class);
}
foreach($class_methods as $class=>$methods){
foreach($methods as $method){
$method_array[] = $method;
}
}
return $method_array;
}
function getLoadedFunctions()
{
$functions = get_defined_functions();
return $functions[user];
}
function getAllowableCalls()
{
$allowable_calls = array_merge($this->getLoadedClassMethods(), $this->getLoadedFunctions(), get_declared_classes());
return $allowable_calls;
}
}
class Controller{
function process($allowable_calls, $request){
if(in_array(strtolower($request['_OBJECT']), $allowable_calls))
{
$object = $request['_OBJECT'];
$moduleObject = new $object;
}
else {
$object = "DefaultClass"; // default
$moduleObject = new $object;
}
if(in_array(strtolower($request['_METHOD']), $allowable_calls))
{
$method = $request['_METHOD'];
$moduleObject->$method();
}
else {
$method = "defaultView"; // default
$moduleObject->$method();
}
}
}
Then in my index.php
PHP Code:
$allowableCalls = new AllowableCalls;
$controller = new Controller;
$controller->process($allowableCalls->getAllowableCalls(), $_REQUEST);
Bookmarks