Home Backend Development PHP Tutorial 二零一四年辛星starphp第一节设置入口文件以及App类

二零一四年辛星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
2.我们今天先写这个star.php文件,它是统一的入口文件,首先我们必须定义一个index.php,它的文件内容如下:

<?php //定义它是从首页进入的define(&#39;INDEX&#39;,True);//包含该核心文件define(&#39;__ROOT__&#39;,__DIR__);include __ROOT__.&#39;/star/star.php&#39;;
Copy after login

3.然后我们书写star.php的内容,它能够引导找到App类,并且调用App类的run方法来使程序运行下去,它的代码:

<?php //定义版本信息define("VERSION","0.1");//控制PHP版本if(phpversion() <&#39;5.3&#39;){	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
4.然后我们写这个App类,它应该能够完成对URL的解析,这里我们还没有设置配置文件,因此我们的URL统一使用PATHINFO模式,而且路径分隔符统一用斜线,而且我们的url的文件名的后部分是按照”模块/控制器/方法/参数1/值1/参数2/值2...."的方式去写的,因此,我们的app.php文件如下:

<?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[&#39;PATH_INFO&#39;],&#39;/&#39;);		$path = explode(&#39;/&#39;,$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
5.我们还需要在core文件夹的fun.php文件中添加一些代码,它有一个star_arr_safe函数用来对数组进行过滤,来防止非法注入,它的内容如下:

<?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
7.截至目前,我们先写这些代码。



*****************辅助工作***************

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
2.该版本目前运行正常,版本号记为0.0.1,代码我上传到csdn的下载的地方:
点击打开链接  ,当然是面积分下载的。

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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

See all articles