/*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 get configuration information required by the application) 3, command class interpreter Commandresolver; (Call the corresponding command class according to user request) 4, command class commands; Call the user request information class and business logic, you can also call the view of the picture pieces of 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;//Portal File ClassclassController {Private $applicationHelper; Private function__construct (); Static functionrun () {$instance=NewController (); $instance-init (); $instance-HandleRequest (); } functionInit () {//get configuration information required by the program $applicationHelper= Applicationhelper::instance (); $applicationHelper-init (); } functionHandleRequest () {//get and execute the command class $request=New\woo\controller\request (); $cmd _r=New\woo\command\commandresolver (); $cmd=$cmd _r->getcommand ($request); $cmd->execute ($request); }}//Get application configuration informationclassApplicationhelper {Private Static $instance; Private $config= "/tmp/data/woo_options.xml";//configuration information to save the file Private function__construct () {}Static functioninstance () {if(!self::$instance) {Selff::$instance=NewSelf (); } returnSelf::$instance; } functioninit () {$dsn= \woo\base\applicatonregistry::Getdsn (); if(!Is_null($dsn)){ return; } $this-getoptions (); } Private functiongetoptions () {$this->ensure (file_exists($this->config), "conld not find options File"); $options=simplexml_load_file($this-config); Print 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 values } Private functionEnsure ($expr,$message){ if(!$expr){ Throw New\woo\base\appexception ($message); } } }//user request Information classclassRequest {Private $properties; Private $feedback=Array(); function__construct () {$this-init (); \woo\base\requestregistry:: Setrequest ($this); } functioninit () {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); } } } functionGetProperty ($key){ if(isset($this->properties[$key]){ return $this->properties[$key]; } } functionSetProperty ($key,$val){ $this->properties[$key] =$val; } functionAddfeedback ($msg){ Array_push($this->feedback,$msg); } functionGetfeedback () {return $this-feedback; } functionGetfeedbackstring ($separator= "\ n"){ return implode($separator,$this-feedback); }}namespace Woo\command;//command Interpreterclasscommandresolver{Private Static $base _cmd; Private Static $default _cmd; function__construct () {if(!self::$base _cmd){ //The base class of the command class, which primarily uses reflection to check whether the found command class is inheriting itSelf::$base _cmd=New\reflectionclass ("\woo\command\command"); Self::$default _cmd=NewDefaultCommand ();//The default command class, which is called when the corresponding command class is not found } } //get Command class functionGetCommand (\woo\controller\request$request){ $cmd=$request->getproperty (' cmd '); $sep= Directory_separator;//representative "/" if(!$cmd){ returnSelf::$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=NewReflectionclass ($classname); if($cmd _class->issubclassof (self::$base _cmd)){ return $cmd _class->newinstance ();//instantiating a command class}Else { $request->addfeedback ("command"$cmd' is not a Command '); } } } $request->addfeedback ("command"$cmd' Not found '); return CloneSelf::$default _cmd; }}//Command ClassAbstract classcommand{Final function__construct () {}functionExecute (\woo\controller\request$request){ $this->doexecute ($request); } Abstract functionDoexecute (\woo\controller\request$request);}classDefaultCommandextendscommand{functionDoexecute (\woo\controller\request$request){ $request->addfeedback ("Welcome to Woo"); include("woo/view/main.php"); }}//Clientrequire("woo/controller/controller.php"); \woo\controller\controller:: Run ();
PHP Object-oriented front-end controller mode