200行描述MVC,读完kohana总结的一点笔记
200行大点,读完kohana总结的一点笔记。 无 ?php header('Content-type: text/html; charset=utf-8'); define('__ROOT__',str_replace('\\','/', __DIR__ )); //set_exception_handler(array("Factory","last_fun")); 设置一个用户定义的异常处理函数 Factory
200行大点,读完kohana总结的一点笔记。
<?php header('Content-type: text/html; charset=utf-8'); define('__ROOT__',str_replace('\\','/', __DIR__ )); //set_exception_handler(array("Factory","last_fun")); 设置一个用户定义的异常处理函数 Factory::getIns($_SERVER['REQUEST_URI']); abstract class ProductInterface { public $data=array(); //用于接收传过来的内容 public static $magic_quotes =NULL; public function __construct() { self::$magic_quotes = (version_compare(PHP_VERSION, '5.4') < 0 AND get_magic_quotes_gpc()); // 清洁所有请求变量 $_GET = self::sanitize($_GET); $_POST = self::sanitize($_POST); $_COOKIE = self::sanitize($_COOKIE); } public static function sanitize($value) { if (is_array($value) OR is_object($value)) { foreach ($value as $key => $val) { $value[$key] = self::sanitize($val); } } elseif (is_string($value)) { if (self::$magic_quotes === TRUE) { $value = stripslashes($value); } if (strpos($value, "\r") !== FALSE) { $value = str_replace(array("\r\n", "\r"), "\n", $value); } } return $value; } public function __set($k,$v) { $this->$k = $v; } public function __call($method,$arg) { echo '错误指定页面'; print_r($arg); //错误指定页面 } public static function __callStatic($method,$arg) { print_r($arg); //错误指定页面 静态的好像不太可能 先写这儿了 } public function assign($key,$value) { $this->data[$key] = $value; } //引入模板 public function display($template,$ext = '.html') { $template_path = $template.$ext; if(file_exists($template_path)){ foreach ($this->data as $k=>$v){ $this->__set($k,$v); } include $template_path; } } } //控制器 class IndexAction extends ProductInterface { public function index() { print_r( $_GET); //$body = file_get_contents('php://input'); //print_r($body); //结果为 member_id=1234567 enctype="multipart/form-data" // print_r($_POST); //结果为 Array ( [member_id] => 1234567 ) //$model= new Article('article'); // $arr= $model->select('SELECT * FROM article limit 2'); //print_r($arr); } public function admin($array) { $model= new Article('article'); $array=array(); $array['rec']='admins'; $array['pos']='李斯'; $array['content']='hellow world'; echo $model->add($array); } } ///控制器2 class AdminAction extends ProductInterface { public function index($array) { echo __method__.'方法'; } } class Factory { protected static $ins=null; protected function __construct(){ ob_start(); //打开缓冲区 static::autoLoad(); register_shutdown_function(array('Factory','last')); //程序执行完后执行 } public static function getIns($url){ if(self::$ins instanceof self ){ self::$ins->execute($url); }else{ self::$ins = new self(); self::$ins->execute($url); } } protected function set_router($uri) { /* $uri='index/admin'; // $route_regex='#^(?:(?P[^/.,;?\n]++)(?:/(?P[^/.,;?\n]++)(?:/(?P[^/.,;?\n]++))?)?)?$#uD'; //$route_regex='#^captcha(?:/(?P[^/.,;?\n]++))?$#uD'; $route_regex='#^(?:(?P<controller>[^/.,;?\n]++)(?:/(?P<action>[^/.,;?\n]++)(?:/(?P<id>[^/.,;?\n]++))?)?)?$#uD'; if ( ! preg_match($route_regex, $uri, $matches)) { echo '没有匹配上'; } else { print_r($matches); } */ $arr=array(); if (strpos($uri,'?') === false) { $pathinfo=array(); if (!empty($_SERVER['PATH_INFO'])) { $arg=trim($_SERVER['PATH_INFO'],'/'); } else if ($request_uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)){ //有效的URL路径发现,设置它。 $arg= trim($request_uri,'/'); } if(stripos($arg,'/') !== false) { $pathinfo=explode('/',$arg); } $arr['ctrolloer']= isset($pathinfo[0])? $pathinfo[0] : 'index' ; array_shift($pathinfo); $arr['action']= isset($pathinfo[0])?$pathinfo[0]:'index' ; array_shift($pathinfo); $num=count($pathinfo); for ($i=0;$i<$num;$i+=2) { $arr['param'][$pathinfo[$i]]=$pathinfo[$i+1]; } return $arr; } else { $ln=parse_url($uri); parse_str($ln['query'],$array); $arr['ctrolloer']=$array['m'] ? strtolower($array['m']) : 'index'; $arr['action']=$array['c'] ? strtolower($array['c']) : 'index'; unset($array['m']); unset($array['c']); $arr['param']=$array; return $arr; } } protected function execute($url) { $arr= self::$ins->set_router($url); $_GET = isset($arr['param']) ? $arr['param'] : array(); $ProductType = ucfirst($arr['ctrolloer']).'Action'; if (class_exists($ProductType)) { $p=new $ProductType(); $p->$arr['action']($arr); } else { throw new Exception("Error Processing Request", 1); } } public static function last()// echo '脚本执行完了'. PHP_EOL; { $info=ob_get_contents(); //得到缓冲区的内容并且赋值给$info $file=fopen($_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR.'indexs.html','w'); ///打开文件 指定缓存目录 fwrite($file,$info); //写入信息到info.txt fclose($file); //关闭文件info.txt //ob_end_clean(); ob_end_flush(); flush(); } static public function loadFile($className) { $dir=array(); $dir[0]=__ROOT__.'/application/'; //指定多个目录 $dir[1]=__ROOT__.'/module/'; $file_arr=self::find_file($dir,$className); foreach($file_arr as $value){ if(is_file($value) ){ return include_once $value; }else{ throw new \Exception('找不到'.$className.'类'); } } } public static function find_file($paths=array(),$name) { $name=ucfirst($name).'.php'; $found=array(); foreach ($paths as $dir){ if (is_file($dir.$name)){ $found[] = $dir.$name; } } return $found; } // 注册自动装载机 static public function autoLoad() { spl_autoload_register( array(__CLASS__,'loadFile') ); } // 注册自动装载机2 //如有必要注册多个自动加载函数 static public function autoLoad2() { spl_autoload_register( array(__CLASS__,'loadFile2') ); } } abstract class DB{ public $db=NULL; public $table=NULL; public $config=array( 'dsn'=>'mysql:dbname=test;host=127.0.0.1', 'user'=>'root', 'password'=>'root', 'charset'=>'utf8', 'persistent'=>false //持久性链接 ); public $options = array( PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES ", ); public function __construct($table){ $this->table=$table; $this->options[PDO::MYSQL_ATTR_INIT_COMMAND] .= $this->config ['charset']; if($this->config ['persistent']){ $this->options[PDO::ATTR_PERSISTENT] = true; } try { // 长连接,如有需要将array(PDO::ATTR_PERSISTENT=>true) 作为第四个参数 $this->db=$pdo = new \PDO ( $this->config ['dsn'],$this->config ['user'],$this->config ['password'],$this->options); //$dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_SILENT); //不显示错误 $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);//显示警告错误,并继续执行 $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);//产生致命错误,PDOException //$this->db->exec('set names utf8'); } catch ( \Exception $e ) { throw new \Exception ( $e->getMessage () ); } } public function query($sql){ return $re=$this->db->query($sql); } public function select($sql) /*查询*/ { $re=$this->query($sql); return $re->fetchAll(); } public function add($arr=array()){ /*添加*/ $sql = 'insert into ' . $this->table . ' (' . implode(',',array_keys($arr)) . ')'; $sql .= " values ('"; $sql .= implode("','",array_values($arr)); $sql .= "')"; $stmt = $this->db->prepare($sql); $stmt->execute($arr); echo $this->db->lastinsertid(); } public function update($arr,$where = ' where 1 limit 1'){ /*修改*/ //$sql = "UPDATE `user` SET `password`=:password WHERE `user_id`=:userId"; $sql = 'update ' . $this->table .' set '; foreach($arr as $k=>$v) { $sql .= $k . "='" . $v ."',"; } $sql = rtrim($sql,','); $sql .= $where; $stmt = $this->db->prepare($sql); $stmt->execute($arr); return $stmt->rowCount(); } public function delete($sql){ /*删除*/ $stmt = $this->db->prepare($sql); $stmt->execute(); return $stmt->rowCount(); } } ////可以每一张表一个类 统一继承DB类。。。。。 class Article extends DB{ } ?> <script> var x=1,y=z=0; function add(n){ n=n+1; } y=add(x); function add(n){ n=n+3; } z=add(x); </script>

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

引言在当今快速发展的数字世界中,构建健壮、灵活且可维护的WEB应用程序至关重要。PHPmvc架构提供了实现这一目标的理想解决方案。MVC(模型-视图-控制器)是一种广泛使用的设计模式,可以将应用程序的各个方面分离为独立的组件。MVC架构的基础MVC架构的核心原理是分离关注点:模型:封装应用程序的数据和业务逻辑。视图:负责呈现数据并处理用户交互。控制器:协调模型和视图之间的交互,管理用户请求和业务逻辑。PHPMVC架构phpMVC架构遵循传统MVC模式,但也引入了语言特定的功能。以下是PHPMVC

mvc架构(模型-视图-控制器)是PHP开发中最流行的模式之一,因为它为组织代码和简化WEB应用程序的开发提供了清晰的结构。虽然基本的MVC原理对于大多数Web应用程序来说已经足够,但对于需要处理复杂数据或实现高级功能的应用程序,它存在一些限制。分离模型层分离模型层是高级MVC架构中常见的一种技术。它涉及将模型类分解为更小的子类,每个子类专注于特定功能。例如,对于一个电子商务应用程序,您可以将主模型类分解为订单模型、产品模型和客户模型。这种分离有助于提高代码的可维护性和可重用性。使用依赖注入依赖

MVC(Model-View-Controller)模式是一种常用的软件设计模式,可以帮助开发人员更好地组织和管理代码。MVC模式将应用程序分为三部分:模型(Model)、视图(View)和控制器(Controller),每个部分都有自己的角色和职责。在本文中,我们将讨论如何使用PHP实现MVC模式。模型(Model)模型代表应用程序的数据和数据处理。通常,

SpringMVC框架解密:为什么它如此受欢迎,需要具体代码示例引言:在当今的软件开发领域中,SpringMVC框架已经成为开发者非常喜爱的一种选择。它是基于MVC架构模式的Web框架,提供了灵活、轻量级、高效的开发方式。本文将深入探讨SpringMVC框架的魅力所在,并通过具体的代码示例来展示其强大之处。一、SpringMVC框架的优势灵活的配置方式Spr

在Web开发中,MVC(Model-View-Controller)是一种常用的架构模式,用于处理和管理应用程序的数据、用户界面和控制逻辑。PHP作为流行的Web开发语言,也可以借助MVC架构来设计和构建Web应用程序。本文将介绍如何在PHP中使用MVC架构设计项目,并解释其优点和注意事项。什么是MVCMVC是一种软件架构模式,通常用于Web应用程序中。MV

PHP8框架开发MVC:逐步指南引言:MVC(Model-View-Controller)是一种常用的软件架构模式,用于将应用程序的逻辑、数据和用户界面分离。它提供了一种将应用程序分成三个不同组件的结构,以便更好地管理和维护代码。在本文中,我们将探讨如何使用PHP8框架来开发一个符合MVC模式的应用程序。第一步:理解MVC模式在开始开发MVC应用程序之前,我

Beego是一个基于Go语言的Web应用框架,具有高性能、简单易用和高可扩展性等优点。其中,MVC架构是Beego框架的核心设计理念之一,它可以帮助开发者更好地管理和组织代码,提高开发效率和代码质量。本文将深入探究Beego的MVC架构,让开发者更好地理解和使用Beego框架。一、MVC架构简介MVC,即Model-View-Controller,是一种常见

PHP8框架开发MVC:初学者需要知道的重要概念和技巧引言:随着互联网的快速发展,Web开发在当今的软件开发行业中扮演着重要的角色。PHP被广泛用于Web开发,并且有许多成熟的框架可以帮助开发人员更高效地构建应用程序。其中,MVC(Model-View-Controller)架构是最常见且广泛使用的模式之一。本文将介绍初学者在使用PHP8框架开发MVC应用程
