Heim > Backend-Entwicklung > PHP-Tutorial > [CI]CodeIgniter快速开发指南,cicodeigniter_PHP教程

[CI]CodeIgniter快速开发指南,cicodeigniter_PHP教程

WBOY
Freigeben: 2016-07-13 10:09:23
Original
973 Leute haben es durchsucht

[CI]CodeIgniter快速开发指南,cicodeigniter

---------------------------------------------------------------------------------------------------------

使用CI以来最强烈的感受是其彻底的MVC设计, 举个例子 : 在application/modesl目录里, 写我们的模型操作, 统一继承CI_Model.

而在控制器里只写逻辑, 无法直接操作数据库, 需要数据直接调用模型, 最后是调用模板.

 

下面分别展示模型, 控制器, 和视图间的协作.

<span>/*</span><span>*
 * 用户模型, 完整CURD示例
 * @Chenwei
 </span><span>*/</span>
<span>class</span> User_model <span>extends</span><span> CI_model<br />{
    </span><span>public</span> <span>function</span><span> __construct()
    {
        parent</span>::<span>__constrcut();
    }

    </span><span>/*</span><span>*
     * 查询用户信息, 这里不建议使用单一id参数作为条件, 为了便于控制器自己组装条件复用此模型方法
     * @param array 格式如: $where = array('id'=>1);
     * @return array
     </span><span>*/</span>
    <span>public</span> <span>function</span> userInfo(<span>$where = array()</span><span>)
    {
        </span><span>if</span>(<span>$where</span> && <span>is_array</span>(<span>$where</span><span>))
        {
            </span><span>$res</span> = <span>$this</span>->db->select('id, username, age')->where(<span>$where</span>)->get('users'<span>);

            </span><span>return</span> <span>$res</span>->result_array(); <span>//</span><span>以二维数组形式返回结果</span>
<span>        }
        </span><span>else</span><span>
        {<br />       $res = $this->db->select('id, username, age')->get('users');<br />
            </span><span>return</span> <span>$res->result_array()</span><span>;
        }
    }

    </span><span>/*</span><span>*
     * 添加用户
     * @param array 格式如: $data = array('username'=>'Chenwei', 'age'=>'18');
     * @reteurn bool
     </span><span>*/</span>
    <span>public</span> <span>function</span> userAdd(<span>$data</span><span>)
    {
        </span><span>if</span>(<span>$data</span> && <span>is_array</span>(<span>$data</span><span>))
        {
            </span><span>$bool</span> = <span>$this</span>->db->insert('users', <span>$data</span><span>);

            </span><span>return</span> <span>$bool</span><span>;
        }
        </span><span>else</span><span>
        {
            </span><span>return</span> <span>false</span><span>;
        }
    }

    </span><span>/*</span><span>*
     * 删除用户
     * @param int $id
     * @reteurn bool
     </span><span>*/</span>
    <span>public</span> <span>function</span> userDel(<span>$id</span><span>)
    {
        </span><span>if</span>(<span>$id</span><span>)
        {
            </span><span>$where</span> = <span>array</span>('id'=><span>$id</span><span>);

            </span><span>$bool</span> = <span>$this</span>->db->where(<span>$where</span>)->delete('users'<span>);

            </span><span>return</span> <span>$bool</span><span>;
        }
        </span><span>else</span><span>
        {
            </span><span>return</span> <span>false</span><span>;
        }
    }

    </span><span>/*</span><span>*
     * 修改用户
     * @param array $where 条件
     * @param array $data 新数据
     * @reteurn bool
     </span><span>*/</span>
    <span>public</span> <span>function</span> userEdit(<span>$where</span>, <span>$data</span><span>)
    {
        </span><span>if</span>(<span>$where</span> && <span>is_array</span>(<span>$where</span><span>))
        {
            </span><span>$bool</span> = <span>$this</span>->db->where(<span>$where</span>)->update('users', <span>$data</span><span>);

            </span><span>return</span> <span>$bool</span><span>;
        }
        </span><span>else</span><span>
        {
            </span><span>return</span> <span>false</span><span>;
        }
    }
}

</span><span>/*</span><span>*
 * 几点注意:
 * 1. 模型类名字User_model首字母大写, 其余字母小写, 继承基础模型类CI_Model
 * 2. 类文件名 application/models/user_model.php
 * 3. 控制器中如何载入此模型 : <br />    $this->load->model('User_model', 'user'); 这是以user为对象名引入;<br />    $this->load->model('User_model'); 这是默认以User_model为对象名引入. 模型文件支持子目录;<br />    如果类文件在application/models/blog/user_model.php中, 可以这样引入: $this->load->model('blog/User_model');
 * 4. 如果有需要, 你可以设置自动加载, 在 application/config/autoload.php文件中.<br /> * 5. 如果没有设置自动连接数据库, 加在模型的时候可以设置连接, 像这样 $this->load->model('User_model', '', TRUE);
 </span><span>*/<br /><br />Ps:<br />这里是一个联合查询的例子, 有需要可以尝试:<br />$res = $this->db->select('p.id, p.uid, p.order_no, p.amount, p.pay_way, p.pay_type, p.pay_bank, p.pay_time, p.goods_type, p.contact_tel, p.detail_desc, p.add_time, u.username')->from('payment as p')->join('users as u', 'p.uid = u.id')->order_by('p.id', 'desc')->get();</span>
Nach dem Login kopieren

<span>/*</span><span>*
 * 用户控制器, CURD示例
 * @Chenwei
 </span><span>*/</span>
<span>class</span> Users <span>extends</span><span> CI_Controller
{
    </span><span>public</span> <span>function</span><span> __construct()
    {
        parent</span>::<span>__construct();

        </span><span>$this</span>->load->model('User_model', 'user'<span>);
    }
    
    </span><span>/*</span><span>*
     * 用户列表
     </span><span>*/</span>
    <span>public</span> <span>function</span><span> index()
    {
        </span><span>$data</span>['user_list'] = <span>$this</span>->user-><span>userInfo();

        </span><span>$this</span>->load->view('user_list', <span>$data</span><span>);  //调用模板, 并将数据输出到前台
    }

    </span><span>/*</span><span>*
     * 添加用户
     </span><span>*/</span>
    <span>public</span> <span>function</span><span> user_add()
    {
        </span><span>$data</span> = <span>array</span><span>(
            </span>'username'=><span>$this</span>->input->post('name'<span>);
            </span>'age'=><span>intval</span>(<span>$this</span>->input->post('age'<span>));
        );

        </span><span>$bool</span> = <span>$this</span>->user->userAdd(<span>$data</span><span>);

        </span><span>if</span>(<span>$bool</span><span>)
        {
           </span><span>$this</span>->show_tips('操作成功 !'<span>);
        }
        </span><span>else</span><span>
        {
            </span><span>$this</span>->show_tips('操作失败 !'<span>);
        }
    }

    </span><span>/*</span><span>*
     * 修改用户
     </span><span>*/</span>
    <span>public</span> <span>function</span><span> user_edit()
    {
        </span><span>$id</span> = <span>$this</span>->input->post('id'<span>);

        </span><span>$data</span> = <span>array</span><span>(
            </span>'username'=><span>$this</span>->input->post('name'<span>);
            </span>'age'=><span>intval</span>(<span>$this</span>->input->post('age'<span>));
        );

        </span><span>if</span>(<span>$id</span><span>)
        {<br />       $where = array('id'=>$id);<br />
            </span><span>$bool</span> = <span>$this</span>->user->userEdit(<span>$where</span>, <span>$data</span><span>);

            </span><span>if</span>(<span>$bool</span><span>)
            {
               </span><span>$this</span>->show_tips('操作成功 !'<span>);
            }
            </span><span>else</span><span>
            {
                </span><span>$this</span>->show_tips('操作失败 !'<span>);
            }
        }
        </span><span>else</span><span>
        {
            </span><span>$this</span>->show_tips('非法操作 !'<span>);
        }
    }

    </span><span>/*</span><span>*
     * 删除用户
     </span><span>*/</span>
    <span>public</span> <span>function</span><span> user_del()
    {
        </span><span>$id</span> = <span>$this</span>->input->post('id'<span>);

        </span><span>$bool</span> = <span>$this</span>->user->userDel(<span>$id</span><span>);

        </span><span>if</span>(<span>$bool</span><span>)
        {
           </span><span>$this</span>->show_tips('操作成功 !'<span>);
        }
        </span><span>else</span><span>
        {
            </span><span>$this</span>->show_tips('操作失败 !'<span>);
        }
    }
}

</span><span>/*</span><span>*<br /> * 几点注意:
 * 1. 控制器文件在 application/controller/users.php , 支持子目录
 * 2. 控制器名首字母必须大写, 且必须继承CI_Controller
 * 3. 前后台权限控制都在application/core/MY_Controller.php文件中,<br />    定义两个控制器, 分别用于前台和后台, 继承CI_Controller , 其余都只需继承这两个自定义的控制器即可.
 * 4. 定义默认控制器, 在 application/config/route.php
 </span><span>*/</span>
Nach dem Login kopieren

<span>/*</span><span>*
 * 视图层 示例
 * @Chenwei
 </span><span>*/</span>
<?<span>php
    </span><span>$this</span>->load->view('header'<span>);
</span>?>

<!-- 简单的输出 -->
<div>
    <table>
        <?php <span>if</span>(<span>$user_list</span>):?>
            <?php <span>foreach</span>(<span>$user_list</span> <span>as</span> <span>$v</span>):?>
            <tr><td><?=<span>$v</span>['username'];?></td></tr>
            <?php <span>endforeach</span>;?>
        <?php <span>endif</span>;?>
    </table>
</div>

<?<span>php
    </span><span>$this</span>->load->view('header'<span>);
</span>?>

<span>/*</span><span>*
 * 几点注意:
 * 1. 模板中可以直接使用控制器中分配的变量, 使用CI系统的所有函数和方法.
 * 2. 开启CI短标签支持后, 即使php未开启支持, CI也会帮我们自动解析, 可以放心使用.
 </span><span>*/</span>
Nach dem Login kopieren

可能存在手误, 以上Code不要直接复制使用; 更多CI的实用用法, 可以随时去查阅CI手册.

 

Link: http://www.cnblogs.com/farwish/p/3991419.html

@黑眼诗人 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/946584.htmlTechArticle[CI]CodeIgniter快速开发指南,cicodeigniter --------------------------------------------------------------------------------------------------------- 使用CI以来最强烈的...
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage