程序人生
TinkPHP学习专题之控制器
作者:邵波涛    时间:2016-08-28 浏览量:264

ThinkPHP控制器名称首字母大写,后面带控制器单词和.class.php后缀例如NameController.class.php,其中可被外界访问到的public方法称作动作,控制器内的各不同的动作就用来相应各类请求,控制器类默认动作为index,当然也可以没用这个默认的方法,有一个特殊的方法_empty(),用来应对控制器内非预期的请求,当然也可以做一些纯传值性质的控制器,例如上一篇中的使用R函数获取。

控制器内需要获取数据库数据,需要获取页面回传的参数,同时需要处理数据然后反馈回去,因此分三部分介绍。

1、获取数据库信息,数据库设置已在基础篇中讲解,不再多提,获取数据主要的CURD方法如下:

D函数:

$Category=D('category');
$list=$Category->select();

其中D用于数据模型的实例化操作,后续的select()是查询操作,可以使用多种条件组合出各类查询结果;

M函数:

$User = M('User');
$User->select();

使用M方法实例化的话,不需要加载具体的模型类,所以性能会更高。

当然如果想自己建立SQL语句执行,也可以实例化空模型。

$Model = M();
$Model->query('SELECT * FROM user WHERE status = 1');

常用的操作如下:

$Book=D('book');
$total=$Book->count();//计数
$alist=$Book->join('t_booktype ON t_book.tid=t_booktype.tid')->order('id desc')->select();//联合查询,排序
$p=$Book->where('pus<>0')->order('id desc')->limit(8)->select();//条件,限制记录集大小
$v=$Book->where('vcnt<>0')->order('vcnt desc')->limit(8)->select();//条件,排序
$t=$Book->field('tag,count(*) as num')->group('tag')->order('num desc')->select();//统计字段,分类,排序
$c=$Book->where('id<>""')->field('id,count(*) as num')->group('id')->limit(8)->select();各类混合。

增加操作:

$Book=M('book');
$data['title']=I('post.title');
$Book->create($data);
$Book->add();

修改操作:

$Book=M('book');
$id=I('post.id');
$data['title']=I('post.title');
$Book->where(array('id'=>$id))->save($data);

删除操作:

$id=I('param.id');
$Book=D('book');
$Book->where(array('id'=>$id))->delete();

2、取页面回传参数像上面例子一样使用I函数即可,默认ThinkPHP已经做了大量过滤工作,非常方便,还有一种是直接把参数传递给控制器来操作,可直接建立含参的控制器函数即可,例如:

public function bookinfo($id)
{
   $Book=M('book');
   $bk=$Book->where(array('id'=>$id))->find();
}

还有纯粹性为了前台传值用的,例如:

public function bookname($id){
   $t=D('book')->where(array('id'=>$id))->field('title')->select();
   return $t[0]['title'];
}

3、变量分配,操作完数据库后需要把这些数据集分配到各个模板,不过请注意,模板是分块引入的,不能给分块分配变量,必须是该模板内每个分块模块需要的变量都得分配到,切记,还拿上面做例子。

public function bookinfo($id)
{
   $Book=M('book');
   $bk=$Book->where(array('id'=>$id))->find();
   $this->assign('bk',$bk);//分配数据集$bk,给变量bk,在模板中就可以使用$bk进行变量输出
   $this->display();
}

最后,控制器中还有一些跳转指令

redirect('User/login');//跳转指定控制器的指定方法
$this->success('新增成功', 'User/list');//成功后跳转指定控制器的指定方法
$this->error('新增失败');//失败返回上一页面


发言请留称呼=>
本文留言
共有 6 条留言
  • AngeloAntef
    2017-10-02
    As you will inevitably learn on your path to losing weight, effective weight loss is not only about watching what you eat, but much more about changing your lifestyle. This means changing your habits and how you approach your day-to-day life. Read this information to help you throughout the process.
  • Caseynob
    2017-10-11
    Some people, especially those running on busy daily schedules tend to use the pills to help maintain weight since they can not afford to follow all the diet programs. This is not advised. It is recommended that one seek advice from a professional in this field before using the pills. This can save one from many dangers associated with the misuse. The diet pills should always be taken whole. Some people tend to divide the pills to serve a longer period of time. This is not advised and can lead to ineffectiveness. If it is required that one takes a complete tablet, it means that a certain amount of the ingredients are required to achieve the desired goal. It is also recommended that one does not crush the pill and dissolve it in beverages. Chemicals found in beverages have the potential of neutralizing the desired nutrients in the pill thereby leading to ineffectiveness. The best way to take the tablets is swallowing them whole with a glass of water. The diet pills speed up the metabolic processes. This is the key factor that leads to the burning of all the fats in the body. This means that one passes out lots of urine, which subsequently leads to dehydration. It is imperative that the user take lots of water round the clock. This will help curb dehydration, which can lead to health problems. In addition to that, water offers the required medium for the function of the nutrients and elimination of the fats. When buying the review of diet pills, it is imperative that one gets the most recommended dose. People tend to compromise the quality and effectiveness of the tablets due to the variation in cost. The low priced pills depict poor quality, which means their effectiveness is not reliable. Some have also been found to cause health problems. The dose should also be taken as recommended. Over dose will not speed up the process but rather lead to complication. This will increase risk of side effects. If the taking of the pill is forgotten, do not take more to compensate for the lost time. The diet plan enclosed with the diet pills has also to be followed. According to the requirements, the termination of the diet must be done even with no results. This means your body is irresponsive.
  • Robertdyern
    2017-12-08
    Doctor Who is now considered a British Institute and has come a long way since it first aired on November 23rd 1963. The very first show saw the Doctor travel 100,00 years into the past to help some dim cavemen discover light. After 26 seasons and seven Doctors later the series came off our screens in 1989 much to the disappointment of the huge devoted fanbase. In 1996 an attempt was made to revive Doctor Who but it wasnt until June 2005 when it came back with a vengeance with Christopher Eccleston as the ninth Doctor that put the series back on the map as it were. It then went on for 5 years with David Tenant portraying the Doctor until 2010 when Matt Smith took over the role. Today it is still a great family show and has attracted many new fans. https://www.cialissansordonnancefr24.com/acheter-cialis-a-paris-sans-ordonnance/
  • Lukhoogue
    2018-01-26
    Efficacia Propecia 5 Alfa Reduttasi online pharmacy Nizagra Sildenafil Prix Du Cytotec Au Maroc Cialis Generique Sans Danger
  • Lukhoogue
    2018-02-07
    Tadalis Sx Soft And Generique cialis Prix Kamagra 20 Dutasteride Canada
12
扫一扫,手机浏览网站
友情链接
申请链接