thinkphp 5.0 控制器

新建一个控制器

  1. 在控制器里面 新建PHP文件,注意首字母大写
  2. 在PHP文件里面编写代码,先写命名空间,再引入Controller类
  3. 在文件里面接着写,类要继承controller,在写方法,这里注意,方法名字用驼峰法命名
  4. 最后要加载页面就可以访问了
<?php
    namespace app\index\controller;
    use think\Controller;
    class Index extends Controller
    {
        public function index()
        {
             return $this->fetch() ;
        }
    }

简单页面加载

有两种加载办法,
一个是理由系统类,另一个是用系统方法,关于更多参数,我会在页面加载中,仔细讲解

<?php
    namespace app\index\controller;
    use think\Controller;
    class Index extends Controller
    {
        public function index()
        {
        //这个是一种,先导入,在继承,在用继承类的方法
             return $this->fetch() ;
        }
    }
<?php
    namespace app\index\controller;
    use think\Controller;
    use think\View;
    class Index extends Controller
    {
        public function index()
        {
        //这个是用view类来实现的
             $con= new view;
             return $con->fetch() ;
        //当然也可以使用助手函数
            return view();
        }
    }

控制器的初始化

  1. 必须调用并继承controller类。
  2. 他的作用是,初始化就运行
<?php
    namespace app\index\controller;
    use think\Controller;
    class Index extends Controller
    {
    //访问首页index,就先输出init
        public function _initialize()
        {
            echo 'init<br/>';
        }
        public function index()
        {
            return 'index';
        }
    }

前置方法

意思是,在运行方法之前,在先运行别的方法

<?php
    namespace  app\index\controller;
    use think\Controller;
    class Index extends Controller
    {
        protected  $beforeActionList = [
            //下面表示全部调用,意思是运行别的方法时候,调用one方法。
            'one',
            //下面表示除了index别的,都是先调用
            'two' => ['except'=>'index'],
            //下面是之对index有效
            'three'=>['only'=> 'index']
        ];
        public function one(){
            echo 'one';
        }
        public function two(){
            echo 'two';
        }
        public function three(){
            echo 'three';
        }
        public function index(){
            echo "我是 index";
        }
    }

页面跳转

  1. 首先要引入并继承Controller类
  2. 看下面代码
<?php
    namespace  app\index\controller;
    use think\Controller;
    class Index extends Controller
    {
    //基本格式为,success(提示信息,跳转的地址,自定义数据,倒计时时间,头信息)
        $this ->success('登陆成功',url('index/index/index'));
        $this ->errot('登陆失败',url('index/index/index'));
    }

修改跳转页面

//位置在(\thinkphp\tpl\dispatch_jump.tpl
'dispatch_success_tmpl'  => THINK_PATH . 'tpl' . DS . 'dispatch_jump.tpl',
'dispatch_error_tmpl'    => THINK_PATH . 'tpl' . DS . 'dispatch_jump.tpl',
//修改可以指定新的url地址。在(\thinkphp\tpl\dispatch_jump.tpl),新建success.tpl和error.tpl
// 修改默认页面
'dispatch_success_tmpl'  => THINK_PATH . 'tpl' . DS . 'success.tpl.tpl',
'dispatch_error_tmpl'    => THINK_PATH . 'tpl' . DS . 'error.tpl',

重定向

他们目的是为了防止资源浪费,就说他们访问到其他方法,跳转到首页

//下面代码表示,跳转到news控制category方法,添加get参数,cate_id=2,临时跳转,等等
$this->redirect('News/category', ['cate_id' => 2], 302, ['data' => 'hello']);

空操作

这个很重要,都需要添加,这个是防止恶意输入,为了安全。

<?php
    //声明命名空间
    namespace app\index\controller;
    //引入controller类
    use think\Controller;
    //新建error控制器并继承controller类
    class Error extends Controller{
        public function index(){
            $this->redirect('index/index');//用重定向到首页
        }
        public function _empty(){
            $this->redirect('index/index');//用重定向到首页
        }
    }

猜你喜欢

转载自blog.csdn.net/weixin_42249565/article/details/80459387