Concept of how to work with DooFramework « Learn DooPHP
description
Transcript of Concept of how to work with DooFramework « Learn DooPHP
Learn DooPHPLog in
HomeAboutContactForum
SUBSCRIBE TO RSS
Learn DooPHP: High performance PHP framework
29 Sep 2009
by Milos Kovacki
31 Comments »
Demos & Snippets
Concept of how to work with DooFrameworkOk in this article I will show you how should you work with DooFramework, when I am making website I like tohave one super class that extends DooController class and in constructor of that class I will add all stuff needed for myapplication, so lets make that class, we should name it for example CoreController
001.002.003.004.005.006.007.008.009.010.011.012.013.014.015.016.017.018.019.020.021.022.023.024.025.026.
<?php class CoreController extends DooController /*** Current URL*/ protected static $_currentUrl = null; /*** Instance of Doo::db*/ protected $_db = null; /*** Translator*/protected $_translate = null; /*** Instance of Doo::cache*/ protected $_cache = null;
027.028.029.030.031.032.033.034.035.036.037.038.039.040.041.042.043.044.045.046.047.048.049.050.051.052.053.054.055.056.057.058.059.060.061.062.063.064.065.066.067.068.069.070.071.072.073.074.075.076.077.078.079.080.081.082.083.084.085.086.087.088.089.090.091.092.093.094.095.096.097.098.099.100.101.
/*** Base path of application*/ public $_basePath = null; /*** Instace of DooSession*/public $_session = null; /*** Instance of DooAcl*/ public $_acl = null; /*** Host*/ public $host = null; /*** Js path*/ public $jsPath = null; public function __construct() $this>_basePath = Doo::conf()>ROOT_DIR;// add some globals that we need$this>_db = Doo::db();$this>_view = DooController::view();$this>host = Doo::conf()>host;$this>_view>host = Doo::conf()>host;// ACL$this>_acl = Doo::acl();// add sessions$this>_session = Doo::session("mywebsite");$templateVariables = $this>getSettings();// session$this>_view>_session = $this>_session;$this>_cache = Doo::cache('apc');$this>jsPath = $this>_view>host . 'static/js/'; /*** Before run method*/ public function beforeRun($resource, $action) $this>_view>requestInfo = array("controller" => $resource, "action" => $action); /*** Redirect method*/ public function _redirect($url) header("Location: " . $url); /*** isPost Returns true if method is post** @return boolean*/ public function isPost() if ($_SERVER['REQUEST_METHOD'] == "POST") return true;else return false;
As you can see I have some functions that I usualy use in every development, now our MainController will extend ourCoreController class, our CoreController is located in OUR_APP/protected/controller folder, and one more thing inindex.php we must load CoreController class with:
When we load our CoreController, we can make our MainController class for example:
Add route first:
102.103.104.105.106.107.108.109.110.111.112.113.114.115.116.117.118.119.120.121.122.123.124.125.126.127.128.129.130.131.132.133.134.135.136.137.138.139.140.141.142.143.144.145.146.147.
/*** isGet Returns true if method is get** @return boolean*/ public function isGet() if ($_SERVER['REQUEST_METHOD'] == "GET") return true;else return false; /*** Appends file to header** @param array $data Data for view part* @param string $url Url of the file* @param string $type Type of the file example: "text/javascript"*/ public function appendFile(&$data, $url, $type) switch ($type) case 'text/javascript':$html = '<script type="'.$type.'" src="'.$url.'"></script>';break;case '':break;if (isset($data['scripts'])) $data['scripts'] .= $html; else $data['scripts'] = $html; /*** Gets path to javascript folder** @return string Javascript path*/ public function getJsPath() return $this>jsPath; ?>
1. Doo::loadController('CoreController');
1. $route['*']['/'] = array('MainController', 'index');01.02.03.04.05.06.07.08.09.10.11.
<?php/*** Description of MainController** @author Milos Kovacki*/ class MainController extends CoreController public function index() $this>_session>time = date("Ymd H:i:s");
As you can see we will render template and $data variable that has content in it, that content is our script that will berendered. So this is one way to use template.Now lets make template.php that is located in OUR_APP/protected/viewc folder:
So as you can see in our template script we will include header/footer and our content. Now we must make index.phpthat will be our content in MainController for index action:
So its very simple we defined 2 variables in our controller function, one is stored in session and that is time and other isusername that we assigned in our action. So as you can see its pretty simple, you have your skeleton for makingwebapps One more thing I want to show you as you can see I have one function that is named appendFile I am using it foradding some js from controller for some action where I need some js script, for example if you need some js just onindex action of MainController you can add it trought controller action. I will now show you how is that doable, weneed to add little something to our header.php where we have <head> tag, so in our <head> tag we add:
Now if there are some scritpts in data echo them. Now we use function appendFile:
We do this before we do render, but you already know that. Well thats about it.
Tnx for reading, please ask questions.
0
31 Comments
« Previous Post Next Post »
Zares
12.13.14.15.16.17.18.
$this>_view>username = "John Smith";$data['content'] = 'index';$this>_view>render('template', $data, true); ?>
1.2.3.4.5.
<?phpinclude "header.php";include "$data['content'].php";include "footer.php";?>
1.2.3.4.
<?phpecho "Welcome ".$this>username." at " . $this>_session>time . ' time'; ?>
1. <?php if (isset($data['scripts'])) echo $data['scripts']; ?>
1. $this>appendFile($data, $this>getJsPath() . 'main/index.js', $type = 'text/javascript');
29 Sep, 2009
Milos, my attempt to start the application with placing CoreController.php in a directory of controllers has failed!The error message: DooController not found!…It began to work after I have moved CoreController in protected/class.
Mil0s29 Sep, 2009
Hmm that is very strange, maybe you can provide us with code of your index.php (boostrap), I realy dontsee what could be the problem. But if its working in /class directory then its ok it doesnt realy matter.
Zares29 Sep, 2009
Milos, here detailed results of an application testing.
Version(I)
1. index.php is not modified
2. in common.conf.php added set_include_path($config['SITE_PATH'].’protected/class’);
3. CoreController.php in directory clas
4. in MainController.php added Doo::loadClass(’CoreController’);
Result: OK!
Version(II)
1. in index.php added Doo::loadController(’CoreController’);
2. in common.conf.php deleted set_include_path($config['SITE_PATH'].’protected/class’);
3. CoreController.php in directory controller
4. in MainController.php deleted Doo::loadClass(’CoreController’);
Result: Fatal error: Class ‘DooController’ not found in D:\…\controller\CoreController.php on line 3
Search... Random Join ChatShare
+1
Mil0s29 Sep, 2009
Hmm that is very strange, you just need to add Doo::loadController(’CoreController’); in boostrap and thenit works, since DooController is part of DooFw and its added in Doo class. You dont need to load it onemore time.
Drewish1 Oct, 2009
I really appreciate the work Mil0s, I truly do but I couldnt get this to work either and I have been using DooFramework for a while. I cant figure it out.I did exactly what you said same error as guy aboveif i put the load controller in the main controller i get a page full of errors.
Roman1 Oct, 2009
The redirect() method had better call exit() after sending the header, I’ve found out.
Mil0s1 Oct, 2009
Well, I will post code that works so that will solve all problems, stay tuned I will pack example of app andupload it here.
+1
Chuck5 Oct, 2009
Yeah, I attempted this too with the same result. Also, is this tutorial for the latest SVN trunk? I don’t believeDoo::session is found in the 1.2 version.
Leng5 Oct, 2009
DooSession is not in 1.2. It can be found in the latest trunk
Roman11 Oct, 2009
Milos, I have a couple of questions:
Why do we need to Doo::loadController(’CoreController’)? Why can’t the file that defines MainControllerinclude the file that defines CoreController?
What benefit do you get from using appendFile(), etc. to include JS files as opposed to specifying them directlyin template files?
Milos Kovacki11 Oct, 2009
You can do with include aswell.
Its just my function for adding JS to the some page, well there is no that much benefit but I like to heaveall js and and css done inside
tags.
It’s just simple function to add some js/css in header of document.
+1
stanley20 Oct, 2009
Same error. But it is look like fixed by adding
require_once(Doo::conf()>BASE_PATH .”controller/DooController.php”);
Doo::loadController(’CoreController’);
in index.php script. From my point of view when we try to load CoreController in index.php DooController isnot loaded yet.
Leng20 Oct, 2009
yes indeed, you have to load DooController before loading the children class that extend it if you do it manuallyin index.php
Roman15 Dec, 2009
Manually as opposed to turning on auto loading the framework classes?
Deepak16 Dec, 2009
Hi Milos,What happen if I access the url like http://www.mysitename.com/. In that case MainController, ‘index’ wouldnever we called and intialize those session settings. It that right??
We can also inialize all the core objects in “index.php”.
+1
+1
+1
Milos Kovacki17 Dec, 2009
It would call controller you asigned in routes, and that MainController just extends CoreController so allsettings and session would be initialized.
You just extend your controllers to super controller.
taj9 Feb, 2010
The only way I can load the CoreController in the bootstrap without getting the DooControllernotfounderroris by loading DooController manually first, after I set Doo::conf()>set($config).
Doo::loadCore(’controller/DooController’);Doo::loadController(’CoreController’);
But I guess this is the wrong way of doing it, right?
Milos Kovacki9 Feb, 2010
I just have:Doo::loadController(’CoreController’);
in my boostrap, and everything works ok, my CoreController is located in protected/controller dir, I willmake one demo to show.
Intellidance12 Mar, 2010
Hi@all,
The “Problem” or “Trick” is to aktivate the __autoload element on index.php to autoload all needet classes.If you comment out this function the missed Klass is loadet and you haven`t any error
rd28 Jun, 2010
Hey Milos,what do you mean by ‘in my bootstrap’ can you please specify a specific file/location etc.?
Milos Kovacki7 Jul, 2010
Bootstrap is my index.php in public directory, he is initializing DooFramewrok.That file is just including configs and then:
Doo::app()>route = $route;Doo::app()>run();
Keith Loy25 Jul, 2010
Warning: This is only my second day using DooPHP. Will someone more advanced please double check myinstructions.
I followed Milos Kovacki’s instructions explicitly and ran into several errors. This is how I successfully got thisworking with the version of DooPHP I checked out from svn yesterday.
Error 1: Class ‘DooController’ not foundFile: index.phpReason: autoloading needs to be turned on.Fix: Uncomment function __autoload($classname)
Error 2: Undefined property: DooConfig::ROOT_DIRFile: controllers/CoreController.phpReason: ROOT_DIR appears to have been renamed to BASE_PATHFix: change $this>_basePath = Doo::conf()>ROOT_DIR; to
$this>_basePath = Doo::conf()>BASE_PATH;
Error 3: Undefined property: DooConfig::$hostFile: controllers/CoreController.phpReason: host appears to be renamed APP_URLFix: change $this>host = Doo::conf()>host; to$this>host = Doo::conf()>APP_URL;
Error 4: Undefined property: DooConfig::$host (different then error 3)File: controllers/CoreController.phpReason: host appears to be renamed APP_URLFix: $this>_view>host = Doo::conf()>host; to$this>_view>host = Doo::conf()>APP_URL;
guynamedkeith25 Jul, 2010
I believe I have solved all the issues. I am trying to post my fixes, but am getting server errors. Most errors arestemming from the changes in DooPHP allowing the app path to be in a different place.
guynamedkeith25 Jul, 2010
Warning: This is only my second day using DooPHP.
I followed Milos Kovacki’s instructions explicitly and ran into several errors. This is how I successfully got thisworking with the version of DooPHP I checked out from svn yesterday.
Error 1: Class ‘DooController’ not foundFile: index.phpReason: autoloading needs to be turned on.Fix: Uncomment function __autoload($classname)
Error 2: Undefined property: DooConfig::ROOT_DIRFile: controllers/CoreController.phpReason: ROOT_DIR appears to have been renamed to BASE_PATHFix: change $this>_basePath = Doo::conf()>ROOT_DIR; to$this>_basePath = Doo::conf()>BASE_PATH;
Error 3: Undefined property: DooConfig::$hostFile: controllers/CoreController.phpReason: host appears to be renamed APP_URL
Fix: change $this>host = Doo::conf()>host; to$this>host = Doo::conf()>APP_URL;
Error 4: Undefined property: DooConfig::$host (different then error 3)File: controllers/CoreController.phpReason: host appears to be renamed APP_URLFix: $this>_view>host = Doo::conf()>host; to$this>_view>host = Doo::conf()>APP_URL;
Bint25 Aug, 2010
guynamedkeith, thank you for the tips!“This is how I successfully got this working” and integrated with my code
Son Do Ha28 Aug, 2010
I usedDooUriRouter::redirect($url);But it does not work. Can anyone help me? Thank you!
Bill2 Sep, 2010
Since a reference to the current session is always available via Doo::session(), why do you save it to the instancemember, _session?
Milos Kovacki2 Sep, 2010
@Son Do Ha
What is the error you are getting, and one more thing see if you are sending anything to the client since youmust use that before you send any data.
@BillBecause in first version there was no Doo::session() I think, but ofcourse you can always useDoo::session().
Ganesh9 Nov, 2010
DooController is included only during run(). but in your example, we’re including the CoreController evenbefore run() so, it won’t be able to extend doocontroller (which is never loaded).
so, i don’t see this example working. if i’m wrong, @Milos, can you please post a simple demo.
Milos Kovacki15 Nov, 2010
You just need to load controller in bootstrap with Doo::loadController(’CoreController’);So this would be example of your bootstrap(index.php)[php]ini_set(’display_errors’, 1);include ‘./protected/config/common.conf.php’;include ‘./protected/config/routes.conf.php’;include ‘./protected/config/db.conf.php’;
#Just include this for production mode//include $config['BASE_PATH'].’deployment/deploy.php’;include $config['BASE_PATH'].’Doo.php’;include $config['BASE_PATH'].’app/DooConfig.php’;
# Uncomment for auto loading the framework classes./*function __autoload($classname)Doo::autoload($classname);*/Doo::conf()>set($config);
# remove this if you wish to see the normal PHP error view.include $config['BASE_PATH'].’diagnostic/debug.php’;
# database usage//Doo::useDbReplicate(); #for db replication masterslave usage//Doo::db()>setMap($dbmap);//Doo::db()>setDb($dbconfig, $config['APP_MODE']);//Doo::db()>sql_tracking = true; #for debugging/profiling purpose
Doo::app()>route = $route;Doo::loadController(’CoreController’);# Uncomment for DB profiling//Doo::logger()>beginDbProfile(’doowebsite’);Doo::app()>run();[/php]
Ganesh24 Nov, 2010
but corecontroller extends doocontroller which is not loaded till you run(). the example won’t work.
Search...
Our Sponsors
Choose Language
English中文RussianDeutschEspañolArabicPortugese
Popular Tags
Find Tutorial
EasyNormalDifficult
Categories
Demos & Snippets (4)News & Updates (2)Screencast (1)Tutorials (17)
Recent Comments
kukat on Intro to DooPHP slides (PHP Malaysia meetup 2011)Milos Kovacki on Using DooTranslator for translationWeb dev Greece on Using DooTranslator for translationMilos Kovacki on Handling sessions without Apache sessionsT on Create a simple To Do List in DooPHP – Part 2
Blogroll
Development BlogDocumentationForumGoogle Code
Home About Contact Forum
All contents copyright © Learn DooPHP. All rights reserved. Theme design by WebKreation.