search
HomeBackend DevelopmentPHP Tutorialci framework (1), ci framework (_PHP tutorial

ci framework (1), ci framework (_PHP tutorial

Jul 13, 2016 am 10:23 AM
coresystemonecoreframeofTable of contentsprogramstructure

ci framework (1), ci framework (

ci directory structure                                                                                                                      

Visit form
|-----<span>system</span><span>                框架程序目录  
    </span>|-----<span>core              框架的核心程序  
        </span>|-----CodeIgniter.<span>php   引导性文件  
        </span>|-----Common.<span>php    加载基类库的公共函数  
        </span>|-----Controller.<span>php    基控制器类文件:CI_Controller  
        </span>|-----Model.<span>php     基模型类文件:CI_Model  
        </span>|-----Config.<span>php    配置类文件:CI_Config  
        </span>|-----Input.<span>php     输入类文件:CI_Input  
        </span>|-----Output.<span>php    输出类文件:CI_Output  
        </span>|-----URL.<span>php       URL类文件:CI_URl  
        </span>|-----Router.<span>php    路由类文件:CI_Router  
        </span>|-----Loader.<span>php    加载类文件:CI_Loader  
    </span>|-----<span>helpers           辅助函数  
        </span>|-----url_helper.<span>php    url相关的辅助函数,如:创建url的辅助函数  
        </span>|-----captcha_helper.<span>php创建图形验证码的辅助函数  
    </span>|-----<span>libraries         通用类库  
        </span>|-----Pagination.<span>php    通用分页类库  
        </span>|-----Upload.<span>php    通用文件上传类库  
        </span>|-----Image_lib.<span>php 通用图像处理类库  
        </span>|-----Session.<span>php   通用session类库  
    </span>|-----<span>language          语言包  
    </span>|-----<span>database          数据库操作相关的程序  
        </span>|-----DB_active_rec.<span>php 快捷操作类文件(ActiveRecord)  
    </span>|-----<span>fonts             字库  
      
</span>|-----<span>application           项目目录  
    </span>|-----<span>core              项目的核心程序  
    </span>|-----<span>helpers           项目的辅助函数  
    </span>|-----<span>libraries         通用类库  
    </span>|-----<span>language          语言包  
    </span>|-----<span>config            项目相关的配置  
        </span>|-----config.<span>php    项目相关的配置文件     
        </span>|-----database.<span>php  数据库相关的配置文件  
        </span>|-----autoload.<span>php  设置自动加载类库的配置文件  
        </span>|-----constants.<span>php 常量配置文件  
        </span>|-----routes.<span>php    路由配置文件  
    </span>|-----<span>controllers       控制器目录  
        </span>|-----welcome.<span>php   控制器文件,继承CI_Controller  
    </span>|-----<span>models            模型目录  
        </span>|-----welcome_model.<span>php 模型文件,继承CI_Model  
    </span>|-----<span>views             视图目录  
        </span>|-----welcome.php   视图模板文件,默认后缀名为.<span>php  
    </span>|-----<span>cache             存放数据或模板的缓存文件  
    </span>|-----<span>errors            错误提示模板  
    </span>|-----<span>hooks             钩子,在不修改系统核心文件的基础上扩展系统功能  
    </span>|-----<span>third_party       第三方库  
    </span>|-----<span>logs              日志  
  
</span>|-----index.php             入口文件

Add function in applicationcontrollerswelcome.php:

Entry file.php/controller/action
<span>public</span> <span>function</span><span> hello()    
{    
    </span><span>echo</span> "test"<span>;   
}</span>

ci framework (1), ci framework (_PHP tutorial Create a new controller yourself, hello.php:

Note:

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

    </span><span>class</span> Hello <span>extends</span><span> CI_Controller 
    {
        </span><span>public</span> <span>function</span> sayHello(<span>$name</span><span>)
        {
            </span><span>echo</span> <span>$name</span>,",Hello World"<span>;
        }
    }
</span>?>

ci framework (1), ci framework (_PHP tutorial Method names starting with an underscore cannot be accessed successfully and can only be accessed indirectly.

Only public modified methods can be accessed.
  • Try not to use methods with the same name as the class, as they will be treated as constructors.
  • Loading view                                                                                         
applicationviewsview_test.php or applicationviewsviewtest.php (this method is mainly to facilitate the same type of views to be placed in the same folder for easy management)

Controller:

or:

<span><</span><span>html</span><span>></span>
<span><</span><span>head</span><span>></span>
<span></</span><span>head</span><span>></span>
<span><</span><span>body</span><span>></span><span>
test_ci_hello_world
</span><span></</span><span>body</span><span>></span>
<span></</span><span>html</span><span>></span>
Effect:

<span>public</span> <span>function</span><span> addView()
{
    </span><span>$this</span>->load->view("view_test"<span>);
}</span>
Assign variables                                                                                                                                                                    because

Transmit data from the controller to the view, controller:

<span>public</span> <span>function</span><span> addView2()
{
    </span><span>$this</span>->load->view("view/test"<span>);
}</span>

View:

ci framework (1), ci framework (_PHP tutorialEffect:

public

<span>public</span> <span>function</span><span> addView()
        {
            </span><span>$this</span>->load->vars("title","value"<span>);
            </span><span>$list</span> = <span>array</span><span>(
            </span><span>array</span>('id'=>1,'name'=>'jack','email'=>'123@123.com'),
            <span>array</span>('id'=>2,'name'=>'jack2','email'=>'1233@123.com'),
            <span>array</span>('id'=>3,'name'=>'jack3','email'=>'12333@123.com'<span>)
            );
            </span><span>$data</span>['new_title']="标题"<span>;
            </span><span>$data</span>['list']=<span>$list</span><span>;
            </span><span>$this</span>->load->vars(<span>$data</span><span>);
            
            </span><span>$this</span>->load->view("view_test"<span>);
        }</span>
function

addView() {
<html>
    <head>
    </head>
    <body>
        <h1><?php <span>echo</span> <span>$title</span>;?></h1>
        <h1><?php <span>echo</span> <span>$new_title</span>;?></h1><span>
        test_ci_hello_world
        </span><table>
            <?php <span>foreach</span>(<span>$list</span> <span>as</span> <span>$item</span>):?>
            <tr>
                <td><?=<span>$item</span>['id']?></td>    
                <td><?=<span>$item</span>['name']?></td>
                <td><?=<span>$item</span>['email']?></td>                
            </tr>
            <?php <span>endforeach</span>;?>
        </table>
    </body>
</html>

$this

->load->vars("title","value"

); ci framework (1), ci framework (_PHP tutorial$list = array( array('id'=>1,'name'=>'jack','email'=>'123@123.com'), array('id'=>2,'name'=>'jack2','email'=>'1233@123.com'), array('id'=>3,'name'=>'jack3','email'=>'12333@123.com') ); $data['new_title']="title"; $data['list']=$list; $this->load->vars($data); $this->load->view("view_test"); $this->load->view("footer"); } View view_test: View footer: Display effect: uri parameter acquisition                                                               

Controller:

<html>
    <head>
    </head>
    <body>
        <h1><?php <span>echo</span> <span>$title</span>;?></h1>
        <h1><?php <span>echo</span> <span>$new_title</span>;?></h1><span>
        test_ci_hello_world
        </span><table>
            <?php <span>foreach</span>(<span>$list</span> <span>as</span> <span>$item</span>):?>
            <tr>
                <td><?=<span>$item</span>['id']?></td>    
                <td><?=<span>$item</span>['name']?></td>
                <td><?=<span>$item</span>['email']?></td>                
            </tr>
            <?php <span>endforeach</span>;?>
        </table>

Effect:

ci framework (1), ci framework (_PHP tutorial加载数据库                                                                                 

这个操作在MVC中是放到model中做的。

在\application\config\database.php中配置数据库参数,注意dbprefixswap_pre这两个参数。在php中写的是前缀,会默认当作swap_pre,然后放到数据库中的时候会转成dbprefix,但是最好两个都弄成一样的。

还有$active_group,默认是default,如果要连接两个数据库,把default另外取名,然后在函数中写明参数就OK。

必须继承数据核心类CI_Model,同时重载父类中的构造方法。

<span>class</span> Model_name <span>extends</span><span> CI_Model
{
    </span><span>function</span><span> __construct()
    {
        parent</span>::<span>__construct();
    }
}</span>

在每次使用数据库的时候,都需要加载一次数据库:

<span>$this</span>->load->database();

为了方便,可以将数据库的加载设置成自动加载,在\application\config\autoload.php中。

<span>$autoload</span>['libraries'] = <span>array</span>('database');

对于数据库访问对象,装载到超级对象的属性中 $this->db

<span>$res</span> = <span>$this</span>->db->query(<span>$sql</span>);<span>//</span><span>返回对象</span>
<span>$res</span>->result();<span>//</span><span>返回数组,数组中是一个一个的对象</span>
<span>$res</span>->result_array();<span>//</span><span>返回二维数组,里面是关联数组</span>
<span>$res</span>->row();<span>//</span><span>返回第一条数据,直接是一个对象</span>

AR操作数据库                                                                              

在database.php文件中,将$active_recoed的值改为TRUE,这样就可以使用AR了。

<span>//</span><span>查询</span>
<span>public</span> <span>function</span><span> index()
{
    </span><span>$res</span> = <span>$this</span>->db->get('表名');<span>//</span><span>这里自动调用前缀</span>
    <span>foreach</span>(<span>$res</span>->result() <span>as</span> <span>$item</span><span>)
    {
        </span><span>echo</span> <span>$item</span>->name."<br />"<span>;
    }
}</span>
<span>//</span><span>插入</span>
<span>public</span> <span>function</span><span> index()
{
    </span><span>$data</span>=<span>array</span><span>(
        </span>'name'=>'lisi',
        'password'=><span>md5</span>('lisi'<span>)
    );
    </span><span>$bool</span> = <span>$this</span>->db->insert("表名",<span>$data</span><span>);
    </span><span>var_dump</span>(<span>$bool</span><span>);
}</span>
<span>//</span><span>更新</span>
<span>public</span> <span>function</span><span> index()
{
    </span><span>$data</span>=<span>array</span><span>(
        </span>'name'=>'wangwu',
        'password'=><span>md5</span>('wangwu'<span>)
    );

    </span><span>$bool</span> = <span>$this</span>->db->update('表名',<span>$data</span>,<span>array</span>('id'=>3<span>));
    </span><span>var_dump</span>(<span>$bool</span><span>);
}</span>
<span>//</span><span>删除</span>
<span>$bool</span> = <span>$this</span>->db->delete('表名',<span>array</span>('id'=>2<span>));
</span><span>var_dump</span>(<span>$bool</span>);

新手助PHP 程序 CI框架开发 进来看一眼吧

常规的方法是ul嵌套,即主菜单ul-li里嵌套子菜单ul,要用到两级循环
首先循环主菜单,要有固定的条件来判断出主菜单,比如主菜单的uid==0或者其它。。。


  • 栏目名称
    if($news_item['uid'] == 0){ //判断并得到主菜单

    echo "
  • ".$news_item['title'] . '
      ';
      foreach ($news as $child_item): //循环二次
      if($news_item['id'] == $child_item['uid']){ //判断并得到对应子菜单
      echo "
    • "."ss".$child_item['title']."";
      }
      endforeach;
      echo "";
      }

      endforeach; ?>


      当然这仅限于两级菜单,多级或无限极,可以使用函数递归
      function menu($uid=0){ //设置缺省从主菜单开始
      global $news;
      foreach ($news as $news_item):

      if($news_item['uid'] == $uid){

      echo "
    • ".$news_item['title'] . '
        ';
        menu($news_item['id']); //递归调用

        echo "";
        }

        endforeach;

        }
        ------ 调用方法 ------------------------------
          >
           

          CI框架想创建一个model类继承CI_Modle,我创建在app/core/MY_Model,直接报错了

          Doesn’t the class name of MY_Controller need to be the same?

          www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/836503.htmlTechArticleci framework (1), ci framework (ci directory structure |----- system framework program directory |- ---- core The core program of the framework |-----CodeIgniter.php boot file|-----Common.php loads the base class library...
Statement
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
How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

How does PHP handle object cloning (clone keyword) and the __clone magic method?How does PHP handle object cloning (clone keyword) and the __clone magic method?Apr 17, 2025 am 12:24 AM

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP vs. Python: Use Cases and ApplicationsPHP vs. Python: Use Cases and ApplicationsApr 17, 2025 am 12:23 AM

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

Describe different HTTP caching headers (e.g., Cache-Control, ETag, Last-Modified).Describe different HTTP caching headers (e.g., Cache-Control, ETag, Last-Modified).Apr 17, 2025 am 12:22 AM

Key players in HTTP cache headers include Cache-Control, ETag, and Last-Modified. 1.Cache-Control is used to control caching policies. Example: Cache-Control:max-age=3600,public. 2. ETag verifies resource changes through unique identifiers, example: ETag: "686897696a7c876b7e". 3.Last-Modified indicates the resource's last modification time, example: Last-Modified:Wed,21Oct201507:28:00GMT.

Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1?Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1?Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP: An Introduction to the Server-Side Scripting LanguagePHP: An Introduction to the Server-Side Scripting LanguageApr 16, 2025 am 12:18 AM

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP and the Web: Exploring its Long-Term ImpactPHP and the Web: Exploring its Long-Term ImpactApr 16, 2025 am 12:17 AM

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

Why Use PHP? Advantages and Benefits ExplainedWhy Use PHP? Advantages and Benefits ExplainedApr 16, 2025 am 12:16 AM

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

See all articles

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)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools