CI架構擴充系統核心類的方法分析_php執行個體

來源:互聯網
上載者:User

本文執行個體講述了CI架構擴充系統核心類的方法。分享給大家供大家參考,具體如下:

首先你系統擴充類是放在application/core下面的,本來系統核心類是CI_Controller,所以你不能以CI_開頭了,你需要開啟 application/config/config.php 修改:

$config['subclass_prefix'] = 'MY_';

為你的首碼!

一些公用的模組就可以卸載自己的核心類裡面了!

這幾天讀了Dilicms(輕量級的後台架構),比如說他的後台擴充類是這樣的:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');abstract class Admin_Controller extends CI_Controller{  /**  * _admin  * 儲存當前登入使用者的資訊  *  * @var object  * @access public  **/  public $_admin = NULL;  /**  * 建構函式  *  * @access public  * @return void  */  public function __construct()  {    parent::__construct();    $this->load->library('session');    $this->settings->load('backend');    $this->load->switch_theme(setting('backend_theme'));    $this->_check_login();    $this->load->library('acl');    $this->load->library('plugin_manager');  } /**  * 檢查使用者是否登入  *  * @access protected  * @return void  */  protected function _check_login()  {    if ( ! $this->session->userdata('uid'))    {       redirect(setting('backend_access_point') . '/login');    }    else    {      $this->_admin = $this->user_mdl->get_full_user_by_username($this->session->userdata('uid'), 'uid');      if ($this->_admin->status != 1)      {        $this->session->set_flashdata('error', "此帳號已被凍結,請聯絡管理員!");        redirect(setting('backend_access_point') . '/login');      }    }  } /**  * 載入視圖  *  * @access protected  * @param string  * @param array  * @return void  */  protected function _template($template, $data = array())  {    $data['tpl'] = $template;    $this->load->view('sys_entry', $data);  } /**  * 檢查許可權  *  * @access protected  * @param string  * @return void  */  protected function _check_permit($action = '', $folder = '')  {    if ( ! $this->acl->permit($action, $folder))    {      $this->_message('對不起,你沒有訪問這裡的許可權!', '', FALSE);    }  } /**  * 資訊提示  *  * @access public  * @param string  * @param string  * @param bool  * @param string  * @return void  */  public function _message($msg, $goto = '', $auto = TRUE, $fix = '')  {    if($goto == '')    {      $goto = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : site_url();    }    else    {      $goto = strpos($goto, 'http') !== false ? $goto : backend_url($goto);      }    $goto .= $fix;    $this->_template('sys_message', array('msg' => $msg, 'goto' => $goto, 'auto' => $auto));    echo $this->output->get_output();    exit();  }}

它就把一些公用資訊入驗證,登陸,跳轉放在Admin_Controller裡面,於是我在寫前台的時候也是這樣操作,擴充了Font_Controller:

abstract class Font_Controller extends CI_Controller{ public function __construct() {  parent::__construct(); } //前台載入視圖方法 public function _template($template, $data = array()){  $data['nav'] = $this->get_nav();  $data['tpl'] = $template;  $this->load->view('default',$data); } //公用導航資訊 public function get_nav(){ } //公用友情連結資訊 public function get_friendlink(){ }}

提示,一些經常用到得工具和函數可以放在helper中!

更多關於CodeIgniter相關內容感興趣的讀者可查看本站專題:《codeigniter入門教程》、《CI(CodeIgniter)架構進階教程》、《php優秀開發架構總結》、《ThinkPHP入門教程》、《ThinkPHP常用方法總結》、《Zend FrameWork架構入門教程》、《php物件導向程式設計入門教程》、《php+mysql資料庫操作入門教程》及《php常見資料庫操作技巧匯總》

希望本文所述對大家基於CodeIgniter架構的PHP程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.