PHP头条
热点:

SpeedPHP框架学习-.基础及MVC理解


Controller为控制器文件。程序从index.php开始执行

[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] 
<?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 "<ahref=/index.php?c=main&a=index>return</a>"; 
       } 
}     
从index.php过来默认调用main方法的index函数,在本例中这个函数首先设定模板名参数(tpl)。再新建一个model。使用model的findall方法,查找全部数据库信息。最后使用tpl模板显示结果。上述控制器程序,必须继承自spController类,方法名就是调用的action名。在程序中显式调用时路径为index.php?c=main&a=write。Model如下所示,是和数据库表同名的类文件。这个model必须继承自spModel,同时设置了主键和表名属性。

[php] 
<?php 
class guestbook extends spModel 

  var $pk = "id"; //每个留言唯一的标志,可以称为主键 
  var $table ="guestbook"; // 数据表的名称 

在tpl模板目录里的index.html文件下使用如下所示程序,格式化输出结果。

[html] 
<{foreach from=$results item=one}> 
     <h4><{$one.title}></h4> 
     <p><{$one.name}>:<br /><{$one.contents}></p> 
<{/foreach}> 

www.phpzy.comtrue/phprm/13855.htmlTechArticleSpeedPHP框架学习-.基础及MVC理解 Controller为控制器文件。程序从index.php开始执行 [php] ?php define(SP_PATH,dirname(__FILE__)./SpeedPHP); define(APP_PATH,dirname(__FILE__)); $spConfig = array( db =array( host =localhost...

相关文章

    暂无相关文章
相关频道:

PHP之友评论

今天推荐