二零一四年辛星starphp第一节设置入口文件以及App类
Jun 13, 2016 pm 12:02 PM
controller
php
quot
2014年辛星starphp第一节设置入口文件以及App类
*********************本节目标****************
1.首先是我们的框架大致布局,我们即将写成的这个框架,它的入口文件统一为star.php,它需要做的一些事,比如加载配置项,日志文件什么的日后再说,首先确定一下它的目录结构,它的目录结构是如下的样子:
|---------star| |------------core:核心类库| | |---------------app.php| | |---------------model.php| | |---------------controller.php| | |----------------view.php| | |-----------------fun.php| || |-------------common:函数库| |-------------class:类库| |-------------extends:其他类库| |--------------star.php||-----------app它的文件目录为| |------------遵循模块/控制器/方法的格式| |------------index.php||------------adimin|
Copy after login
<?php //定义它是从首页进入的define('INDEX',True);//包含该核心文件define('__ROOT__',__DIR__);include __ROOT__.'/star/star.php';
Copy after login
3.然后我们书写star.php的内容,它能够引导找到App类,并且调用App类的run方法来使程序运行下去,它的代码:
<?php //定义版本信息define("VERSION","0.1");//控制PHP版本if(phpversion() <'5.3'){ exit("版本太低不支持");} //表示路径分隔符define("DS",DIRECTORY_SEPARATOR);//这个STAR表示我们的star目录if(!defined("STAR")){define("STAR",__DIR__);}//定义应用程序目录,if(! defined("APP")){define("APP",__ROOT__.DS."app");}if(! defined("CORE")){define("CORE",STAR.DS."core");}//导入应用程序控制文件 include STAR.DS."core".DS."app.php";//导入核心文件include CORE.DS."fun.php";$app = new App();$app->run();
Copy after login
<?php /***该类用于统一处理所有的信息**/if(!defined("STAR")) die("系统错误");include STAR.DS."core".DS."controller.php";class App{ //模块名 private $module = ""; //控制器 private $controller = ""; //方法名 private $method = ""; //参数名 private $param = array(); //参数个数 private $paramlength = 0; /** *用于解析控制器和方法 * */ public function __construct(){ //默认使用/来解析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);//模块名 $controller = array_shift($path);//控制器名 $method = array_shift($path);//方法名 var_dump($path); for($i = 0;$i 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); //自动根据解析的路由来执行 } /** *用于运行方法 * */ 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(); } } /** * * */ }
Copy after login
<?php /***用于过滤用户输入信息的函数*它主要是防止sql注入*也需要防范html实体*/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; }
Copy after login
6.我们的Controller还没有实际的意义,但是为了能够起到形式上的作用,我们定义如下:
<?php /***该类用于解析url并且根据url来执行相关的方法**/if(!defined("STAR")) die("系统错误");class Controller{ }
Copy after login
*****************辅助工作***************
1.为了测试运行,我们在app目录下的controller目录下新建了一个MyController.php,它有一个index方法,我们这里给定如下:
<?phpif (!defined("STAR")) exit("Not Allowed");class MyController extends Controller{ public function index($arr){ echo "hello world"; var_dump($arr); }}
Copy after login
点击打开链接 ,当然是面积分下载的。
3.该版本可以作为我日后的回忆,哈哈。
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot tools Tags

Hot Article
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

How To Set Up Visual Studio Code (VS Code) for PHP Development
