Page 1 of 1
Pages management: apage.php VS index.php?p=apage
Posted: Thu Jan 20, 2011 8:29 am
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?
Re: Pages management: page1.php VS index.php?p=page1
Posted: Thu Jan 20, 2011 11:07 am
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
Re: Pages management: page1.php VS index.php?p=page1
Posted: Thu Jan 20, 2011 12:10 pm
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
Re: Pages management: page1.php VS index.php?p=page1
Posted: Thu Jan 20, 2011 1:51 pm
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.

I edit title now to avoid misunderstandings.
Re: Pages management: apage.php VS index.php?p=apage
Posted: Thu Jan 20, 2011 2:56 pm
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.
Re: Pages management: apage.php VS index.php?p=apage
Posted: Thu Jan 20, 2011 3:11 pm
by Jonah Bron
Re: Pages management: apage.php VS index.php?p=apage
Posted: Thu Jan 20, 2011 5:06 pm
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...
Re: Pages management: apage.php VS index.php?p=apage
Posted: Mon Jan 24, 2011 6:54 am
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>