Jump to content

Recommended Posts

I'm trying to write a relatively simple app in PHP - just a photo manager that lets you search by keywords.  I would like to write with the MVC paradigm to the greatest reasonable extent, but I'm having some trouble.  Basically, for simple CRUD functions, I have a file that's mostly HTML, with some tags like <? echo $template['pageTitle']; ?> to fill in dynamic content.  This file is the included by a model/controller type script.  This script is essentially a giant if-elseif-elseif... block.:

[code]
if($action == 'new') {

} elseif($action == 'edit') {

} elseif($action == 'delete') {

}
[/code]

The problem I'm having is that the model/controller script is getting kind of ugly.  Any general suggestions for how to nicely organize an app like this?
Link to comment
https://forums.phpfreaks.com/topic/29556-php-mvc/
Share on other sites

Here is what I have been using.  I just pretend the functions are subroutines.  Only difference between the if..elseif..else is that all the conditions are at the top instead of spread throughout (well, maybe not the only difference).
I've included only one function/subroutine as an example.

[code]switch ($action) {

case 'addrecord':
new_record();
break;
case 'insertrecord':
insert_record();
break;
case 'deleterecord':
delete_record();
break;  
case 'editrecord':
edit_record();
break;
case 'showchanges': 
show_changes();
break;
case 'verified':
update_database();
break;
default:
show_data();
}

#-------------------------------------------------------------------------------------------------------------------------------
function delete_record(){
//get the variable datakey


//database info
include("dataConn.php");


   $id = $_GET[id];
   //print out variables so we can see if they made it
   #print ("$id <br>");
  
#i removed other $_GET's for this post
  
//connect to database
mysql_select_db($database_dataConn, $dataConn);

// create sql

$sql_delete_record ="DELETE FROM `courses` WHERE `id` ='$id' LIMIT 1 ; ";
#print ("$sql_delete_record");

// delete the old record if it exists
$result = mysql_query($sql_delete_record);

// close database
mysql_close();

//show records in table
show_data();

}[/code]

-John Sladek
Link to comment
https://forums.phpfreaks.com/topic/29556-php-mvc/#findComment-135598
Share on other sites

Using a controller like that, with switch/cases (or even worse if/elseif) is just plain ugly. Sure, if you insist on avoiding OOP, you'll have little choice, but otherwise I recommend you make better use of OOP.

I'm not sure what you're doing here, but it looks like a page or even front controller. Switch (and elseif) is a transactional thing mostly. When using OOP, it makes no sense to name actions that could, in the worst of implementations, be methods.

If you want to use the MVC or any other pattern, you'll have a hard time doing so without OOP, to put it mildly. In many (or even most) cases it is simply impossible.

Look into employing front and page controllers, I can help you if you have a specific question.

Link to comment
https://forums.phpfreaks.com/topic/29556-php-mvc/#findComment-135693
Share on other sites

shaggy, good call. i've been a fan of that for months. having studied it a bit more, i'd say there ARE better frameworks (having said that, codeigniter is fairly new in comparison) but the online manual over at CodeIgniter is possibly the best, most laymans description of MVC i managed to find - the manual is worth checking out, even if you dont like the framework.
Link to comment
https://forums.phpfreaks.com/topic/29556-php-mvc/#findComment-136176
Share on other sites

I've downloaded codeigniter, installed it and started reading the instructions and still dont have any idea what the hell it is doing.  Keep in mind I have a short attention span.  I am determined to work on one of these frameworks.  I'm mostly interested in one that can be used to build business applications and be taken seriously.  Redbullmarkey, what frameworks would you say are the better (if not best) frameworks out there?

-John
Link to comment
https://forums.phpfreaks.com/topic/29556-php-mvc/#findComment-136184
Share on other sites

personally - Cake and CodeIgniter, but only because 'under the bonnet' is nice and simple and the manuals/support is pretty good on both. My interests were to write my own framework, rather than use either, so my reasoning maybe a little different. I liked the structure/workflow of both.
Installing both properly (including first configuration) is the only tricky bit. Actually using them is much easier as long as the 'restrictiveness' of MVC makes sense to you. Read the parts of the CI manual concerned with M/V/C itself, not just CI's features, and eventually things will click into place. Don't be put of by the learning curve of frameworks in general.

Enough gets said about the Zend Framework, but personally I think it's more of an overblown, heavy demonstration of best-practice, rather than something i'd enjoy using (ZF fans - not trying to start a debate in this thread - start another if you wanna go ;) ). There are others, but Cake and CI are the only ones that truly stuck.
Link to comment
https://forums.phpfreaks.com/topic/29556-php-mvc/#findComment-136205
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.