SpeedPHP框架学习-.基础及MVC理解
Controller为控制器文件。程序从index.php开始执行
[php]
define("SP_PATH",dirname(__FILE__)."/SpeedPHP");
define("APP_PATH",dirname(__FILE__));
$spConfig = array(
"db" =>array(
'host' =>'localhost',
'login' =>'root',
'password' =>'root',
'database' =>'test',
),
'view' => array(
'enabled' =>TRUE,
'config'=>array(
'template_dir'=> APP_PATH.'/tpl',
'compile_dir'=> APP_PATH.'/tmp',
'cache_dir'=> APP_PATH.'/tmp',
'left_delimiter'=> '
'right_delimiter'=> '}>',
),
)
);
require(SP_PATH."/SpeedPHP.php");
spRun();
上述程序定义了App和SP的路径,加载了数据库和视图层的配置,加载SP的核心库文件,最后运行整个系统。以上程序运行起来时,会首先到Controller目录下执行main类下的index方法。main类的程序如下:
[php]
class main extends spController
{
function index(){
$tpl = $this->spArgs("tpl","green");
$guestbook =spClass("guestbook");
$this->results= $guestbook->findAll();
$this->display("{$tpl}/index.html");
}
function write(){
$guestbook =spClass("guestbook");
$newrow =array(
'name'=> $this->spArgs('name'),
'title'=> $this->spArgs('title'),
'contents'=> $this->spArgs('contents'),
);
$guestbook->create($newrow);
echo "
}
}
从index.php过来默认调用main方法的index函数,在本例中这个函数首先设定模板名参数(tpl)。再新建一个model。使用model的findall方法,查找全部数据库信息。最后使用tpl模板显示结果。上述控制器程序,必须继承自spController类,方法名就是调用的action名。在程序中显式调用时路径为index.php?c=main&a=write。Model如下所示,是和数据库表同名的类文件。这个model必须继承自spModel,同时设置了主键和表名属性。
[php]
class guestbook extends spModel
{
var $pk = "id"; //每个留言唯一的标志,可以称为主键
var $table ="guestbook"; // 数据表的名称
}
在tpl模板目录里的index.html文件下使用如下所示程序,格式化输出结果。
[html]
:

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Chinese version
Chinese version, very easy to use

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
