zoukankan      html  css  js  c++  java
  • thinkphp5.0学习笔记(二)

    1、控制器加载页面

    public function index(){
        //指定的是view文件夹下,[action].html,但是如果并非指定,那么就可以进行传参
        return $this->fetch();
        //渲染指定的abc页面
        return $this->fetch('abc');
        //系统内置方法
        return view();
        //use thinkView;的前提下
        return call_user_func([new View(), 'fetch']);
    }

    注意:对于数据的返回,如果需要,可以调整config.php下的'default_return_type' => 'html';默认是html如果需要可以转成json

     2、控制器的初始化方法与钩子

    controller里的具体类如果继承了 hinkController方法后,那么当每个类进行调用的时候会预先执行_initialize这个方法,只要调用这个类里的所有方法,那么这个方法都会初触发。

    <?php
    namespace appindexcontroller;
    use thinkController;
    
    class Index extends Controller
    {
        public function _initialize(){
            echo 'this is initialize';
        }
    
        public function index(){
            return $this->fetch();
        }
    }

    当类继承了 hinkController后,就可以调用里面的生命周期函数

    <?php
    namespace appindexcontroller;
    use thinkController;
    
    class Index extends Controller
    {
        protected $beforeActionList = [
            'chect',                                    //这个类里的所有方法都适用
            'check' => ['only' => 'index,first'],       //只有指定的两个方法可用
            'check' => ['except' => 'first']            //除了指定的方法,其他可用
        ];
    
        public function index(){
            return $this->fetch();
        }
    
        public function first() {
            return 'this is first';
        }
    
        public function check() {
            echo 'are you ok????';
        }
    }

     3、控制器的页面跳转

    tp的页面跳转主要分两类,一个是成功跳转,一个是失败跳转,因为页面跳转是基于系统控制器类,所以控制器必需要继承 hinkController类

     前提需要继承 hinkController,注意:这里的URL在内部的方法是有通过URL类进行转义的

    $this->success(成功的提示, 跳转链接, 数据, 等待时间, header设置)
    $this->error(失败的提示, 跳转链接, 数据, 等待时间, header设置)

    成功时跳转的页面,失败时跳转的页面的配置在config.php里面

     tp重定向

    $this->redirect(链接,参数,http code, 隐式参数)  //注意:这里的链接里面是没有转义的,可以调用URL类进行转义

     空操作,即空的action

     当初恶意输入错误的controller的时候,那么会执行controller类里的默认方法

    public function _empty() {
        $this->redirect(url('index/index'));
    }

     空控制器,即空的控制器或操作

    在对应的controller下面建立Error.php类,那么当遇到空控制器时,系统自动会调用里面的方法,具体代码如下

    <?php
    namespace appindexcontroller;
    use thinkController;
    
    class Error extends Controller{
        public function index() {
            $this->redirect(url('index/index'));
        }
        public function _empty() {
            $this->redirect(url('index/index'));
        }
    }

     4、请求和响应

      a、获取url信息

    public function index(Request $req){
        //方法一、使用系统方法
        dump(request());
        //方法二、使用系统Request类 使用use thinkRequest;
        $res = Request::instance();
        dump($res);
        //方法三,引入use thinkRequest; 指定方法上的一个参数
        dump($req);
    }
    public function index(){
        //获取域名
        dump(request()->domain());
        //获取当前地址
        dump(request()->url());
        //获取入口文件
        dump(request()->baseFile());
        //获取pathinfo路径,这个会包含后缀
        dump(request()->pathinfo());
        //不会包含后缀
        dump(request()->path());
        //获取后缀
        dump(request()->ext());
    }

       b、获取模块信息

    public function index(){
        //获取模块信息
        dump(request()->module());
        //获取控制器
        dump(request()->controller());
        //获取方法名
        dump(request()->action());
    }

       c、获取请求相关参数或者方法

    public function first() {
        //获取ip
        dump(request()->ip());
        dump(input('id'));
        //判断请求方式
       request()->has('id');
    request()->isAjax(); request()->isGet(); request()->isPost(); request()->isPut(); request()->isDelete(); //判断请求方式 dump(request()->method()); //获取所有的参数 dump(Request::instance()->param()); //获取字段为id参数 dump(request()->only('id')); //获取除id之外的参数 dump(request()->except('id')); //是否是手机访问 dump(request()->isMobile()); }
    public function first() {
        //获取session
        dump(request()->session());
        //获取cookie
        dump(request()->cookie());
        //获取server对象
        dump(request()->server());
        //获取文件
        dump(request()->file());
        //获取头信息
        dump(request()->header());
    }
    //参数的过滤
    Request::instance()->filter(['strip_tags','htmlspecialchars']),

     

    public function first() {
        dump(request()->param());
        dump(request()->get(['id' => 88]));     //表求修改
        dump(request()->post(['id' => 789]));
        dump(request()->post('id'));    //表示获取值
        dump(request()->put('id'));
        dump(request()->delete('id'));
    }
  • 相关阅读:
    基于Windows环境下Myeclipse10.0下载安装破解及jdk的下载安装及环境变量的配置
    51 Nod 1791 合法括号子段【分治+字符串】
    一步一步深入理解Dijkstra算法
    POJ 3662 Telephone Lines【Dijkstra最短路+二分求解】
    拍拍网接入微信卡包备战双12圣诞购物季
    公众平台认证订阅号接口调整 增加群发接口等,可授权登录
    搜狗搜索推广品牌专区新增微信互动区展示企业微信公众号内容
    WP8版微信5.4发布 新增夜间模式 暂没小视频
    Kik CEO Ted Livingston发博称要成为西方的微信?
    微信"附近的人"新增商家公众号入驻功能
  • 原文地址:https://www.cnblogs.com/rickyctbu/p/11531232.html
Copyright © 2011-2022 走看看