Application::__construct PHP Method

__construct() public method

Start the application, analyze URL elements, call according controller/method or relocate to fallback location
public __construct ( )
    public function __construct()
    {
        // create array with URL parts in $url
        $this->splitUrl();
        // creates controller and action names (from URL input)
        $this->createControllerAndActionNames();
        // does such a controller exist ?
        if (file_exists(Config::get('PATH_CONTROLLER') . $this->controller_name . '.php')) {
            // load this file and create this controller
            // example: if controller would be "car", then this line would translate into: $this->car = new car();
            require Config::get('PATH_CONTROLLER') . $this->controller_name . '.php';
            $this->controller = new $this->controller_name();
            // check for method: does such a method exist in the controller ?
            if (method_exists($this->controller, $this->action_name)) {
                if (!empty($this->parameters)) {
                    // call the method and pass arguments to it
                    call_user_func_array(array($this->controller, $this->action_name), $this->parameters);
                } else {
                    // if no parameters are given, just call the method without parameters, like $this->index->index();
                    $this->controller->{$this->action_name}();
                }
            } else {
                // load 404 error page
                require Config::get('PATH_CONTROLLER') . 'ErrorController.php';
                $this->controller = new ErrorController();
                $this->controller->error404();
            }
        } else {
            // load 404 error page
            require Config::get('PATH_CONTROLLER') . 'ErrorController.php';
            $this->controller = new ErrorController();
            $this->controller->error404();
        }
    }

Usage Example

Example #1
0
 public function __construct($argv)
 {
     parent::__construct();
     // get the parsers;
     $parsers = $this->getParsers();
     parent::addParameter('parser', true, implode("|", $parsers), null, 'bio2rdf parser to run');
     parent::addParameter('statistics', false, "true|false", "false", 'generate statistics');
     if (parent::setParameters($argv, true) === FALSE) {
         if (parent::getParameterValue('parser') == '') {
             parent::printParameters();
             exit;
         }
     }
     $statistics = parent::getParameterValue("statistics");
     // now get the file and run it
     $parser_name = parent::getParameterValue('parser');
     $file = $parser_name . '/' . $parser_name . '.php';
     if (!file_exists($file)) {
         trigger_error("{$file} does not exist", E_USER_ERROR);
         exit(-1);
     }
     require $file;
     $parser_class = str_replace(".", "", $parser_name) . "Parser";
     $parser = new $parser_class($argv);
     set_time_limit(0);
     $start = microtime(true);
     $parser->Run();
     $end = microtime(true);
     $time_taken = $end - $start;
     print "Start: " . date("l jS F \\@ g:i:s a", $start) . "\n";
     print "End:   " . date("l jS F \\@ g:i:s a", $end) . "\n";
     print "Time:  " . sprintf("%.2f", $time_taken) . " seconds\n";
 }
All Usage Examples Of Application::__construct