Home Backend Development PHP Tutorial Analysis on the method of integrating smarty and adodb in Codeigniter

Analysis on the method of integrating smarty and adodb in Codeigniter

Jun 14, 2018 pm 03:18 PM
codeigniter smarty integrated

This article mainly introduces the method of integrating smarty and adodb in Codeigniter, and analyzes the usage skills of the Codeigniter library in the form of examples. Friends in need can refer to it

The examples in this article describe the integration of smarty and adodb in Codeigniter Adodb method. Share it with everyone for your reference, the details are as follows:

To write your own library in CodeIgniter, you need to write two files, one is the init_myclass.php file under application/init (if there is no init directory, Create it yourself). The other is to create the myclass.php file in the application/libraries directory.

Here myclass is your class name. You can just read the manual for some rules. I will just explain the steps here.

1) Create mysmarty.php and adodb.php respectively under application/libraries
The contents of the mysmarty.php file are as follows:

<?php
// load Smarty library
require('Smarty/Smarty.class.php');
// The setup.php file is a good place to load
// required application library files, and you
// can do that right here. An example:
// require('guestbook/guestbook.lib.php');
class MySmarty extends Smarty {
 function MySmarty()
 {
    // Class Constructor.
    // These automatically get set with each new instance.
    $this->Smarty();
    $basedir=dirname(__FILE__);
    $this->template_dir = "$basedir/templates/";
    $this->compile_dir = "$basedir/templates_c/";
    $this->config_dir  = "$basedir/configs/";
    $this->cache_dir  = "$basedir/cache/";
    //$this->compile_check = true;
    //this is handy for development and debugging;never be used in a production environment.
    //$smarty->force_compile=true;
    $this->debugging = false;
    $this->cache_lifetime=30;
    $this->caching = 0; // lifetime is per cache
    //$this->assign('app_name', 'Guest Book');
 }
}
?>
Copy after login

The file path is modified according to the specific situation. The file path starts relative to the home directory of your website, not the current directory of the current file. For example, the above require('Smarty/Smarty.class.php'); is not relative. application/libraries directory, but relative to the $_SERVER['DOCUMENT_ROOT'] directory. The content of the

adodb.php file is as follows:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Adodb
{
  function Adodb()
  {
    //$dsn="dbdriver://username:password@server/database"
    $dsn = 'mysql://user:password@localhost/xxxx';
    require_once("adodb/adodb.inc".EXT);
    $this->adodb =& ADONewConnection($dsn);
    $this->adodb->Execute("set NAMES 'utf8'"); 
  }
}
?>
Copy after login

2) Create init_adodb.php and init_mysmarty.php respectively in the application/init directory .

The content of the init_adodb.php file is as follows:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
$obj =& get_instance();
$obj->adodb = new Adodb($obj);
$obj->ci_is_loaded[] = 'adodb';
Copy after login

The content of the init_mysmarty.php file is as follows:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
if ( ! class_exists('MySmarty'))
{
  require_once(APPPATH.'libraries/mysmarty'.EXT);
}
$obj =& get_instance();
$obj->mysmarty = new MySmarty();
$obj->ci_is_loaded[] = 'mysmarty';
?>
Copy after login

3) Use them
Create a file you need in the application/controllers directory. You can use adodb and smarty like this.

<?php
class Test extends Controller {
 function Test()
 {
  parent::Controller(); 
  $this->load->library('mysmarty');
  $this->load->library('adodb');
 }
 function index()
 {
 $this->load->library('adodb');
 $row = $this->adodb->adodb->getrow('SELECT * FROM admin');
    $this->mysmarty->assign("row",$row);
    $this->mysmarty->display("test.tpl");
 }
}
?>
Copy after login

I don’t know why adodb is needed twice here. According to the official method, it should only be needed once, but his method is wrong for me. Maybe it's because I don't know much about CodeIgniter yet. I'll see if there is a solution if I dig deeper. But at least this one works for now.

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

About Nginx rewrite rule configuration of PHP’s Symfony and CodeIgniter frameworks

About CI framework Usage analysis of $this->load->library()

#

The above is the detailed content of Analysis on the method of integrating smarty and adodb in Codeigniter. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement custom middleware in CodeIgniter How to implement custom middleware in CodeIgniter Jul 29, 2023 am 10:53 AM

How to implement custom middleware in CodeIgniter

CodeIgniter middleware: Accelerate application responsiveness and page rendering CodeIgniter middleware: Accelerate application responsiveness and page rendering Jul 28, 2023 pm 06:51 PM

CodeIgniter middleware: Accelerate application responsiveness and page rendering

How to migrate and integrate projects in GitLab How to migrate and integrate projects in GitLab Oct 27, 2023 pm 05:53 PM

How to migrate and integrate projects in GitLab

PHP development: Using CodeIgniter to implement MVC pattern and RESTful API PHP development: Using CodeIgniter to implement MVC pattern and RESTful API Jun 16, 2023 am 08:09 AM

PHP development: Using CodeIgniter to implement MVC pattern and RESTful API

Oracle API integration strategy analysis: achieving seamless communication between systems Oracle API integration strategy analysis: achieving seamless communication between systems Mar 07, 2024 pm 10:09 PM

Oracle API integration strategy analysis: achieving seamless communication between systems

GitLab API integration and custom plug-in development tips GitLab API integration and custom plug-in development tips Oct 20, 2023 pm 05:30 PM

GitLab API integration and custom plug-in development tips

How to use the database query builder (Query Builder) in the CodeIgniter framework How to use the database query builder (Query Builder) in the CodeIgniter framework Jul 28, 2023 pm 11:13 PM

How to use the database query builder (Query Builder) in the CodeIgniter framework

CodeIgniter middleware: Provides secure file upload and download functions CodeIgniter middleware: Provides secure file upload and download functions Aug 01, 2023 pm 03:01 PM

CodeIgniter middleware: Provides secure file upload and download functions

See all articles