Jump to content

Stefano

Members
  • Posts

    10
  • Joined

  • Last visited

    Never

Everything posted by Stefano

  1. The code is getting a bit messy, i gotta clean it up, also there's almost no error check or validation so i'll need to build an Error class to manage that... and... the layout system is really stupid atm, well, i'll change a few things later today, for now this is the code: public_html/index.php <?php define("O_FRAMEWORK", "Bla bla"); define("APP_PATH", __DIR__ . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "application" . DIRECTORY_SEPARATOR); require_once(APP_PATH . "Omega.php"); $routes = array( array("/", "index", "index"), array("/about", "about", "index"), ); new Omega($routes); ?> application/Omega.php <?php if(!defined("O_FRAMEWORK")){ echo "Direct file access is not allowed"; exit();} class Omega { private $_uri; private $_routes; private $_route; public function __construct($routes) { $this->_uri = $_SERVER['REQUEST_URI']; $this->_routes = $routes; if(!$this->_isUriValid()){echo "404";exit();} $controller_name = $this->_route[1] . "Controller"; $method_name = $this->_route[2] . "Action"; require_once(APP_PATH . "controllers" . DIRECTORY_SEPARATOR . $controller_name . ".php"); $controller = new $controller_name(); $controller->$method_name(); } private function _isUriValid() { foreach($this->_routes as $route){ if($route[0] == $this->_uri){ $this->_route = $route; return true; } } return false; } } ?> application/controllers/indexController.php <?php if(!defined("O_FRAMEWORK")){ echo "Direct file access is not allowed"; exit();} require_once(APP_PATH . "Controller.php"); class indexController extends Controller { public function __construct(){} public function indexAction() { $this->_loadView("index"); } } ?> application/controllers/aboutController.php <?php if(!defined("O_FRAMEWORK")){ echo "Direct file access is not allowed"; exit();} require_once(APP_PATH . "Controller.php"); class aboutController extends Controller { public function __construct(){} public function indexAction() { $this->_loadView("index"); } } ?> application/Controller.php <?php if(!defined("O_FRAMEWORK")){ echo "Direct file access is not allowed"; exit();} class Controller { public function __construct(){} protected function _loadView($action) { //Remove the "Controller" part of the name $controller_name = str_replace("Controller", "", get_class($this)); //Here we include the top part, then the content and then the bottom part to get the full page... //I think there are better ways to do this >_> //Also there's no error checking, that's because i didn't make an Error class yet require_once(APP_PATH . "layout" . DIRECTORY_SEPARATOR ."top.phtml"); //-> require_once(APP_PATH . "views" . DIRECTORY_SEPARATOR . $controller_name . DIRECTORY_SEPARATOR . $action . "View.phtml"); //-> require_once(APP_PATH . "layout" . DIRECTORY_SEPARATOR ."bottom.phtml"); } protected function _loadModel($model) { require_once(APP_PATH . "models" . DIRECTORY_SEPARATOR . $model . "Model.php"); } } ?> application/layout/top.phtml <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Title</title> </head> <body> <div id = "header"> <h1>Omega Framework</h1> </div> <div id = "nav"> <ul> <li>Homepage</li> <li>About</li> </ul> </div> <div id = "content"> application/layout/bottom.phtml </div> <div id = "footer"> <p>Footer</p> </div> </body> </html> application/views/about/indexView.phtml <h2>About page</h2> <p>Hmmm what?</p> application/views/index/indexView.phtml <h2>Index Page</h2> <p>This is the index page!</p> public_html/.htaccess RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule !\.(js|ico|gif|jpg|png|css)$ /index.php Don't use this code for a real project, it sucks! I'll update it when i can
  2. Ok, I rewrote it, and now is far simpler... thanks, i'll update the first post EDIT: looks like i can't edit the first post, well i'll post it here: index.php <?php define("O_FRAMEWORK", "Bla bla"); define("APP_PATH", __DIR__ . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "application" . DIRECTORY_SEPARATOR); require_once(APP_PATH . "Omega.php"); $routes = array( array("/", "index", "index"), array("/about", "about", "index"), ); new Omega($routes); ?> Omega.php <?php if(!defined("O_FRAMEWORK")){ echo "Direct file access is not allowed"; exit();} class Omega { private $_uri; private $_routes; private $_route; public function __construct($routes) { $this->_uri = $_SERVER['REQUEST_URI']; $this->_routes = $routes; if(!$this->_isUriValid()){echo "404";exit();} $controller_name = $this->_route[1] . "Controller"; $method_name = $this->_route[2] . "Action"; require_once(APP_PATH . "controllers" . DIRECTORY_SEPARATOR . $controller_name . ".php"); $controller = new $controller_name(); $controller->$method_name(); } private function _isUriValid() { foreach($this->_routes as $route){ if($route[0] == $this->_uri){ $this->_route = $route; return true; } } return false; } } ?> indexController.php <?php if(!defined("O_FRAMEWORK")){ echo "Direct file access is not allowed"; exit();} //Will extend Controller class indexController { public function __construct(){} public function indexAction() { echo "Index method of IndexController"; } } ?> aboutController.php <?php if(!defined("O_FRAMEWORK")){ echo "Direct file access is not allowed"; exit();} //Will extend Controller class aboutController { public function __construct(){} public function indexAction() { echo "Index method of aboutController"; } } ?> I'll create the parent class Controller with a few things right now. What do you think?
  3. Because it's fun! and also we can do better EDIT: Also.. if people will like this framework they can use it, but hm... yea maybe there are too many frameworks like this around, what if i do it without OOP? do you think it would be useful to someone? Someone who may wants the fastest framework he can get...
  4. No questions, this is the continuation to http://www.phpfreaks.com/forums/application-design/mvc-and-speed/ What do you think about the code?
  5. Ok guys i made a topic with the code: http://www.phpfreaks.com/forums/php-coding-help/omega-framework/ It currently sucks, but i'll work on it! Obviously feel free to give suggestions and/or rewrite some of the code, <3
  6. Lol! Ok let's see... index.php <?php /* index.php this file will be under /var/www/site/public_html or some other public dir */ //this will look into ../application //you can do something like /var/www/site/application if you want define("APP_PATH", __DIR__ . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "application" . DIRECTORY_SEPARATOR); define("LIB_PATH", APP_PATH . "lib" . DIRECTORY_SEPARATOR); //This is because we don't want direct access to files, we'll be checking in every private file //if this is defined define("O_FRAMEWORK", "Bla bla bla"); //Had to make theese variable because i had some problems with the require_once when using constants + strings, i'll fix this soon $config_path = APP_PATH . "config.php"; $router_path = LIB_PATH . "Router.php"; $front_path = LIB_PATH . "Front.php"; require_once($config_path); require_once($front_path); require_once($router_path); if(isset($autoload)){ if(!autoload($autoload)){ echo "zzz"; exit(); //Errors when trying to open files... } } $router = new Router(); if($router->isRouteValid() == false){ echo "404"; exit(); //Didn't find route } $front = new Front(); $front->servePage($router->getRoutePath()); //This part of code should probably be optimized... function autoload($autoload) { for($i = 0; $i < sizeof($autoload); $i++){ if(!file_exists(APP_PATH . $autoload[$i] . ".php")) return false; require_once($autoload[$i] . ".php"); } return true; } ?> Front.php <?php /* Front.php Ok hm... this is very uncomplete :/ */ if(!defined("O_FRAMEWORK")){ echo "Direct file access forbidden"; exit();} define("CONTROLLER_PATH", APP_PATH . "controllers" . DIRECTORY_SEPARATOR); class Front { public function __construct(){} public function servePage($routePath) { if(strpos("/", $routePath)){ //Will do this later } else { $controller_name = $routePath; $method = "index"; } require_once(CONTROLLER_PATH . $controller_name . ".php"); $controller = new $controller_name(); $controller->$method(); } } ?> Router.php <?php /* Router.php */ if(!defined("O_FRAMEWORK")){ echo "Direct file access forbidden"; exit();} class Router { private $_route; private $_available_routes = array ( "Homepage" => "/", ); private $_route_path; public function __construct() { $this->_route = $_SERVER["REQUEST_URI"]; } public function getRoute() { return $this->_route; } public function getRoutePath() { return $this->_route_path; } public function isRouteValid() { foreach($this->_available_routes as $route){ if($this->_route == $route){ $this->_route_path = array_keys($this->_available_routes, $route, true); $this->_route_path = $this->_route_path[0]; return true; } } return false; } } ?> config.php <?php //This is empty atm ?> Homepage.php (this is the home controller) <?php if(!defined("O_FRAMEWORK")){ echo "Direct file access forbidden"; exit();} class Homepage { public function __construct(){} public function index() { echo "Homepage"; } } ?> This is what i got, it's all messed up but i'll fix it soon
  7. Thanks for your help, I was thinking about cache and I think i will build a database query cache system, so that you execute queries only once an hour or something like that, because query results would be cached i think that would be pretty cool, also i could cache anything else basically... for the htaccess, probably i'm too worried about that, and hm... for url routing i'll test a few things and i'll update you Still... OOP looks kinda slow to me, and PHP has a garbage collector, so it's even worse speed-wise. But yea... i'll probably use it anyway, takes too much time to write and maintain procedural code. And as i said, i'll be using cache so i'll be fine with OOP. I think i'll just post some code later, so we can optimize it, if you are nerd enough
  8. I know that .htaccess overrides the server configuration but it's good to remove it and instead edit the server configuration file, i heard it's faster that way. My application is not slow, but i think it would be very slow if i had a lot of users online, and hm yea i think i tend to over complicate things too
  9. Thank you for your reply, I can surely optimize routing, remove the htaccess and let the server handle that but is the design wrong? OOP in general, it's clean and maintainable but also much slower than procedural... but i'm not sure if i'll be able to handle a big project using procedural programming... it's really hard to choose... Edit: what i really wanted to ask is: if you gotta write Youtube, what would you use? OOP+MVC? Procedural? i'm not writing youtube but it's just an example, i want to make the fastest possible website wich is also possible to maintain... hard choice
  10. Hi everyone, I just joined this forum and I hope you can help me with my problem. I've been messing around with php for a while, building websites with frameworks and recently i decided to write my own framework. I got it done pretty quickly and it works fine but i'm starting to think that maybe MVC isn't the best design choice for a webapp. My main concern is speed, my code right now is quite clean and mantainable but there are a few problems. I redirect every request to index.php using an .htaccess file (and i think that slows down my site) and also i'm using url routing, so for each request to the server my code will loop over all the regexps and check if he can find a match... Oh and... then i load the controller wich loads the models and view etc.. etc.. I think all those operations slow down the website a lot and I would like to know if you have any solutions to my problem, my english is pretty bad I know, I hope you'll understand what i wrote anyway Edit: I'm also considering the idea of writing only procedural php from now on, i want superfast websites. The code will probably be hard to mantain but as i said, all i want is speed, what do you think?
×
×
  • 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.