目前分類:Codeigniter (4)

瀏覽方式: 標題列表 簡短摘要

config檔底下:

$config['index_page'] = '';

 

 

.htaccess檔內:

<IfModule mod_rewrite.c>
<Files ~ "^.(htaccess|htpasswd)$">
    deny from all
</Files>

    Options -Indexes
    Options +FollowSymLinks

#允許解析文件中的SSI指令
    Options +Includes

#定義404,500錯誤頁面
    ErrorDocument 404 /404.htm
    ErrorDocument 500 /404.htm

#定義目錄索引頁面
    DirectoryIndex index.php
    order deny,allow
    RewriteEngine on

#設置根目錄
    #RewriteBase /你的ci目錄/

#去掉鏈接地址中index.php字符串
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond $1 !^(index\.php|css|flash|images|img|includes|js|public|language|robots\.txt)
    RewriteRule ^(.*)$ index.php/$1 [L]

</IfModule>

 

官方設定文件

文章標籤

incode 發表在 痞客邦 留言(0) 人氣()

基於久了又會忘記,趕快查一查記錄在此。

 

首先是Controller繼承。

置於application\core 底下

 

/* ----------------  開始 ---------------- */
class BASE_Controller extends CI_Controller
{
    function __construct() {
        parent::__construct();
        

        $this->load->helper(array('form','url'));
        $this->load->library('form_validation');
        
        /* 這是在模組用
        $obj =& get_instance();
        $obj->load->helper('url');
        */
        

    }

 

function show_menu($ml, $url="", $css_id="nav") {

        $base_url = $this->config->item('base_url');
        $full_url = $this->config->item('full_url');
        
        $menu_code = '<ul id="'.$css_id.'">';
        
        foreach($ml AS $k => $v) {
            $menu_code .= '<li>' . anchor($full_url.$url."/".$k,"$v") . '</li>';
        }
        
        $menu_code .= '</ul>';

        
        return $menu_code;
    
        
    }

abstract class Pages_Controller extends Base_Controller
{
    
    
    
    
}

abstract class Admin_Controller extends Base_Controller
{
    
    
    
    
}

/* ----------------  結束 ---------------- */

基本上這樣就能夠分別在不同的頁面(一般頁及後台頁)給不同的繼承。

 

 

置於控制器部分,我的思維是每個頁面(function)只要做好基本設置,佈署很快

class Admin extends Admin_Controller {

    function __construct() {
        parent::__construct();
        
        
    }
    
    //控制器名稱
    public $name = '管理端 ';
    public $path = 'index.php/admin';
    
    //管理區選單陣列
    public $nav = array
        (
            'index'        =>    '回前台',
            'admin'        =>    '首頁',

        
        );
    
    
    
    public function index()
    {
        //載入網站資訊
        $data['web_info'] = $this->web_info();
        $data['web_info']['page'] = $this->name .'首頁';
        $data['web_info']['theme'] = 'v1';
        
        //載入選單
        $menu = $this->nav;
        $data['menu'] = $this->show_menu($menu,$this->path);
        
        $data['content'] = '首頁';
        
        
        $this->load->view('admin',$data);
        
        
    }

 

在view部分則用includes

<?php $this->load->view('includes/header'); ?>
內容
<?php $this->load->view('includes/footer'); ?>

 

即可。

incode 發表在 痞客邦 留言(0) 人氣()

2.1版在core下建立MY_Controller.php檔案

內容為:

/*-------------------------------------*/

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

class FUN_Controller extends CI_Controller{

    public $site = array(
                "name" => "網站名稱",
                "tag" => "標籤"
    
            );
    public function __construct()
    {
        parent::__construct();
    }
 
    function FUN_Controller ()  {
        parent::Controller();
    }

}

/*-------------------------------------*/

 

Controller繼承

/*-------------------------------------*/

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

class Front extends FUN_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     *         http://example.com/index.php/welcome
     *    - or -  
     *         http://example.com/index.php/welcome/index
     *    - or -
     * Since this controller is set as the default controller in
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
    public function index()
    {
        echo $this->site['name']; //呼叫變數(全域)
        $this->load->view('Front',$data);
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/Front.php */

 

/*-------------------------------------*/

incode 發表在 痞客邦 留言(0) 人氣()

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

class Login extends CI_Model {

    /*
     *  調用方式
     *
     *
    ##########  Controllers 控制器各頁面  ##########
    if (!$this->session->userdata("admin")) redirect("member/login");
    
    ##########  Controllers 控制器部份  ##########
    public function login()
    {
        //檢查session值
        //echo $this->session->userdata("admin");
        //echo $this->session->userdata("uid");
        
        $data["path_name"] = "登入頁";
        $data['base_url'] = $this->config->item('base_url');
        $data['this_url'] = $data['base_url'] . "index.php/member/login";
        
            $check = array(
                'bl' => false,
                'msg' => ''
            );
            
            
                    if(isset($_POST['login']))
                    {
                        $logid = $this->input->post('login_id');     //對應view的表單
                        $logpwd = $this->input->post('login_pwd');

                        $this->load->model('Login');
                        $check = $this->Login->check($logid,$logpwd);//回傳參數bl(布林),以及msg 何種錯誤訊息
                        
                        if ($check['bl']){
                        $newdata = array( //寫入session的參數
                               'uid'  => $logid,
                               'admin'     => TRUE, //必須對應驗證
                               'logged_in' => TRUE
                           );

                        
                        $this->session->set_userdata($newdata);
                        redirect("member");  //成功後轉向
                        
                        }
                        
                    }


            $data['login_msg']= $check['msg'] ;
            $this->load->view('view_login',$data);
            
    }
    
    //登出
    public function logout()
    {
    
        $del_session = array('uid', 'admin', 'logged_in');
        
        $this->load->model('Login');
        $this->Login->logout($del_session);

        redirect('member/login');
    
    }
    
    ##########  Views 視圖部份  ##########
    
    <?php echo form_open_multipart('member/login');?>
      <label>
        帳號
          <input type="text" name="login_id" id="login_id" />
      </label>
      <br />
      <label>
        密碼
          <input type="password" name="login_pwd" id="login_pwd" />
      </label>
            <input type="hidden" name="login" value="1" id="login" />
      <br />
      <label>
        <input type="submit" name="button" id="button" value="登入" />
      </label>
    </form>
    <a href="logout">登出</a>
    
    

     *
     *
     */
    
    function __construct()
    {
        parent::__construct();
        $this->load->database();

        
    }
    
    
    function check($id,$pwd)
    {
        
        
        //讀取資料庫字串
        //$query = $this->db->get('check_db');
        
        $key = "@lib";
        
        $msg[0] = "登入成功";
        $msg[1] = "查無此帳號";
        $msg[2] = "請重新檢查帳號密碼";
        $msg[3] = "無法進行認證";
        
        if (true)//$query->num_rows() > 0)
        {
           //$check = $query->result_array();
           $check[0]['id'] = "abc";
           $check[0]['pwd'] = "ecfc8084e7c6c29e3ccf3f202635a197";
           $check[1]['id'] = "def";
           $check[1]['pwd'] = "d3bb34166864b20243936820ef8c5935";
           $check[2]['id'] = "user";
           $check[2]['pwd'] = "user001";
           
           
            $i = 1;
            foreach ($check as $row)
            {
                
                if($id!=$row['id'])
                {
                    $check['msg'] = $msg[1];
                    $check['bl'] = false;
                    continue;// 檢查帳號,不是就跳出此次循環
                    
                    
                }else{
                
                    $mdpwd = md5($pwd.$key);
                    if($mdpwd == $row['pwd'])
                    {
                        //驗證成功
                        $check['msg'] = $msg[0];
                        $check['id'] = $id;
                        //$check['level'] = "可以添加權限回傳";
                        $check['bl'] = true;
                    
                        
                        
                    }else{
                        echo "出錯訊息:" . $mdpwd ."<br/>";
                        $check['msg'] = $msg[2];
                        $check['bl'] = false;
                        
                    
                        
                    
                    }
                    
                    break;//檢測密碼,帳號對了就不再跑迴圈
                }
                
                
            }
            
            return $check;
           
        }else{
            
            $check['msg'] = $msg[3];
            $check['bl'] = false;
            return $check;        
        
        }
        
    }
    
    function logout($del_session)
    {
        
        foreach ($del_session as $row)
        {
        
            $this->session->unset_userdata($row);
        
        }
        
    
    }

    
    
    
    
    
}



/* End of file login.php */
/* Location: ./application/Model/login.php */

文章標籤

incode 發表在 痞客邦 留言(0) 人氣()