thinkPHP5学习笔记

115 阅读2分钟

1、 集成开发环境(phpStudy)

2、 惯例配置:thinkphp\convention.php;

应用配置:与app同级新建一个conf,里面新建config.php(就叫这个名字),用来新增和修改惯例配置(我们不去修改 框架文件,底层逻辑是两个数组的merge,后面的会覆盖前面的。)

扩展配置:conf中新建extra文件夹(就叫这个名字),database特例,可以不用放在extra文件夹下,直接与extra同级放置即可(thinkphp的人性化设计)。【扩展配置会覆盖掉应用配置,再就是惯例配置】。

场景配置:config.php中添加‘app_status’ => 'home',在conf中新建两个文件home.php和office.php,通过修改app_status的值,即可轻松使用两个地方的配置。【注意:5.0.6版本,如何要在home或者office中修改database的某个值,需要全部粘贴然后在修改,不然其他值会丢失】。

模块配置:conf文件夹下,新建与模块同名的文件夹(如:index、admin)conf下的config.php,全局有效,与模块同名的文件夹下的文件只对该模块有效。

动态配置:public function __construct(){},中动态配置的值,针对当前控制器下所有方法中有效,当前方法中配置的值,只对当前方法有效。

3、 获取request对象的方法

三种方法可以获取到request对象

      1.  request()

      $request = request();

      dump($request);

      2. 使用think下的request类。来获取实例

      use think\request

      $request = Request::instance();

      dump($request);

      3.同样使用think下的request类。来获取实例【推荐】

      给index()传参数 index(Requset $request);

4、 请求对象参数获取

    public function index1(Request $request) 
    {
      //能在request对象获取:
      // 1、获取浏览器输入框的值
      dump($request->domain()); // "http://localhost"
      dump($request->pathinfo());// "req/index/index1/type/5.html"
      dump($request->path());// "req/index/index1/type/5"
      // 2、请求类型
      dump($request->method());//"GET"
      // 快捷判断请求类型的方式
      dump($request->isGet());//true
      dump($request->isPost());//false
      dump($request->isAjax());//false
      // 3、请求的参数
      dump($request->get()); //数组
      // {
      //   ["/req/index/index1/type/5_html"] => string(0) ""
      //   ["id"] => string(2) "10"
      // }
      dump($request->param()); //数组
      // {
      //   ["/req/index/index1/type/5_html"] => string(0) ""
      //   ["id"] => string(2) "10"
      //   ["type"] => string(1) "5"
      // }
      dump($request->post());// {}
      // session('name','yin');
      dump($request->session());
      // cookie('email','123456');
      dump($request->cookie());

      //获取参数中的值
      dump($request->param('type'));//"5"
      dump($request->cookie('email'));//"123456"

      // 获取 模块、控制器、操作
      dump($request->module());//"req"
      dump($request->controller());//"Index"
      dump($request->action());//"index1"
    }