Jump to content

How do you handle page conent both modularly and dynamically?


c4onastick

Recommended Posts

Hello everyone,

 

It has been a little while but I'm back to programming PHP.

 

I've been working on a few "fun" projects and my "hammer" solution for displaying page content is what I'd like to improve upon. Usually I find myself writing a class to display the page, it simply prints all the required bits and bobs in the correct order in the desired format. What changes is the content in the central div. Since all these sites are database driven there are numerous db calls to create the content that's displayed in the content div. Previously, I'd always written a monstrous case block:

if($_GET['page'] == 'home'){
   //home content here
}elseif($_GET['page'] == 'form'){
   //db calls and other content here
}elseif(...
...
}
return $content;

That way I could use one "page" (index.php) to display all the content from. How do people usually handle getting content like this to the page that displays it?

 

Any links or suggestions would be great! Thanks in advance!

Link to comment
Share on other sites

If you're hell bent on handling all incoming requests from that single page here's one solution.

 

1) Take each case in your switch and move it to its own file, so you'll have files such as home.php, form.php, etc.

2) Create a hash such as:

$hash = array("page" => "page.php", "form" => "form.php"); // etc

3) Remove the switch and replace it with something roughly equivalent to the following:

if (isset($hash[$_GET["page"])) {
    require $hash[$_GET["page"]];
else
    // handle the page not found error

 

This would be a small first step towards improving your design.  You would register your pages with the hash and let that drive the switch.  We haven't even attempted to use objects at this point, or other techniques such as reflection.  If you'd like to pursue those avenues let me know and I can keep going.

Link to comment
Share on other sites

Thanks for the quick reply!

 

If you're hell bent on handling all incoming requests from that single page here's one solution.

Actually I'm not really hell bent on that. I would, however, like to minimize the upkeep effort (since I almost always end up maintaining them all anyway), make it as modular as possible so as facilitate easy expansion (since the I never quite know what I want the first time around), and expand my programming knowledge by applying some good design principles (always looking to learn something new to do it better!).

 

This would be a small first step towards improving your design.  You would register your pages with the hash and let that drive the switch.  We haven't even attempted to use objects at this point, or other techniques such as reflection.  If you'd like to pursue those avenues let me know and I can keep going.

I had thought of using a hash in that manner and at this point its beyond me why I decided against it. Reflection? Please do continue, if you wouldn't mind, I'm just getting off the ground with OO concepts any chance I get to apply them is great!

 

Thanks again!

Link to comment
Share on other sites

Alright, I'll go on a little.

 

1) Why don't you make a page controller out of each of the cases in your switch?  The class could have a single public method "execute" which contains the case's contents.  You already have the code separated is creating an individual file/class for each block going to be harder to maintain?  Won't it be conceptually clearer?

2) After creating page controllers you should have a rough idea how much of the code is repeating between the various classes.  Once you have identified it try to create a method to do the work, and call the method in the class.  Then move the method to a base class (you might call it Page_Controller) and have the various classes you've made extend from it.  This will make it easier to make changes in the future.

3) Once you have all of your "pages" encapsulated in a page controller class you can then either make a factory or use reflection to get new instances of each page.  Both would work in roughly the same way; you would call a method and pass some string and any arguments, it would return a new page controller on which you would call execute.

 

Once you've done that I'm sure you'll find that there is lots of common functionality between all your pages.  For example, you might be able to consolidate all the HTML generation into a new class and delegate the UI generation responsibilities to that class.  Likewise all the queries you're making to the database can probably be moved into some class, again delegating responsibility for interacting with the database.  This would leave the page controllers to worry about taking input and shuttling it to the correct place for output.  I believe that would be a step in the right direction for maintainability, and if you ever wanted to add a new page you would simply extend the base class, throw in any of the new helper classes you've created, and be on your way.

 

If you'd like to talk about actual implementation details for reflection, factories, MVC, etc feel free to start a post on whatever forum is meant for that discussion, and if you alert me I'll go post some code there.

Link to comment
Share on other sites

Thanks for your insight.

 

1) Multiple files/pages actually would be easier to maintain, and class inheritance would minimize most of my upkeep time. I do like the concept too.

2) I currently have a setup sort of like that. Currently I have an invocation roughly following this:

<?php
$page = new page_class();
$content = new page_content();
...
$page->content = $content->get_content();

$page->display();
?>

I just have a attribute of page_class that holds the place where the content is to go. I then use the page_content class to fill page_class' content variable. Two completely separate objects linked by one assignment.

3) This is the place where I would like to get to. I understand sub-classing the pages to reflect their individual content, but I'm not sure how to do it dynamically through a "factory" or via "reflection".

 

I would love it you could point me in the right direction for getting a factory or some reflection techniques working.

 

I've started a thread here where we can continue this discussion, thanks again for your help!

Link to comment
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.