Front-end Controller mode examples

Source: Internet
Author: User
/* The main components and functions of the front-end controller are as follows: 1, the import file class controller; (the call to this system starts with this file, and it is equivalent to a control center that calls all related classes) 2. Application configuration information Class Applicationhelper ;(used to obtain the configuration information required by the application) 3, the command class interpreter Commandresolver; (Call the corresponding command class according to the user request) 4, command class commands, (call the user request information class and business logic, also can call the view document) The whole system call step is probably: 1, get the configuration information required by the program 2, Get command Class 3, execute command class front-end controller and command mode comparison, in my opinion the two basically no difference, the principle is roughly the same. The code and annotations of the front controller are as follows: */namespace woo\controller;//entrance File class Controller {private $applicationHelper;p rivate function __    Construct ();    static function Run () {$instance = new Controller (); $instance->init (); $instance->handlerequest (); } function init () {///Gets the configuration information required by the program $applicationHelper = Applicationhelper::instance (); $applicationH    Elper->init (); } function HandleRequest () {//Gets and executes the command class $request = new \woo\controller\request (); $cmd _r = new \woo\command\com    Mandresolver (); $cmd = $cmd _r->getcommand ($request); $cmd->execute ($request);        }}//Get application configuration information class Applicationhelper {private static $instance;p rivate $config = "/tmp/data/woo_options.xml"; Configuration information saved files PrivaTe function __construct () {} static function instance () {if (!self:: $instance) {selff:: $instance = new self ();    }return self:: $instance;    } function init () {$dsn = \woo\base\applicatonregistry::getdsn ();        if (!is_null ($DSN)) {return;    } $this->getoptions ();    } Private Function GetOptions () {$this->ensure (file_exists ($this->config), "conld not find options file"); $options = simplexml_load_file ($this->config);p rint get_class ($options); $dsn = (string) $options->dsn; $this-        >ensure ($DSN, "No DSN found");        \WOO\BASE\APPLICATIONREGISTRY::SETDSN ($DSN); Configuration information will be cached in a registry class//Set other value} Private function ensure ($expr, $message) {if (! $expr) {throw new \woo\base\a        Ppexception ($message); }}}//user requests information class request {private $properties;p rivate $feedback =array (); function __construct () {$this->init ()        ;    \woo\base\requestregistry::setrequest ($this); } function init () {if (Isset ($_server[' Request_method ')]) {$this->properties = $_request;return; }foreach ($_server[' argv ') as $arg) {if (Strpos ($arg, ' = ')) {list ($key, $val) = explode ("=", $arg); $this->setproperty (            $key, $val);        }}} function GetProperty ($key) {if (Isset ($this->properties[$key]) {return $this->properties[$key];    }} function SetProperty ($key, $val) {$this->properties[$key] = $val;    } function Addfeedback ($msg) {Array_push ($this->feedback, $msg);    } function Getfeedback () {return $this->feedback;    }function getfeedbackstring ($separator = "\ n") {return implode ($separator, $this->feedback);    }}namespace woo\command;//command interpreter class Commandresolver{private static $base _cmd;private static $default _cmd; function __construct () {if (!self:: $base _cmd) {//The base class of the command class, this will use reflection to check whether the found command class is inherited from it self: $base _cmd = new \                Reflectionclass ("\woo\command\command");    Self:: $default _cmd = new DefaultCommand (); The default command class, which will be called when the corresponding command class is not found}}//Get command class function GetCOmmand (\woo\controller\request $request) {$cmd = $request->getproperty (' cmd '); $sep = Directory_separator;        Represents "/" if (! $cmd) {return self:: $default _cmd;    } $cmd = Str_replace (Array ('. ', $sep), "", $cmd) $filepath = "woo{$sep}command{$sep} {$cmd}.php";            The file path of the command class $classname = "woo\\command\\{$cmd}"; Class name if (File_exists ($filepath)) {@require_once ("$filepath"), if (Class_exists ($classname)) {$cmd _class = new Reflec            Tionclass ($classname), if ($cmd _class->issubclassof (self:: $base _cmd)) {return $cmd _class->newinstance ();                Instantiate the command class} else {$request->addfeedback ("command ' $cmd ' is not a command");    }}} $request->addfeedback ("command ' $cmd ' not Found"), Return clone self:: $default _cmd; }}//Command class abstract class Command{final function __construct () {}function execute (\woo\controller\request $request) {$    This->doexecute ($request); }abstract function Doexecute (\woo\controller\request $request);} Class DefaultCommandExtends Command{function Doexecute (\woo\controller\request $request) {$request->addfeedback ("Welcome to woo");    Include ("woo/view/main.php"); }}//client require ("woo/controller/controller.php"); \woo\controller\controller::run ();
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.