2014 Sinsing starphp first section set up portal files and app classes

Source: Internet
Author: User

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:

<?php//Definition It is entered from the home page define (' INDEX ', True);//contains the core file define (' __root__ ', __dir__); include __root__. ' /star/star.php ';

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:

<?php//definition Version Information define ("version", "0.1");//Control PHP version if (phpversion () < ' 5.3 ') {exit ("Low version is not supported");}//Represents the path delimiter define (" DS ", directory_separator);//This star represents our star directory if (!defined (" star ")) {define (" star ", __dir__);} Define the application directory, if (! defined ("app")) {define ("app", __root__. DS. " App ");} if (! defined ("core")) {define ("core"), STAR. DS. " Core ");} Import application control files  include STAR. DS. " Core ". DS. " App.php ";//import core file include core. DS. " Fun.php "; $app = new  app (); $app->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:

<?php/*** This class is used to uniformly handle all information **/if (!defined ("star")) Die ("System error") and include STAR. DS. " Core ". DS. " Controller.php "; class app{//module name private $module =" ";//Controller Private $controller =" ";//Method name Private $method =" ";//Parameter name private $param = Array ();//The number of arguments private $paramlength = 0;/*** used to resolve the controller and method **/public function __construct () {//default use/to parse Url$path = Trim ($_server[' path_info '), '/'); $path = explode ('/', $path); Var_dump ($path); $paramlength = (count ($path)-3)/2;var_ Dump ($paramlength), $this->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 **/public function run () {$controller = $this->controller;$ According to the parsed Route origin 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:

<?php/*** function to filter user input information * It is primarily to prevent SQL injection * also need to guard against HTML entity */function Star_arr_safe ($array) {if (Is_array ($array)) {$count = Count ($array); for ($i = 0; $i < $count; $i + +) {$array [$i] = Htmlspecialchars ($array [$i]); $array [$i] = addslashes ($array [$i]);}} return $array;}

6. Our controller has no practical significance, but in order to be able to play a formal role, we define the following:

<?php/*** The class is used to parse the URL and perform the associated Method **/if ("!defined (" "STAR") Die ("System error") according to the URL; class controller{}
7. So far, let's write this code first.



Auxiliary work ***************

1. In order to test the operation, we create a new mycontroller.php under the Controller directory in the app directory, which has an index method, which we have given here as follows:

<?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 operating normally and the version number is 0.0.1, the code I uploaded to Csdn's download place:
Click the open link, of course, the area of the download.

3. This version can be used as my future memories, haha.


2014 Sinsing starphp first section set up portal files and app classes

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.