Cakephp框架的配置和示例

一、把app/webroot目录设置为根目录。

二、配置Apache

1、设置为下面形式:
1)目录可以复盖
<Directory />
    Options FollowSymLinks
    AllowOverride All
</Directory>

2)允许重写路径
LoadModule rewrite_module modules/mod_rewrite.so

三、修改程序

1)
app/config在这个目录下把database.php.default修改成database.php,然后打开就可以修改到数据库的链接了。

2)输入http://127.0.0.1就可以看到正确的页面

三、举例演示

1)新建news表 里面的字段 id,title,content,time,sort

2)
因为一定要操作news这个表,所以先建立news的model
/app/models/news.php

<?
class  news  extends  AppModel{
var   $name = ' news '
}
?>

如果没有var $name='news';这一行 模型会默认为要处理的表是newss

3)然后新建/app/controllers/news_controller.php

<?
class  newsController  extends  AppController{
var   $uses = array ( ' news ' ); // 使用news这个模型
function  index(){
     
$this -> set( ' lists ' , $this -> news -> findAll());
}
// 默认
function  add( $id = 0 ){

     
if ( $id ){
      
$this -> set( ' id ' , $id );
      
$this -> news -> id = $id ;
     }
    
     
if ( ! empty ( $this -> data)){
      
$ret = $this -> news -> save( $this -> data[ ' news ' ]);
      
if ( $ret ){
       
if ( $id )
       
$this -> flash( ' 更新成功 ' , ' /news/ ' );
       
else
       
$this -> flash( ' 添加成功 ' , ' /news/ ' );
      }
else {
       
if ( $id )
       
$this -> flash( ' 更新失败 ' , ' /news/add/ ' . $id );
       
else
       
$this -> flash( ' 添加失败 ' , ' /news/add ' );
      }
     }
     
// exit();
}      // 添加/修改
function  delete( $id ){
     
$this -> news -> id = $id ;
     
$ret = $this -> news -> remove();
     
if ( $ret )
      
$this -> flash( ' 删除成功 ' , ' /news/ ' );
     
else
      
$this -> flash( ' 删除失败 ' , ' /news/ ' );
   
     
exit ();
// 删除
function  view( $id ){
     
$this -> news -> id = $id ;
     
$this -> set( ' news ' , $this -> news -> find());
}
}
?>

这样就可以访问http://127.0.0.1:/news/

但是访问的时候会有提示你miss view add.thtml view.thtml delete.thtml index.thtml
因为cake会默认输出的view名为你当前的action名 如果要让它按自己的意愿输出使用$this->render('test'); 也就是 test.thtml
这些thml文件都在/app/views/controller/目录下面     当前的controller 是news
所以文件放在/app/views/news/目录下

add.thtml

 

<? echo   $html -> formTag( ' add ' ); ?>
<? echo   $html -> input( ' news/title ' ); ?>
<? echo   $html -> textarea( ' news/content ' ); ?>
<? echo   $html -> submit( ' submit ' ); ?>
</ form >

index.thtml
news:
<br>
<?
for($i=0;$i<count($lists);$i++){
?>
title:<?=$lists[$i]['news']['title']?>
<?
}
?>

view.thtml
title:<?=$news['news']['title']?>
content:<?=$news['news']['content']?>

就可以用http://127.0.0.1/news/index
http://127.0.0.1/news/add
http://127.0.0.1/news/view
来访问了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值