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(&#39;Smarty/Smarty.class.php&#39;);
// The setup.php file is a good place to load
// required application library files, and you
// can do that right here. An example:
// require(&#39;guestbook/guestbook.lib.php&#39;);
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(&#39;app_name&#39;, &#39;Guest Book&#39;);
 }
}
?>
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(&#39;BASEPATH&#39;)) exit(&#39;No direct script access allowed&#39;);
class Adodb
{
  function Adodb()
  {
    //$dsn="dbdriver://username:password@server/database"
    $dsn = &#39;mysql://user:password@localhost/xxxx&#39;;
    require_once("adodb/adodb.inc".EXT);
    $this->adodb =& ADONewConnection($dsn);
    $this->adodb->Execute("set NAMES &#39;utf8&#39;"); 
  }
}
?>
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(&#39;BASEPATH&#39;)) exit(&#39;No direct script access allowed&#39;);
$obj =& get_instance();
$obj->adodb = new Adodb($obj);
$obj->ci_is_loaded[] = &#39;adodb&#39;;
Copy after login

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

<?php if (!defined(&#39;BASEPATH&#39;)) exit(&#39;No direct script access allowed&#39;);
if ( ! class_exists(&#39;MySmarty&#39;))
{
  require_once(APPPATH.&#39;libraries/mysmarty&#39;.EXT);
}
$obj =& get_instance();
$obj->mysmarty = new MySmarty();
$obj->ci_is_loaded[] = &#39;mysmarty&#39;;
?>
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(&#39;mysmarty&#39;);
  $this->load->library(&#39;adodb&#39;);
 }
 function index()
 {
 $this->load->library(&#39;adodb&#39;);
 $row = $this->adodb->adodb->getrow(&#39;SELECT * FROM admin&#39;);
    $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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

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 Introduction: In modern web development, middleware plays a vital role in applications. They can be used to perform some shared processing logic before or after the request reaches the controller. CodeIgniter, as a popular PHP framework, also supports the use of middleware. This article will introduce how to implement custom middleware in CodeIgniter and provide a simple code example. Middleware overview: Middleware is a kind of request

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 Introduction: In the software development process, project migration and integration is an important task. As a popular code hosting platform, GitLab provides a series of convenient tools and functions to support project migration and integration. This article will introduce the specific steps for project migration and integration in GitLab, and provide some code examples to help readers better understand. 1. Project migration Project migration is to migrate the existing code base from a source code management system to GitLab

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: Accelerating Application Responsiveness and Page Rendering Overview: As web applications continue to grow in complexity and interactivity, developers need to use more efficient and scalable solutions to improve application performance and responsiveness. . CodeIgniter (CI) is a lightweight PHP-based framework that provides many useful features, one of which is middleware. Middleware is a series of tasks that are performed before or after the request reaches the controller. This article will introduce how to use

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

Introduction to the method of using the database query builder (QueryBuilder) in the CodeIgniter framework: CodeIgniter is a lightweight PHP framework that provides many powerful tools and libraries to facilitate developers in web application development. One of the most impressive features is the database query builder (QueryBuilder), which provides a concise and powerful way to build and execute database query statements. This article will introduce how to use Co

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

OracleAPI integration strategy analysis: To achieve seamless communication between systems, specific code examples are required. In today's digital era, internal enterprise systems need to communicate with each other and share data, and OracleAPI is one of the important tools to help achieve seamless communication between systems. This article will start with the basic concepts and principles of OracleAPI, explore API integration strategies, and finally give specific code examples to help readers better understand and apply OracleAPI. 1. Basic Oracle API

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's API integration and custom plug-in development skills Introduction: GitLab is an open source code hosting platform that provides a rich API interface for developers to use to facilitate integration and custom plug-in development. This article will introduce how to integrate GitLab's API and some tips on custom plug-in development, and provide specific code examples. 1. Obtain API access token for GitLab's API integration. Before API integration, you first need to obtain GitLab's API access token. beat

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

As web applications continue to evolve, it is important to develop applications more quickly and efficiently. And, as RESTful API is widely used in web applications, it is necessary for developers to understand how to create and implement RESTful API. In this article, we will discuss how to implement MVC pattern and RESTful API using CodeIgniter framework. Introduction to MVC pattern MVC (Model-Vie

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 Introduction: In the process of web application development, file upload and download are very common functions. However, for security reasons, handling file uploads and downloads often requires additional security measures. CodeIgniter is a popular PHP framework that provides a wealth of tools and libraries to support developers in building secure and reliable web applications. This article will introduce how to use CodeIgniter middleware to implement secure files

See all articles