Hey all, I’m still fairly new to PHP (less than a year or so) but have many years experience in an ASP.Net environment. So far, I’ve been able to migrate most of my core apps from .net to a php platform and overall am enjoying the different platform. I’ve moved from a windows OS to a Mac environment so PHP is naturally they way I’d like to continue developing going forward.
I’ve got the basics all working, but am still somewhat flummoxed on the “correct” (or rather sensible) approach to handling lists of content on pages.
In .Net, i’d simply tie a button (say edit or delete) to a row and handle the code when that event and row index is triggered.
However in PHP, I find I’m having to go way back to my old ASP days which feels like I’m missing out on some newer approaches as found in CMS’s like WordPress. So far, my solution for list based content is to render the list to the page, and include my edit/delete buttons as hyperlinks for each row that submit parameters to a handler file via HTTP GET (i.e. delete.php?id=1) and then redirect to the current page once the database changes are complete.
The other alternative would be to have a whole bunch of individually named FORM objects, but that feels really awkward as well.
I’ve augmented this to integrate the ‘handler’ code on the same code that renders the list, which seems like a good halfway solutions and simplifies my maintenance, but would really appreciate some guidance on how others have ‘best tackled’ this in their solutions.
So far, my searches online haven’t revealed any good explanations or examples ; at least that don’t expect expert-level understanding of PHP.
You’ve touched on the good and the bad of PHP. PHP is an extremely flexible language which allows you to decide which design pattern you implement on the server side. Having a delete.php file which handles your deletes, is just as valid (but not recommended) as having an API which executes DELETE requests.
I would recommend that you read up on some common design patterns, then implement the one that works best for your project. Here are a couple of resources to get you started:
The company I work for decided to create their own custom framework, which implements HTML tags as PHP objects. We use these objects as a simple templating mechanism, and we implement the Delegation design pattern to handle the event on the client side. To build a table with row management buttons would look like this in our framework:
This is too big a topic to cover in just a comment, but I hope that I have pointed you in the right direction.