Grow your CSS skills. Land your dream job.

PHP Event-driven design pattern like ASP.net?

  • # December 30, 2012 at 3:58 pm

    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.

    Any help is greatly appreciated!

    Ted

    # January 4, 2013 at 5:13 pm

    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:

    http://www.phpdesignpatterns.com/
    http://shop.oreilly.com/product/0636920028062.do

    You might also want to look at some existing PHP frameworks to see how they handle events. Here are a couple to check out:

    http://framework.zend.com/
    http://kohanaframework.org/

    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:

    $rows = SomeMagicFunctionToGetTableData();
    $table = new Table();
    $table->id = ‘mytable';
    foreach($rows as $rowData) {
    $row = $table->addRow();
    $row->addCell($rowData->id);
    $row->addCell(new Button(‘Delete Row’));
    }

    $tableDelegate = new Delegate(‘mytable’);

    In order to capture the click event, we use event delegation on the page. Any time the page is clicked on we validate that click against a list of registered Delegates. If it originates within the passed context we let that Delegate act on the action. In this example the `$tableDelegate` will render a javascript object with the context of ‘#mytable’. So any click that originates on the table will be handled by the $tableDelegate.

    This is too big a topic to cover in just a comment, but I hope that I have pointed you in the right direction.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".