Jump to content

Help with general application design


Cobby

Recommended Posts

Hi all,

 

I had a look at the stickied post, but there was too many links, and I wasn't exactly sure which one I should read (I looked at a few, but didn't find what I was looking for).

 

I know procedural and OO PHP, and I know how to use smarty. But my question is, how do I design an application? What I have just assumed and am doing for my current project (a simply EMS), is have this sort of layout:

 

-config.php  <-- stores application wide variables, also creates $smarty = new Smarty;

-includes.php <-- class that stores all my functions, i.e login, logout, viewProfile. Also includes config.php

-header.tpl.html <-- self-explainitory, html header file

-footer.tpl.html <-- html footer

-<include>.tpl.html <-- file to be display between header and footer

 

Directory Structure:

{root}/ <--stores logic pages index.php, login.php, logout.php, profile.php, etc.

/config/ <--stores config files (currently only config.php)

/inc/ <--stores include files (currently only includes.php)

/templates/ <--template files for smarty

/templates_c/ <--template cache

/libs/ <--smarty directorty

 

Is that how I should lay my applications out?

I would also appreciate if someone could direct my to a tutorial that will explain my query.

 

Cheers,

Cobby

Link to comment
Share on other sites

Thanks for that :)

 

I spent half the weekend understand how it works, and spent the other half making my own framework, I want one that didn't have to store page in class and could implement smarty a little easier. I also intergrated the registry class the implements ArrayAccess. It goes like this:

 

Directory Layout:

/classes

/includes

/processors

/templates

/templates_c

 

DB Layout:

`pages` table:

By default has 3 fields:

pid - Page ID

title - the title of the page

filename - has the name of the file wthout extension (because all will .php)

 

Firstly, the include.php (from /includes folder)

<?php

// Set error reporting
error_reporting(E_ALL);

// get url function
function url(){
$sp = $_SERVER['SERVER_PROTOCOL'];

$sp = explode ('/', $sp);

$protocol = strtolower($sp[0]);

// set server with http
$url = $protocol."://".$_SERVER['SERVER_NAME'];

// get the script path
$path = $_SERVER['PHP_SELF'];

// separate path by /
$path = explode('/', $path);

$final = '';
// join parts, start at 1 to remove first blank and end 2 early to skip includes and filename name
for($i=1; $i<count($path)-2; $i++){
	$final .= "/".$path[$i];
}

// add trailing /
$final .= "/";

// join parts
$ret = $url.$final;
return $ret;

}

// Set Constants
if(!defined('DIRSEP')){
define('DIRSEP', DIRECTORY_SEPARATOR);
}

$site_path = realpath(dirname(__FILE__) . DIRSEP . '..' . DIRSEP) . DIRSEP;
if(!defined('site_path')){
define('site_path', $site_path);
}


if(!defined('site_url')){
define('site_url', url());
}
include 'classes/reg.class.php';
$reg = new reg;

$scandir = scandir(site_path."classes\\");
// Load classes
foreach($scandir as $val){

if(preg_match("+.class.php+", $val)){

	$val = substr($val, 0 ,-10);

	if(($val != 'Smarty_Compiler') && ($val != 'Config_File') && ($val != 'reg')){

		include_once site_path."classes\\".$val.".class.php";

		$reg[$val] = new $val($reg);

	}

}

}


?>

 

My real master peice in that was the foreach loop. Instead of using __autoload like in the MVC tutorial, it loads every file that has .class.php extension. (except for Smarty_Compiler, Config_File and reg). That way in index, I don't have to declare each class (becuase there will be a lot!). I manually declared the reg class so I could automatically create a registry variable for each class (i.e. $reg['db'] for the db class). Also, everyclass has a handler function which loads the reg class.

 

Now, the only file in the root directory is index.php which goes like this:

<?php

session_start();

// Include include.php
require_once'includes/include.php';

// Connect to database
$reg['db']->dbConnect('localhost', 'root', '<password>', 'sneaky');

// Run page() function
$reg['handler']->page();

?>

 

Index.php is pretty self explanitory, starts a session, connects to database (everypage needs a database connection). And runs the page() function from the handler class.

 

And of course, the class that make everything happen, handler:

<?php

class handler{

protected $reg;

function handler($reg){
	$this->reg = $reg;
}

function page(){
	// Lazy variable
	$reg = $this->reg;
	// Get and refine page
	$page = (isset($_GET['page']) == true)?$_GET['page']:'index';
	$page = strtolower($page);

	// MySQL query
	$sql = "SELECT * FROM
			`page`
			WHERE filename = '".mysql_escape_string($page)."'";

	// Set fetch array variable
	$row = $reg['db']->dbFetchArray($sql);

	$file = site_path.DIRSEP."processors".DIRSEP.$row['filename'].".php";

	$reg['smarty']->assign('title', $row['title']);

	$reg['smarty']->display('header.tpl');
	if(is_file($file) == true){
		include $file;
	}else{
		echo 'ERROR 404: File not found';
	}
	$reg['smarty']->display('footer.tpl');

	$reg['db']->dbclose();

}
}

?>

The page() function will look for a GET variable called page, and will query the DB for a page with the same filename. It also automatically smarty->display's the header and footer templates. If it doesn't find a file, it echoes 404 (i didn't use die becuase then it doesn't display the footer). Last night, I also setup mod_rewrite so instead of having /index.php?page=example it looks like /example.php.

 

So now the real example page would look like this:

 

<?php

$reg['smarty']->assign('examplevar', 'This is an example');
$reg['samrty']->register_function('multi', 'multi');

function multi($params){
return $params['num1'] * $params['num2'];
}

$sql = "SELECT * FROM `users` WHERE `username` = '".mysql_escape_string($_GET['username'])."'";

$row = $reg['db']->dbFetchArray($sql);

$reg['smarty']->assign('username', $row['username']);

$reg['smarty']->display('example.tpl');

?>

 

I haven't really polished yet, so if anyone has any ideas, please let me know!

 

Cheers,

Cobby

 

EDIT: Sorry the code looks bad, it hasn't remembered the tabs? It can see them when I edit....

Link to comment
Share on other sites

I have been looking for a tutorial like that for a while now but I been looking for a while.

 

You should try using 4 spaces instead, I think it is more acceptable to use spaces as the layout will be intact on any editor you wish to use. I could be wrong on that, can anyone confirm?

 

Cheers

Sam

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.