kc9ddi Posted December 5, 2006 Share Posted December 5, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/29556-php-mvc/ Share on other sites More sharing options...
jsladek Posted December 5, 2006 Share Posted December 5, 2006 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 infoinclude("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 databasemysql_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 tableshow_data(); }[/code]-John Sladek Quote Link to comment https://forums.phpfreaks.com/topic/29556-php-mvc/#findComment-135598 Share on other sites More sharing options...
448191 Posted December 5, 2006 Share Posted December 5, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/29556-php-mvc/#findComment-135693 Share on other sites More sharing options...
shaggycap Posted December 6, 2006 Share Posted December 6, 2006 kc9ddi,check this out: It rocks![url=http://www.codeigniter.com]www.codeigniter.com[/url] Quote Link to comment https://forums.phpfreaks.com/topic/29556-php-mvc/#findComment-136162 Share on other sites More sharing options...
redbullmarky Posted December 6, 2006 Share Posted December 6, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/29556-php-mvc/#findComment-136176 Share on other sites More sharing options...
jsladek Posted December 6, 2006 Share Posted December 6, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/29556-php-mvc/#findComment-136184 Share on other sites More sharing options...
redbullmarky Posted December 6, 2006 Share Posted December 6, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/29556-php-mvc/#findComment-136205 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.