Reputation:
So, I've a little system which generates pretty permalinks using htaccess.
PHP side of the things I handle in the following manner:
if (!$_GET) {
//
$page = "home";
$page_title = "General Site Title";
include ("template/index.php");
//
} else if ($_GET['page'] == "profile") {
//
if ($_GET['subpage']) {
//
header("HTTP/1.0 404 Not Found");
include ("template/404.php");
exit();
//
} else {
//
$page = "profile";
$page_title = "User Profile";
include ("template/profile.php");
//
}
//
} else if ($_GET['page'] == "words") {
//
if ($_GET['subpage']) {
//
header("HTTP/1.0 404 Not Found");
include ("template/404.php");
exit();
//
} else {
//
$page = "words";
$page_title = "Words Page";
include ("template/words.php");
}
//
} else if ($_GET['page'] == "contacts") {
//
if ($_GET['subpage']) {
//
header("HTTP/1.0 404 Not Found");
include ("template/404.php");
exit();
//
} else {
//
$page = "contacts";
$page_title = "User Contacts";
include ("template/contacts.php");
//
}
//
} else if ($_GET['page'] == "about") {
//
if ($_GET['subpage']) {
//
header("HTTP/1.0 404 Not Found");
include ("template/404.php");
exit();
//
} else {
//
$page = "about";
$page_title = "About Page";
include ("template/about.php");
//
}
//
} else if ($_GET['page'] == "verify") {
//
//
} else {
//
header("HTTP/1.0 404 Not Found");
include ("template/404.php");
exit();
//
}
As you can see there is allot of repeating code. How would you go about making this admin panel friendly? What I want to do is have an array of pages with data that will generate this if else statement. Is it possible?
Ideally something like this:
array("page" => "home", "page_title" => "blah", "template" => "index.php", "has_subpage" => false);
or should I stick to the way I'm doing things atm?
Upvotes: 0
Views: 172
Reputation: 781726
Use the following structure:
$pages = array('profile' => array('title' => 'User Profile',
'include' => 'template/profile.php'),
'words' => array('title' => 'Words Page',
'include' => 'template/words.php'),
...);
// Set defaults if no parameters supplied
$page = "home";
$page_title = "General Site Title";
$include_file = "template/index.php";
// Override from parameters
if (isset($_GET['page'], $pages[$_GET['page']])) {
if ($_GET['subpage']) {
header("HTTP/1.0 404 Not Found");
include ("template/404.php");
exit();
} else {
$page = $_GET['page'];
$page_title = $pages[$page]['title'];
$include_file = $pages[$page]['template'];
}
}
include ($include_file);
Upvotes: 0
Reputation: 32272
Something like:
$pages = array(
'words' => array('title' => 'Words', 'file' => 'includes/words.php'),
'contact' => array('title' => 'Contact', 'file' => 'includes/contact.php'),
'profile' => array('title' => 'Profile', 'file' => 'includes/profile.php',
'subpages' => array('subpage1' => array( 'title' => 'foo', 'file' => 'bar'))
),
);
if( isset($_GET['page']) && isset($pages[$_GET['page']]) ) {
$page = $pages[$_GET['page']];
if( ! isset($_GET['subpages']) ) {
$title = $page['title'];
include($page['file']);
} else if( isset($page['subpages']) && isset($page['subpages'][$_GET['subpage']]) ) {
$page = $page['subpages'][$_GET['subpage']];
$title = $page['title'];
include($page['file']);
} else {
// 404 code here
}
} else {
// 404 code here
}
Upvotes: 1
Reputation: 72965
Or something like (warning! untested!)
if (!$_GET) {
$page = "home";
$page_title = "General Site Title";
include ("template/index.php");
}
else if ($_GET)
{
$pages = array('profile' => 'User Profile', 'words' => 'Words Page', 'contacts' => 'User Contacts', 'about' => 'About Page', 'verify' => 'Verify');
foreach ($pages as $page => $page_title)
{
if (($_GET['page'] == $page) && (!$_GET['subpage']))
include ("template/$page.php");
}
}
else
{
header("HTTP/1.0 404 Not Found");
include ("template/404.php");
}
Upvotes: 0
Reputation: 28409
You could use a switch and case but you'd still be left with a lot of duplicate code. Or you could use an array.
$pages=array(
'home'=>array('title'=>'Home', 'has_subpage'=>false),
'profile'=>array('title'=>'User Profile', 'has_subpage'=>false),
'words'=>array('title'=>'Words Page', 'has_subpage'=>false),
'contacts'=>array('title'=>'User Contacts', 'has_subpage'=>false),
// etc
);
// if no page is set, use "home"
if (!isset($_GET['page'])) $_GET['page']='home';
if (array_key_exists($_GET['page'], $pages)){
if ($_GET['subpage']) {
die404();
}else{
$page=$pages[$_GET['page']];
$page_title = $page['title'];
include ("template/".$_GET['page'].".php");
}
}else{
die404();
}
function die404(){
header("HTTP/1.0 404 Not Found");
include ("template/404.php");
exit();
}
Upvotes: 0