zoukankan      html  css  js  c++  java
  • ThinkPHP5 相关知识重点笔记

    一、相关配置
    1、配置文件:config/config.php
    <?php
    return [
    // 是否开启路由
        'url_route_on'           => true,
    ];
    ?>
    2、获取配置项
    <?php
    namespace appindexcontroller;
    use thinkController;
    use thinkConfig;
    class Index
    {
        public function index()
        {
            //return config('site_name');配置方法获取
            return dump(Config::get('database'));//get获取,推荐使用这种
        }
    }
    //设置配置
    Config::set('abc',123);
     Config::set($arr)
     
    二、路由设置
    config/route.php
    <?php
    // thinkRoute::rule('demo/:class','sam/test/demo','GET',['ext'=>'html'],['class'=>'w{1,10}']); //单个设置
    //数组方式设置(推荐)
    return [
    'index'=>'sam/test/index',
    'demo/:class'=>['sam/test/demo',['method'=>'GET','ext'=>'html'],'class'=>'w{1,10}'],
    ];
    ?>
    -----默认值(接上面)-------------
    http://tp5.cn/demo/yan/javascript.html
    public function demo($name,$class='php')
    {
    return '这是'.$name.'老师的正在学习'.$class;
    }
    thinkRoute::rule('demo/:name/[:class]/','sam/test/demo','GET',['ext'=>'html'],['class'=>'w{1,10}','name'=>'w{3,8}']);

    return [
    'index'=>'sam/test/index',
    'demo/:name/[:class]/'=>['sam/test/demo',['method'=>'GET','ext'=>'html'],['class'=>'w{1,10}','name'=>'w{3,8}']],
    ];

    -----------

    路由参数规则route.php
    (1)分开写
    thinkRoute::pattern([
    'name'=>'[a-zA-Z]+',
    'age'=>'d{2}'
    ]);
    thinkRoute::get('demo/:name/:age','sam/test/demo');
    (2)合并写
    return [
    '__pattern__'=>[
    'name'=>'[a-zA-Z]+',
    'age'=>'d{2}'
    ],
    'demo/:name/:age'=>'sam/test/demo'
    ];

     

    依赖注入,向类中的方法传递对象的方法
    class Temp
    {
    private $name;
    public function __construct($name='Sam')
    {
    $this->name=$name;
    }
    public function setName($name)
    {
    $this->name=$name;
    }
    public function getName()
    {
    return '方法是:'.__METHOD__.'属性是:'.$this->name;
    }
    }
    ----------
    public function getMethod(appcommonTemp $testtemp)
    {
    // 方法里的 appcommonTemp $testtemp等价于下面这行
    // $testtemp = new appcommonTemp;
    $testtemp->setName('SamC');
    return $testtemp->getName();
    }
    //绑定一个类到容器
    public function bindClass()
    {
    //把一个类放到容器中(注册到容器)
    hinkContainer::set('temp','appcommonTemp');
    //使用助手函数bind()
    //bind('temp','appcommonTemp');
    //将容器里的类实例化并取出来
    $temp = hinkContainer::get('temp',['name'=>'Samphp']);
    //或 
    //$temp = app('temp',['name'=>'Samphp']);
    return $temp->getName();
    }
    //绑定一个闭包到容器(理解为匿名函数)
    public function bingClosure()
    {
    hinkContainer::set('demo',function($domain){
    return '微语录的网址是:'.$domain;
    });
    //将容器里的闭包取出来用
    return hinkContainer::get('demo',['domain'=>'www.top789.cn']);
     
    }
  • 相关阅读:
    jenkins配置发送邮件
    inux下rz、sz的简单安装
    Linux系统信息查看命令
    selenium元素定位
    windows下 maven+selenium+testng项目搭建(七)
    eclipse testng插件安装
    window maven安装(六)
    linux下Apache+Svn环境搭建(五)
    Linux实战教学笔记38:企业级Nginx Web服务优化实战(下)
    Linux实战教学笔记37:企业级Nginx Web服务优化实战(上)
  • 原文地址:https://www.cnblogs.com/samphp/p/8576074.html
Copyright © 2011-2022 走看看