Pages management: apage.php VS index.php?p=apage

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply

Pages management: apage.php VS index.php?p=apage

Full PHP files for every page.
0
No votes
One full page, included contents.
3
100%
 
Total votes: 3

User avatar
lorenzo-s
Forum Commoner
Posts: 43
Joined: Tue Aug 25, 2009 12:25 pm

Pages management: apage.php VS index.php?p=apage

Post by lorenzo-s »

Hi everyone. I want to ask you how do you manage website pages in your (large) project. Let's suppose we are creating a simple photography community.


Creating full PHP files for every page.
I have a folder structure like this:

Code: Select all

/includes
    /head.php
    /menu.php
photo.php
userprofile.php
index.php
Each page has it's own full PHP file. For example, userprofile.php:

Code: Select all

<html>
<head><?php include 'includes/head.php' ?></head>
<body> 
    <?include 'includes/menu.php' ?>
    Page contents.
</body>
</html>
So, if I want a user page the URL is: example.com/userprofile.php?user=123


One full page, included contents.
I have a folder structure like this:

Code: Select all

/pages
    /photo.php
    /userprofile.php
index.php
The index.php is something like:

Code: Select all

<html>
<head>Title, meta, CSS...</head>
<body> 
    <div id="menu">The common menu</div>
    <?php include "pages/$page.php"; ?>
</body>
</html>
Where $page is get from URL and validated before.
So, if I want a user page the URL is: example.com/index.php?p=userprofile&user=123


I know there are lots of things that can affect my two scenarios (URL rewriting, templating, etc), but I think the problem can always be traced back to these simple examples.

I think each method has got pros and cons (CSS and JS inclusion, code maintenance, etc).
What do you think? Which do you use?
Last edited by lorenzo-s on Thu Jan 20, 2011 1:52 pm, edited 1 time in total.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Pages management: page1.php VS index.php?p=page1

Post by Jonah Bron »

The best way is to organize your pages in a MVC model. Frameworks make this very easy, there's lots of options:

http://www.google.com/search?q=php+framework
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Pages management: page1.php VS index.php?p=page1

Post by alex.barylski »

The solution you seek is called pagination. Its done dynamically, no need for multiple physical pages, if I understood you correctly.

Cheers,
Alex
User avatar
lorenzo-s
Forum Commoner
Posts: 43
Joined: Tue Aug 25, 2009 12:25 pm

Re: Pages management: page1.php VS index.php?p=page1

Post by lorenzo-s »

Jonah Bron wrote:The best way is to organize your pages in a MVC model.
I know there are lots of PHP MVC frameworks, but I do not like them. Too much complexity for small project imho. I prefer to keep simplicity and use my own set of classes (that can be a sort of tiny framework, in fact, but not MVC).
alex.barylski wrote:The solution you seek is called pagination. Its done dynamically, no need for multiple physical pages, if I understood you correctly.
I think you just stop reading my post after title. :mrgreen: I edit title now to avoid misunderstandings.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Pages management: apage.php VS index.php?p=apage

Post by John Cartwright »

Do some research on Page Controllers (each file is own access point) vs Front Controllers (single access point).

Really, having a single entry point into your application lets you streamline lots of processes, and keeps things simple. You don't need to implement the entire MVC structure, but a Front Controller is an excellent step in the right direction and you will slowly find yourself wanting an MVC structure the more you experience and learn.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Pages management: apage.php VS index.php?p=apage

Post by Jonah Bron »

User avatar
lorenzo-s
Forum Commoner
Posts: 43
Joined: Tue Aug 25, 2009 12:25 pm

Re: Pages management: apage.php VS index.php?p=apage

Post by lorenzo-s »

John Cartwright wrote:Do some research on Page Controllers (each file is own access point) vs Front Controllers (single access point).
Really, having a single entry point into your application lets you streamline lots of processes, and keeps things simple.
That's exactly the type of suggestions I need. I will read about these pattern. And thank you Jonah for the links.
John Cartwright wrote:You don't need to implement the entire MVC structure, but a Front Controller is an excellent step in the right direction and you will slowly find yourself wanting an MVC structure the more you experience and learn.
Yes, the fact is that I do not want to adopt an existing MVC PHP framework, simply because I prefer to work on something that I can really understand and that can reflect my "way to see things". And right now a MVC framework is "too much" for me, so I do not want to get into it.

At the moment I have a simpler approach. I built (as time goes by) a simple library that handles the "Model part" with the Active record pattern. The rest is done by auxiliary classes, PHP simple pages and Smarty template system. Maybe a day it will became a MVC, why not...
User avatar
lorenzo-s
Forum Commoner
Posts: 43
Joined: Tue Aug 25, 2009 12:25 pm

Re: Pages management: apage.php VS index.php?p=apage

Post by lorenzo-s »

What do you think about this approach? It uses a class to manage main document, and Smarty as template engine.

index.php

Code: Select all

MainDocument::init($smarty_instance); // This creates singleton instance
include 'pages/content.php'; // This comes after a $_GET['page'] parsing...
MainDocument::get()->display();
pages/content.php

Code: Select all

MainDocument::get()->setTitle('My content page');
MainDocument::get()->addCss('css/content.css');
MainDocument::get()->setContentTemplate('templates/content.tpl');
// And then PHP code and Smarty assignments...
And the MainDocument class:

Code: Select all

class MainDocument {

	// [...] Singleton stuffs: init(), get(), private __construct()...

	protected $smarty;

	protected $cssLinks = array();
	protected $jsScripts = array();
	protected $title = array();

	protected $template = 'templates/index.tpl';
	protected $contentTemplate = null;

	public setTitle($title) { ... }
	public addCss($file) { ... }

	public setContentTemplate($file) {
		$this->contentTemplate = $file;
	}

	public display() {
	
		// Assign title, css, etc to Smarty instance
		$this->smarty->assign(...);

		// Setup content template and display page
		$this->smarty->assign('contentTemplate', $this->contentTemplate);
		$this->smarty->display($this->template);

	}

}
Finally, index.tpl use Smarty's {include} tag:

Code: Select all

<!-- DOCTYPE, head, title, css links... -->
<body>
	<div id="header"><h1>My website</h1></div>
	{include file=$contentTemplate}
</body>
Post Reply