第三方php库未在Codeigniter中加载

时间:2018-07-10 12:15:43

标签: php codeigniter encryption encoding

我正在为我的应用程序使用codeigniter。我想散列数据库ID。因此,我正在使用以下php库:HashIds for php

该库位于application / libraries / hashids.php

这是我的控制器代码。我尝试了不同的方法,例如Filename Uppercase等,但是它说无法加载请求的类

<?php 
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//require_once(APPPATH.'');
//require_once(APPPATH.'libraries/hashids.php-master/src/Hashids.php');


class Pages extends CI_Controller {
  public function __construct() {

    parent::__construct();

    // include APPPATH . 'libraries/Hashids/Src/HashidsInterface.php';
    //include APPPATH . 'libraries/Hashids/Src/Hashids.php';

    //require_once(APPPATH.'libraries/Hashids/Src/HashidsInterface.php');
    require_once(APPPATH.'libraries/Hashids/Src/Hashids.php');

    //use Hashids/Src/Hashids.php;

    $this->load->model('post_model');
    $this->load->model('comment_model');
    $this->load->model('media_model');
    //$this->load->library('Hashids');
  }


  public function postFunc() {

    $this->load->library('hashids');  
    //$hashids = new hashids(); 

    $hashids->encode(1);
    echo $hashids;
    exit(); 
  }

上述库也为此实现了接口,因为我是Codeigniter的新手,所以不知道为什么会抛出错误。

1 个答案:

答案 0 :(得分:0)

尝试使用作曲家,我认为现在和将来使用库将更加容易。 See here how to install composer.

首先执行命令composer require hashids/hashids或将lib添加到require对象中的composer.json文件中,例如波纹管,然后执行命令composer install

{
    "description": "The CodeIgniter framework",
    "name": "codeigniter/framework",
    "type": "project",
    "homepage": "https://codeigniter.com",
    "license": "MIT",
    "support": {
        "forum": "http://forum.codeigniter.com/",
        "wiki": "https://github.com/bcit-ci/CodeIgniter/wiki",
        "slack": "https://codeigniterchat.slack.com",
        "source": "https://github.com/bcit-ci/CodeIgniter"
    },
    "require": {
        "php": ">=5.3.7",
        "hashids/hashids": "3.0.0"
    },
    "suggest": {
        "paragonie/random_compat": "Provides better randomness in PHP 5.x"
    },
    "require-dev": {
        "mikey179/vfsStream": "1.1.*",
        "phpunit/phpunit": "4.* || 5.*"
    }
}

请确保在配置文件application/config/config.php中将路径设置为composer自动加载脚本,如下所示:

$config['composer_autoload'] = 'vendor/autoload.php';

通常,autoload.php文件位于项目根文件夹上创建的vendor文件夹中。

然后在代码中尝试使用lib在脚本顶部指定名称空间use Hashids\Hashids;,如下所示:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

  use Hashids\Hashids;    

  class Pages extends CI_Controller {

      public function __construct() {        
          parent::__construct();

          $hashids = new Hashids(); // use the lib

          $this->load->model('post_model');
          $this->load->model('comment_model');
          $this->load->model('media_model');
          //$this->load->library('Hashids');        
      }