Cobby Posted August 29, 2007 Share Posted August 29, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/67155-help-with-general-application-design/ Share on other sites More sharing options...
448191 Posted August 29, 2007 Share Posted August 29, 2007 The locations of your files have very little to do with the design of your application. I recommend you start with a simple MVC tutorial. Also, read a little on the theory behind it. You can find links under MVC in the resources sticky. Quote Link to comment https://forums.phpfreaks.com/topic/67155-help-with-general-application-design/#findComment-337123 Share on other sites More sharing options...
Cobby Posted September 3, 2007 Author Share Posted September 3, 2007 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.... Quote Link to comment https://forums.phpfreaks.com/topic/67155-help-with-general-application-design/#findComment-340147 Share on other sites More sharing options...
samtay Posted September 3, 2007 Share Posted September 3, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/67155-help-with-general-application-design/#findComment-340652 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.