Learn DooPHP: High performance PHP framework

Concept of how to work with DooFramework

Ok in this article I will show you how should you work with DooFramework, when I am making website I like to have one super class that extends DooController class and in constructor of that class I will add all stuff needed for my application, so lets make that class, we should name it for example CoreController :)

<?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;

 /**
 * 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;
 }

 /**
 * 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;
 }

}
?>

As you can see I have some functions that I usualy use in every development, now our MainController will extend our CoreController class, our CoreController is located in OUR_APP/protected/controller folder, and one more thing in index.php we must load CoreController class with:

Doo::loadController('CoreController');

When we load our CoreController, we can make our MainController class for example:

Add route first:

$route['*']['/'] = array('MainController', 'index');
<?php
/**
 * Description of MainController
 *
 * @author Milos Kovacki
 */

class MainController extends CoreController {

 public function index() {
 $this->_session->time = date("Y-m-d H:i:s");
 $this->_view->username = "John Smith";
 $data['content'] = 'index';
 $this->_view->render('template', $data, true);
 }

}
?>

As you can see we will render template and $data variable that has content in it, that content is our script that will be rendered. So this is one way to use template.
Now lets make template.php that is located in OUR_APP/protected/viewc folder:

<?php
include "header.php";
include "{$data['content']}.php";
include "footer.php";
?>

So as you can see in our template script we will include header/footer and our content. Now we must make index.php that will be our content in MainController for index action:

<?php
echo "Welcome ".$this->username."  at  " . $this->_session->time . ' time';

?>

So its very simple we defined 2 variables in our controller function, one is stored in session and that is time and other is username that we assigned in our action. So as you can see its pretty simple, you have your skeleton for making webapps :)
One more thing I want to show you as you can see I have one function that is named appendFile I am using it for adding some js from controller for some action where I need some js script, for example if you need some js just on index action of MainController you can add it trought controller action. I will now show you how is that doable, we need to add little something to our header.php where we have <head> tag, so in our <head> tag we add:

<?php if (isset($data['scripts'])) echo $data['scripts']; ?>

Now if there are some scritpts in data echo them. Now we use function appendFile:

$this->appendFile($data, $this->getJsPath() . 'main/index.js', $type = 'text/javascript');

We do this before we do render, but you already know that. Well thats about it.

Tnx for reading, please ask questions.


  • 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.

  • Hmm that is very strange, maybe you can provide us with code of your index.php (boostrap), I realy dont see what could be the problem. But if its working in /class directory then its ok it doesnt realy matter.

  • 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

  • Hmm that is very strange, you just need to add Doo::loadController(’CoreController’); in boostrap and then it works, since DooController is part of DooFw and its added in Doo class. You dont need to load it one more time.

  • I really appreciate the work Mil0s, I truly do but I couldnt get this to work either and I have been using Doo Framework for a while. I cant figure it out.
    I did exactly what you said same error as guy above
    if i put the load controller in the main controller i get a page full of errors.

  • The redirect() method had better call exit() after sending the header, I’ve found out.

  • Well, I will post code that works so that will solve all problems, stay tuned I will pack example of app and upload it here.

  • Yeah, I attempted this too with the same result. Also, is this tutorial for the latest SVN trunk? I don’t believe Doo::session is found in the 1.2 version.

  • DooSession is not in 1.2. It can be found in the latest trunk

  • Milos, I have a couple of questions:

    - Why do we need to Doo::loadController(’CoreController’)? Why can’t the file that defines MainController include the file that defines CoreController?

    - What benefit do you get from using appendFile(), etc. to include JS files as opposed to specifying them directly in template files?

  • - 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 heave all js and and css done inside

    tags.

    It’s just simple function to add some js/css in header of document.

  • 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 is not loaded yet.

  • yes indeed, you have to load DooController before loading the children class that extend it if you do it manually in index.php

  • Manually as opposed to turning on auto loading the framework classes?

  • Hi Milos,
    What happen if I access the url like http://www.mysitename.com/. In that case MainController, ‘index’ would never we called and intialize those session settings. It that right??

    We can also inialize all the core objects in “index.php”.

  • It would call controller you asigned in routes, and that MainController just extends CoreController so all settings and session would be initialized.

    You just extend your controllers to super controller.

  • The only way I can load the CoreController in the bootstrap without getting the DooController-not-found-error is 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?

  • I just have:
    Doo::loadController(’CoreController’);

    in my boostrap, and everything works ok, my CoreController is located in protected/controller dir, I will make one demo to show.

  • 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 :)

  • Hey Milos,
    what do you mean by ‘in my bootstrap’ can you please specify a specific file/location etc.?

  • 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();

  • Warning: This is only my second day using DooPHP. Will someone more advanced please double check my instructions.

    I followed Milos Kovacki’s instructions explicitly and ran into several errors. This is how I successfully got this working with the version of DooPHP I checked out from svn yesterday.

    Error 1: Class ‘DooController’ not found
    File: index.php
    Reason: autoloading needs to be turned on.
    Fix: Uncomment function __autoload($classname)

    Error 2: Undefined property: DooConfig::ROOT_DIR
    File: controllers/CoreController.php
    Reason: ROOT_DIR appears to have been renamed to BASE_PATH
    Fix: change $this->_basePath = Doo::conf()->ROOT_DIR; to
    $this->_basePath = Doo::conf()->BASE_PATH;

    Error 3: Undefined property: DooConfig::$host
    File: controllers/CoreController.php
    Reason: 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.php
    Reason: host appears to be renamed APP_URL
    Fix: $this->_view->host = Doo::conf()->host; to
    $this->_view->host = Doo::conf()->APP_URL;

  • I believe I have solved all the issues. I am trying to post my fixes, but am getting server errors. Most errors are stemming from the changes in DooPHP allowing the app path to be in a different place.

  • 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 this working with the version of DooPHP I checked out from svn yesterday.

    Error 1: Class ‘DooController’ not found
    File: index.php
    Reason: autoloading needs to be turned on.
    Fix: Uncomment function __autoload($classname)

    Error 2: Undefined property: DooConfig::ROOT_DIR
    File: controllers/CoreController.php
    Reason: ROOT_DIR appears to have been renamed to BASE_PATH
    Fix: change $this->_basePath = Doo::conf()->ROOT_DIR; to
    $this->_basePath = Doo::conf()->BASE_PATH;

    Error 3: Undefined property: DooConfig::$host
    File: controllers/CoreController.php
    Reason: 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.php
    Reason: host appears to be renamed APP_URL
    Fix: $this->_view->host = Doo::conf()->host; to
    $this->_view->host = Doo::conf()->APP_URL;

  • guynamedkeith, thank you for the tips!
    “This is how I successfully got this working” and integrated with my code ;-)

  • I used
    DooUriRouter::redirect($url);
    But it does not work. Can anyone help me? Thank you!

  • Since a reference to the current session is always available via Doo::session(), why do you save it to the instance member, _session?

  • @Son Do Ha
    What is the error you are getting, and one more thing see if you are sending anything to the client since you must use that before you send any data.

    @Bill
    Because in first version there was no Doo::session() I think, but ofcourse you can always use Doo::session().

  • DooController is included only during run(). but in your example, we’re including the CoreController even before 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.

  • 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 master-slave 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]

  • but corecontroller extends doocontroller which is not loaded till you run(). the example won’t work.