CI4 上手練習總結

先上源碼php

GitHub地址html

好戲開始啦…………git

在  CI4】CodeIgniter 4 上手練習 中有基本的安裝操做,這裏就直接從安裝後開始github

1、找到  app\Config\App.phpweb

    一、修改項目爲本身的域名redis

    public $baseURL = 'http://localhost:8080/';

    這個改成本身的域名,還能夠添加自定義的變量,如:數據庫

    public $weblibURL   = 'http://test.oaci4.com/weblic/';

    可是   public weblibURL  = $baseURL.  'weblic/';  這樣會提示錯誤,如今尚未解決  @^_^@json

    二、隱藏路徑中的  index.php,把這個數組

    public $indexPage = 'index.php';

        修改成緩存

    public $indexPage = '';

        找到  public\.htaccess 文件 ,把

    RewriteRule ^(.*)/$ index.php/$1 [L,R=301]

        修改成

    RewriteRule ^(.*)/$ index.php?/$1 [L,R=301]

    是否是沒看出區別,修改後會多了一個[?],同理修改

    RewriteRule ^(.*)$ index.php/$1 [L]

到這裏若是環境沒問題,就能夠運行CI的welcome了

 

2、開啓系統日誌

      在  app\Config\Logger.php 文件中,找到

    public $threshold = 4;

     修改成本身須要的等級就能夠了,日誌文件會記錄到  writable\logs 目錄

 

3、數據庫配置

    找到    app\Config\Database.php 文件,設置默認數據庫

    public $defaultGroup = 'default';

    能夠本身修改成本身指定的數據配置,先僅支持 MySQL,PostgreSQL,SqlLite3,MySQL 默認端口 3306,PostgreSQL 默認端口 5432

    更多能夠查看    【官方文檔】

4、緩存配置

    找到  app\Config\Cache.php ,支持  Memcached、Redis 緩存,修改本身使用的緩存數據庫對應的配置便可,(沒有嘗試同時使用兩個緩存)

 

5、加載經常使用 (CI4 以前沒有使用【命名空間】,…………)

    一、session

         使用 session 命名空間

use CodeIgniter\Config\Services;

    定義靜態變量

/**
 * -----------------------------------
 * @Date             2019-04-26
 * @Author           Acclea
 * @Describe         $sessionClass
 * -----------------------------------
 */
public static $sessionClass = '';

    在  initController 方法中實例化

// 加載session
        if(empty(self::$sessionClass)){
            $services           = new Services();
            self::$sessionClass         = $services->session();
        }

    這裏不須要調用  session_start  方法,由於在加載時會默認調用

    使用

//存儲session
$adminRow    = array("id"=>1,"name"=>"admin",);
self::$sessionClass->set($adminRow);

//讀取session
$adminId = self::$sessionClass->get('id');

 

    二、redis

    使用 redis 命名空間,使用 Memcached 一樣

use Config\Cache;
use CodeIgniter\Cache\CacheFactory;

    定義靜態變量

/**
 * -----------------------------------
 * @Date             2019-04-26
 * @Author           Acclea
 * @Describe         redis
 * -----------------------------------
 */
public static $redisConn = '';

在  initController 方法中實例化

// 加載redis
if(empty(self::$redisConn)){
    $redisFact          = new CacheFactory();
    $config             = new Cache();
    self::$redisConn    = $redisFact->getHandler($config);
}

使用 $adminRow 全文同一

//存儲Redis
$cacheKeyAdminInfo      = "ci4_test_admin_info_".$adminRow['id'];
$cacheKeyAdminInfoExp   = 60*60*2;

//讀取Redis
self::$adminInfo = self::$redisConn->get($cacheKeyAdminInfo);

 

三、加載 配置文件 

使用 App,Autoload命名空間,

use Config\Autoload;
use Config\App;

   定義靜態變量

/**
 * -----------------------------------
 * @Date             2019-04-26
 * @Author           Acclea
 * @Describe         加載系統須要的配置
 * -----------------------------------
 */
public static $sysConf = [];

在  initController 方法中實例化

// 加載autoLoad配置
if(!isset(self::$sysConf['autoLoad'])){
    self::$sysConf['autoLoad'] = new Autoload();
}

// 加載app配置
if(!isset(self::$sysConf['app'])){
    self::$sysConf['app'] = new App();
}

使用

$ref['weblibUrl']       = self::$sysConf['app']->weblibURL;

 

四、加載 post、get 獲取提交數據

    使用 ResponseTrait命名空間,

use CodeIgniter\API\ResponseTrait;

在 class 第一行 (推薦)加入

use ResponseTrait;

使用以下獲取數據,

//獲取經過get提交的數據,
 $getData   = $this->request->getGet();

 //獲取經過post提交的數據,
 $postData   = $this->request->getPost();

此處獲得的時數組形式,獲取每一個參數值

$name   = $postData['name'];

 

五、使用自定義通用方法

    建立class,指定命名空間(推薦在 app\Libraries\ 目錄下),調用相應方法步驟,

    建立自定義方法

function refJson(int $code = 0, string $msg = "OK", array $data = array()){

    $refArr     = array(
        'code'  => $code,
        'msg'   => $msg,
    );

    if($data){$refArr['data'] = $data;}

    $jsonStr = json_encode($refArr,JSON_UNESCAPED_UNICODE);
     
    die($jsonStr); 
}

    引用命名空間

use App\Libraries\SelfFunc;

    實例化

$selfFunc   = new SelfFunc();

    調用指定方法

$selfFunc->refJson(-3,"非法請求");

 

更多詳細代碼,能夠在GitHub中查看

相關文章
相關標籤/搜索