SpeedPHP framework learning - basics and MVC understanding_PHP tutorial

WBOY
Release: 2016-07-14 10:06:41
Original
931 people have browsed it

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 "return"; 
       } 
}     
从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] 
<{foreach from=$results item=one}> 
    

<{$one.title}>

 
    

<{$one.name}>:
<{$one.contents}>

 
<{/foreach}> 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/477933.htmlTechArticleController为控制器文件。程序从index.php开始执行 [php] ?php define(SP_PATH,dirname(__FILE__)./SpeedPHP); define(APP_PATH,dirname(__FILE__)); $spConfig = array( db =arr...
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!