Heim Backend-Entwicklung PHP-Tutorial php框架开发2(SPL库和controller)

php框架开发2(SPL库和controller)

Jun 13, 2016 pm 01:14 PM
controller name path php

php框架开发二(SPL库和controller)

根据上话的目录结构(如果不清楚的可以看上一篇)。我在simple文件夹内新建了一个simple.php.

<p>require 'includes/exceptions.php';
require 'includes/autoloader.php';
session_start();</p><p>//$view = new view();
lib::set_item('controller' , new controller());
lib::get_item('controller' , lib::PERSIST_STORGE)->render();
//$content = $view->finish();</p>
Nach dem Login kopieren

这一章主要是讲SPL,所以我们只看第二行的autoloader.php.当我们在index.php中加入

require 'simple/simple.php';
Nach dem Login kopieren

之后。

我们看一下autoloader.php的源码

<?php
class autoloader{
	
	public static function includesautoloader($class){
		$path = defined('SIMPLE_PATH') ? SIMPLE_PATH : $_SERVER['DOCUMENT_ROOT'];
		$file_name = $path . '/includes/' . $class . '.php';
		if(is_readable($file_name))
			require $file_name;
	}
	
	public static function modulesautoloader($class){
		$path = defined('SIMPLE_PATH') ? SIMPLE_PATH : $_SERVER['DOCUMENT_ROOT'];
		$file_name = $path . '/modules/' . $class . '.php';
		if(is_readable($file_name))
			require $file_name;
	}
	
	public static function controllerautoloader($class){
		$path = defined('SIMPLE_PATH') ? SIMPLE_PATH : $_SERVER['DOCUMENT_ROOT'];
		$file_name = $path . '/controller/' . $class . '.php';
		if(is_readable($file_name))
			require $file_name;
	}

}

spl_autoload_register('autoloader::includesautoloader');
spl_autoload_register('autoloader::modulesautoloader');
spl_autoload_register('autoloader::controllerautoloader');
Nach dem Login kopieren


这个类都是静态方法,当引用这个PHP文件后,程序会自动载入这三个文件夹下的内容,这样我们不用重复去required 或include。如果对SPL有不了解的,可以直接去百度,这里只是告诉大家,这个SPL作用相当广。

然后在includes文件夹内新建一个lib.php

<?php
class lib{
	
	const SETTING_ARRAY = true;
	const PERSIST_STORGE = false;
	
	public static function set_item($name , $value , $is_array = false){
		if($is_array){
			$_SESSION[$name]   = array();
			$_SESSION[$name][] = $value;
		}
		else{
			$_SESSION[$name] = $value;
		}
	}
	
	public static function get_item($name , $persist = true){
		$result = null;
		if(isset($_SESSION[$name])){
			$result = $_SESSION[$name];
		}
		if(!$persist){
			unset($_SESSION[$name]);
		}
		return $result;
	}
	
	public static function sendto($url = ''){
		if(empty($url)){
			$url  = '/';
		}
		die(header('Location:' . $url));
	}
}
Nach dem Login kopieren

这个类目前只用于设置SESSION相关的内容。

当执行到lib::set_item('controller' , new controller());时,这里新建了一个控制器对象,并对象存入SESSION中。

controller.php

<?php
class controller{
	//URL部分
	protected $parts;
	//方法参数
	protected $params;
	
	public function __construct(){
		$this->parts = array();
		$this->analysis();
	}
	
	public function analysis(){
		$path_info = $_SERVER['PATH_INFO'];
		if(substr($path_info , 0 , 1) == '/'){
			$path_info = substr($path_info , 1);
		}
		$parts = explode('/' , $path_info);
		if(empty($parts[0]))  $parts[0] = 'index';
		if(empty($parts[1]))  $parts[1] = 'demo';
		
		$this->parts = $parts;
		array_shift($parts);
		array_shift($parts);
		$this->params = $parts;
	}
	
	public function render(){
		if(!class_exists($this->parts[0])){
			throw new ControllerDoesntExistsException($this->parts[0] . ' not exists!');
		}
		if(!method_exists($this->parts[0] , $this->parts[1])){
			throw new ActionDoesntExistsException($this->parts[0] . 'of ' . $this->parts[1] . ' not exists!');
		}
		
		$new_controller = new $this->parts[0];
		$called = call_user_func_array(array($new_controller , $this->parts[1]) , $this->params);
		if($called === false){
			throw new ActionFailedException($this->parts[0] . 'of ' . $this->parts[1] . ' failed to excute property!');
		}
	}
}
Nach dem Login kopieren


这个类只用PATH_INFO模式来获取调用的控制器和ACTION,并将参数传递到相应的ACTION方法里。当controller初始化之后,会自动分析PATH_INFO的内容,

lib::get_item('controller' , lib::PERSIST_STORGE)->render();

然后调用render()就会在相应的控制器目录找到控制器和ACTION。

例如我在controller文件夹内新建一个index.php

<?php
class index{
	
	public function demo(){
		echo "sdfsdf";
	}
}
Nach dem Login kopieren

那么执行http://localhost/index.php/index/demo就会输出"sdfsdf";(这是我的本地路径)。

 

以上代码难度不高,如果没有看懂的同学,请Q我,也可以去PHP官网上去查找相应函数的用法。

下一节相会对视图作一个简单的介绍。

 

Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

AI Hentai Generator

AI Hentai Generator

Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

R.E.P.O. Energiekristalle erklärten und was sie tun (gelber Kristall)
2 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
Repo: Wie man Teamkollegen wiederbelebt
4 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Abenteuer: Wie man riesige Samen bekommt
3 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌

Heiße Werkzeuge

Notepad++7.3.1

Notepad++7.3.1

Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version

SublimeText3 chinesische Version

Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1

Senden Sie Studio 13.0.1

Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6

Dreamweaver CS6

Visuelle Webentwicklungstools

SublimeText3 Mac-Version

SublimeText3 Mac-Version

Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

PHP 8.4 Installations- und Upgrade-Anleitung für Ubuntu und Debian PHP 8.4 Installations- und Upgrade-Anleitung für Ubuntu und Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installations- und Upgrade-Anleitung für Ubuntu und Debian

CakePHP-Projektkonfiguration CakePHP-Projektkonfiguration Sep 10, 2024 pm 05:25 PM

CakePHP-Projektkonfiguration

CakePHP Datum und Uhrzeit CakePHP Datum und Uhrzeit Sep 10, 2024 pm 05:27 PM

CakePHP Datum und Uhrzeit

CakePHP-Datei hochladen CakePHP-Datei hochladen Sep 10, 2024 pm 05:27 PM

CakePHP-Datei hochladen

CakePHP-Routing CakePHP-Routing Sep 10, 2024 pm 05:25 PM

CakePHP-Routing

Besprechen Sie CakePHP Besprechen Sie CakePHP Sep 10, 2024 pm 05:28 PM

Besprechen Sie CakePHP

So richten Sie Visual Studio-Code (VS-Code) für die PHP-Entwicklung ein So richten Sie Visual Studio-Code (VS-Code) für die PHP-Entwicklung ein Dec 20, 2024 am 11:31 AM

So richten Sie Visual Studio-Code (VS-Code) für die PHP-Entwicklung ein

CakePHP-Kurzanleitung CakePHP-Kurzanleitung Sep 10, 2024 pm 05:27 PM

CakePHP-Kurzanleitung

See all articles