2014 Sinsing starphp first section set up portal files and app classes
This section targets ****************
1. First of all we have a framework for the general layout, we are about to write this framework, its import files unified for star.php, it needs to do some things, such as loading configuration items, log files and so on later, first determine its directory structure, its directory structure is as follows:
|---------star| |------------Core: Kernel Class Library | | |---------------app.php| | |---------------model.php| | |---------------controller.php| | |----------------view.php| | |-----------------fun.php| || |-------------common: function library | |-------------class: library | |-------------extends: other Class libraries | |--------------star.php| | -----------app It's file directory is | |------------follow the format of module/Controller/method | |------------index.php| | ------------adimin|
2. We write this star.php file today, it is a unified import file, first we must define a index.php, its file content is as follows:
3. Then we write the star.php content, it can lead to find the app class, and call the App class's Run method to make the program run, its code:
Run ();
4. Then we write this app class, it should be able to complete the resolution of the URL, here we have not set the configuration file, so our URL unified use PathInfo mode, and the path delimiter uniform with a slash, and our URL of the file name of the following section is the "module/Controller/method/ Parameter 1/value 1/parameter 2/value 2 ... "The way to write, therefore, our app.php file is as follows:
Paramlength = $paramlength; $module = Array_shift ($path);//module Name $controller = Array_shift ($path);//Controller name $method = Array_ Shift ($path);//Method name Var_dump ($path); for ($i = 0; $i < $paramlength; $i + +) {$param [] = $path [$i + 1];} Var_dump ($param); $param = Star_arr_safe ($param); Var_dump ($param); $this->param = $param; if ($module = = "") {$module = "Index";} if ($controller = = "") {$controller = "Index";} if ($method = = "") {$method = "index";} $this->module = $module; $this->controller = $controller; $this->method = $method;//spl_autoload_register ($ This->loadcore);//automatically executes}/*** for running methods based on the parsed Route **/public function run () {$controller = $this->controller; $module = $ this->module; $dir = APP. DS. $module. DS. " Controller ". DS. " $controller "." Controller.php "; include" $dir "; $controllerclass = $controller." Controller "; $class = new $controllerclass (); $method = $this->method; $param = $this->param; $length = $this Paramlength;if (Is_int ($length) && ($length >= 1)) {$class-$method ($param);} else{$cLass-> $method ();}} /*****/}5. We also need to add some code in the core folder's fun.php file, which has a star_arr_safe function to filter the array to prevent illegal injection, and its contents are as follows:
6. Our controller has no practical significance, but in order to be able to play a formal role, we define the following:!--? php/*** This class is used to parse the URL and execute the associated method **/if ("!defined (" STAR ")) Die (" System error ") based on the URL, class controller{} 7. As of now, let's write this code first.
***************** auxiliary work ***************
1. For test runs, We have created a new mycontroller.php in the controller directory under the app directory, and it has an index method, which we are given here:
!--? phpif (!defined ("STAR")) exit ("not allowed ") class Mycontroller extends Controller{public function index ($arr) {echo" Hello World "; Var_dump ($arr);}} 2. This version is currently operational and the version number is 0.0.1, the code I uploaded to Csdn's download place:
Click to open the link, of course, the area of the download.
3. This version can be used as my future memories, haha.