PHP Frontpage/Page controller

Posted by atno on Stack Overflow See other posts from Stack Overflow or by atno
Published on 2011-01-05T02:34:05Z Indexed on 2011/01/05 3:54 UTC
Read the original article Hit count: 238

Filed under:
|

I using the following as Frontpage/Page Controller(s) and it's working ok so far, except two problems I'm facing which as you can see are the $pages array and the switch, which are actually much much longer as the one I've pasted here. Everytime there is a need for a new page controller I have to add it to $pages array and to switch which makes that list very long. How would you overcome this problem and do you see any other improvement on this code? loadLogic() in page controllers it is used to get functions under pages/controllername/logic/function.php.

Frontpage Controller - index.php:

include 'common/common.php';
if(!isset($_GET['p']) OR $_GET['p'] == ''){
    $_GET['p'] = 'home';
    header('Location: index.php?p=home');
}

$pages = array('home','register','login','logout','page1','page2','page3');

$_GET['p'] = trim($_GET['p']);

if(isset($_GET['p'])){
    if(in_array($_GET['p'], $pages)){
        switch ($_GET['p']) {
            case 'home':
                include 'home.php';
                break;
            case 'register':
                include 'register.php';
                break;
            case 'login':
                include 'login.php';
                break;
            case 'logout':
                include 'logout.php';
                break;
            case 'page1':
                include 'page1.php';
                break;
            case 'page2':
                include 'page2.php';
                break;
            case 'page3':
                include 'page3.php';
                break;
        }
    }else{
        echo '404!';
    }
}

Page Controller - {home,register,login,logout,page1,page2,page3}.php:

include 'tpl/common/header.php';
contentStart();

if(isset($_SESSION['logged'])){
    loadLogic('dashboard');

}else{
    loadLogic('nologin');

}


//Display login form in logic page instead links
//
if(!isset($_SESSION['logged'])){
    contentEnd();
    loadLogic('nologinForm');
}else{
  contentEnd();
  include'tpl/common/rcol.php';

}
include 'tpl/common/footer.php';

function loadLogic():

function loadLogic($logic) {
    $path = dirname(__DIR__) . '/pages';

    $controller = preg_split('/&/',$_SERVER['QUERY_STRING']);
    $controller = trim($controller[0],"p=");
    $logicPath = 'logic';
    $logic = $logic . '.php';
    $err = 0;
    $logicFullPath = $path.'/'.$controller.'/'.$logicPath.'/'.$logic;

    if($err == '0'){
        include "$logicFullPath";

    }
}

Folder Structure:

projectName
  |
   ---> common
  |
   ---> pages
  |   |
  |    --->home
  |   |
  |    --->register
  |   |
  |    --->login
  |   |
  |    --->logout
  |   |
  |    --->page1
  |   |
  |    --->page2
  |   |
  |    --->page3
  |
   ---> tpl
  |   |
  |    ---> common
  |
   --> home.php
  |
   --> register.php
  |
   --> login.php
  |
   --> logout.php
  |
   --> page1.php
  |
   --> page2.php
  |
   --> page3.php

© Stack Overflow or respective owner

Related posts about php

Related posts about software-design