Nov 27, 2010

The bootstrap file

the mean of www directory is to handle your webroot files. this is the only folder that should be accessible via your web server, also, you can have many webroots that are binded to each of your applications. in this way you share the api and the business code between your applications.
First, create an index.php file and edit it. The first thing we have to do is to import the libraries we need to load: here is the code

// include the configuration file
require_once(dirname(__FILE__) . '/../conf/configuration_front.php');
// include the autoload manager
require_once(dirname(__FILE__) . '/../core/autoloadManager.php');
// set the cache file for the autoload
autoloadManager::setSaveFile(dirname(__FILE__) . '/../tmp/front.php');
// add the folders you want to the  autoload, you should add always CORE to have the core files
// trigger the autoload registration

Second, we will write some logic to handle the public or protected sections of our websites, inject our needed dependencies to the dispatcher and trigger the routing method to call the appropriate method in the controller. Here is the code :

    // get the dispatcher instance
    $front = frontDispatcher::getInstance();
    // get the actions
    $actions = AccessHelper::getActions();
    // authenticate using the actions configuration
    $AuthManager = new AuthManager($actions);
    // tell the Auth Manager where to route if the authentication failed
    $AuthManager->authenticate($front, '/?controller=Feed&action=index');
    // get the appropriate application controller
    $Container = ContainerFactory::get('front');
    // inject the access configuration
    $Container['Access'] = $actions;
    // inject the Authentication object
    $Container['AuthManager'] = $AuthManager;
    // inject the request object
    $Container['Request'] = $_REQUEST;
    // inject the session object
    $Container['Session'] = SessionManager::getSession('front');
    // Route using the container as a confi
catch (Exception $e)
        // instanciate the debugging console, true is for debugging mode, false is the default
        $console = new DebugConsole($e, true);
        // render the errors

Ok, I think this bunch of code needs some explanation. You may notice that there is maybe a lot of code for a bootstrapper, you could be right. However, I prefer to see what the framework is supposed to do for those action instead of having them at a separate layer. So lets explain the code :

  • $front = frontDispatcher::getInstance(); : This is quite simple, we are getting the instance of the front dispatcher.

next – and if we need ┬áto use the Authentication and access manager , we need to add the following lines. Basically, the AccessHelper::getActions will return the list of the public and protected actions of our applications. The helper is related to a configuration file in which you can customize if the user should be authentified or not to access a certain action. The authenticate method of the AuthManager takes as parameters the dispatcher and the default url if the authentication fails.

  • $actions = AccessHelper::getActions();
  • $AuthManager = new AuthManager($actions);
  • $AuthManager->authenticate($front, ‘/?controller=Feed&action=index’);

The folloging lines the code contains concern the Dependency injection container. The idea is that we have to pass all the dependencies of our controller from outside. The mechanism is more explained in the section Dependency injection container of this tutorial.

  • // get the appropriate application controller
    $Container = ContainerFactory::get(‘front’);
    // inject the access configuration
    $Container['Access'] = $actions;
    // inject the Authentication object
    $Container['AuthManager'] = $AuthManager;
    // inject the request object
    $Container['Request'] = $_REQUEST;
    // inject the session object
    $Container['Session'] = SessionManager::getSession(‘front’);

The last line of the try block will trigger the route method of the dispatcher. this allows to call the approppriate action on the approppriate controller.
In the catch block, using the DebugConsole, we display a friendly message to the user. A second parameter, set to true in a debug mode, allows displaying the exception stack trace for the developer.

So at this moment, the frontDispatcher root method is called, we have now to start writing our first controller!.

Leave a comment