tags:

views:

763

answers:

5

Let's say I have a database table "widgets"

Should there be an index.php with a big switch statement for different actions, like "list_widgets", "edit_widget", "new_widget", "delete_widget"... and then each of those actions in a separate file?

Or, should there be list_widgets.php, edit_widget.php, delete_widget.php....

Or some other way?

EDIT (after reading the first answer). I'm really asking, what are the names of the files on my harddrive. If I were to create a website that just edited a single table of Widgets, what would be the names of the files on my harddrive for that project?

(My motive for avoiding frameworks right now is that this project is a learning excercise for php and mysql, so I don't want anything happening automagically yet)

+1  A: 

I'd create a Widget class and give it edit(), new(), and delete() methods. Then you could have a list() function somewhere that builds an array of Widget instances. I'd probably go ahead and create a WidgetList class (WidgetList.php), even if it's just a wrapper around an array initially.

Initially, I'd just have a Widget.php file for the class and an index.php to handle the various requests.

I always make sure the files containing my classes have the same name as the classes themselves (usually one per file). That makes it easier to figure out where things are and makes it easier to use the __autoload function.

Then I'd have a .htaccess file to create friendly urls that send the different types of requests to index.php. Something roughly like this:

Options ExecCGI FollowSymLinks Includes MultiViews

RewriteEngine On
RewriteRule /new index.php?action=new
RewriteRule /edit/([0-9]+) index.php?action=edit&widget_id=$1

How you do the urls depends on a little on how your widgets are actually going to work.

Mark Biek
I edited the question to make it clearer.
Corey Trager
A: 

It would probably be easier to view the file structure if it was /widget/edit.php or /widget/new.php

This way you can redirect people to other modules by just knowing the module name and what action they want to do.

Also that will allow you to implement url rewriting a little easier so it would look like widget/edit/2 once you get an index.php and some rewrite rules in place.

Rob Elsner
+1  A: 

If this is a learning exercise with PHP, then what exactly are your goals? Do you want to implement a full MVC architecture (just for the sake of the exercise)? Do you want to use a Front Controller that routes requests to specific controllers? Or do you want to build a simple one script site that is capable of only of editing your table of widgets?

If it is any of the former then I would recommend that you read the documentation and source code for one of the frameworks (I particularly like Zend Framework) and then implement your own version of MVC and/or Front Controller. Additionally, you can read this article by Rasmus Lerdorf where Rasmus talks about how to build a simple MVC application without using an existing framework.

If you simply want to accomplish the latter (edit your table of widgets), I would recommend that you keep things simple. So I would:

  • Create a file (WidgetsTable.php) with functions that interact directly with the table. For example insert(), update(), delete(), fetchAll(), createNew()
  • Create a file (index.php or widgets.php) that you will interact with directly (in some paradigms this would be the Page Controller). This file will contain functions like createAction(), listAction(), findAction(), showFormAction(). This will be the page to which calls are made and the functions in this page will interact with the functions (or class methods) that you created in the first file.
  • Create a folder called views or templates and create a corresponding view or template file for each action in widgets.php. For example you would create list.php, showForm.php, create.php.

This will ensure that you have the proper separation of concerns and it will make it easier to adjust the overall project structure later if you find you need to.

Good luck with your learning project! :-)

Noah Goodrich
A: 

Have you ever looked at http://www.phpobjectgenerator.com/

It generates really nicely CRUD formatted objects for database tables.

Luca Matteis
OP specifically stated that he does not want to use any third-party tools to actually generate his code.
Noah Goodrich
What? Where is it specified?
Luca Matteis
Code-generation is different from frameworks, other than showing you how the code is structured it's also a great learning tool to understand how to structure your files, exactly what he asked for.
Luca Matteis
A: 

I'm sure that this is probably a matter of taste, but I find that creating 3 files (list, add, edit) for 1 table is flexible enough while keeping it easy to understand, especially for developers new at php.

that way, whenever you need to do CRUD on another table, just create another 3 files for it.

...
includes/
views/
add_member.php
edit_member.php
list_member.php
add_widget.php
edit_widget.php
list_widget.php
...

with includes/ folder storing the config and function files and the views/ folder containing various fragment files to be included, such as header, navigation, footer, etc.

andyk