2012-12-13 42 views
3

我正在創建一個應用程序並處理MY_Controller中的常見事情。我正在使用消息庫來顯示常見消息。消息庫,My_Controller和__remap問題

這裏是MY_Controller.php:

<?php 
class MY_Controller extends CI_Controller { 

    public $data   = array(); 
    public $view   = TRUE; 
    public $theme   = FALSE; 
    public $layout   = 'default';  
    protected $redirect;  

    protected $models  = array(); 
    protected $controller_model; 
    protected $controller_class; 
    protected $controller_library; 

    protected $controller_name; 

    protected $partials  = array(
            'meta'  => 'partials/meta', 
            'header'  => 'partials/header', 
            'navigation' => 'partials/navigation', 
            'content'  => 'partials/content', 
            'footer'  => 'partials/footer' 
            ); 

    public function __construct() 
    { 
     parent::__construct(); 
     $this->output->enable_profiler(true); 

     $this->load->helper('inflector'); 
     $this->load->helper('url');  

     $this->controller_class = $this->router->class; 

     if(count($this->models)>0) 
     { 
      foreach ($this->models as $model) 
      { 
       if (file_exists(APPPATH . 'models/' . $model . '.php')) 
       { 
        $this->controller_model = $model; 
        $this->load->model($model); 
       } 
      } 
     }else{ 

      if (file_exists(APPPATH . 'models/' . $this->controller_model . '.php')) 
      { 
       $this->load->model($this->controller_model); 
      }   
     }  

     $this->controller_name = $this->router->fetch_class(); 
     $this->action_name = $this->router->fetch_method();  

    } 

    public function _remap($method, $parameters) 
    { 
     if (method_exists($this, $method)) 
     { 
      $this->run_filter('before', $parameters); 
      $return = call_user_func_array(array($this, $method),$parameters); 
      $this->run_filter('after', $parameters);  
     }else{ 
      show_404(); 
     } 

     if($this->theme === TRUE OR $this->theme === '') 
     { 
      $this->theme = 'default'; 
      $this->template->set_theme($this->theme);  
     }else if(strlen($this->theme) > 0){ 
      $this->template->set_theme($this->theme); 
     }else{ 

     }  

     if($this->layout === TRUE OR $this->layout === '') 
     { 
      $this->layout = 'default'; 
      $this->template->set_layout($this->layout);  
     }else if(strlen($this->layout) > 0){ 
      $this->template->set_layout($this->layout); 
     }else{ 

     } 

     if(isset($this->partials)) 
     { 

      foreach($this->partials as $key => $value) 
      { 
       $this->template->set_partial($key,$value); 
      } 
     }  

     if(isset($this->data) AND count($this->data)>0) 
     { 
      foreach($this->data as $key => $value) 
      { 
       if(!is_object($value)) 
       { 
        $this->template->set($key,$value); 
       } 
      } 
     } 

     if($this->view === TRUE OR $this->view === '') 
     { 
      if($this->parse == TRUE) 
      { 
       $parse_string = $this->template->build($this->router->method ,'' ,$this->parse); 
       echo $this->parse($parse_string); 
      }else{ 
       $this->_call_content($this->router->method); 
       $this->template->build($this->router->method,array()); 
      } 

     }else if(strlen($this->view) > 0){ 

      if($this->parse == TRUE){ 

       $parse_string = $this->template->build($this->router->method ,'' ,$this->parse); 
       echo $this->parse($parse_string); 
      }else{ 
       $view = $this->view; 
       $this->_call_content($view); 
       $this->template->build($view,array()); 
      }   

     }else{ 
      $checkpoint = $this->session->flashdata('exit'); 
      if($checkpoint){ 
       exit(); 
      }else{ 
       $this->session->set_flashdata('exit',TRUE); 
      } 

      $this->redirect(); 
     } 

    } 

    public function _call_content($view) 
    { 
     $value = $this->load->view($view,$this->data,TRUE); 
     $this->template->set('content',$value); 
    } 

    /* Common Controller Functions */ 
    public function index() 
    { 
     $data[$this->controller_model] = $this->{$this->controller_model}->get_all(); 

     $this->data  = $data; 
     $this->view  = TRUE; 

     if($this->input->is_ajax_request() || $this->session->flashdata('ajax')){ 
      $this->layout = FALSE;  
     }else{ 
      $this->layout = TRUE; 
     } 

    } 

    public function form() 
    { 
     if($this->input->is_ajax_request() OR !$this->input->is_ajax_request()) 
     { 
      $this->load->helper('inflector'); 
      $id      = $this->uri->segment(4,0); 

      if($data = $this->input->post()){ 
       $result = $this->{$this->controller_model}->validate($data); 
       if($result){ 
        if($id > 0){ 

        }else{ 
         $this->{$this->controller_model}->insert($data); 
        } 
        $this->message->set('message','The page has been added successfully.'); 
        $this->view  = FALSE; 
        $this->layout  = FALSE; 
        $this->redirect = "index"; 
       }else{ 
        $this->message->set('message','The Red fields are required'); 
       } 
      } 
      $row = $this->{$this->controller_model}->where($id)->get(); 

      $this->data[$module_name]= $row; 
     } 
    } 

    public function delete() 
    { 
     $id = $this->uri->segment(3,0); 

     if($id != 0){ 
      $this->{$this->controller_model}->delete($id); 
     } 

     $this->view  = FALSE; 
     $this->layout  = FALSE; 
    }  

    public function redirect() 
    { 
     redirect($this->redirect); 
    } 

    public function call_post($data) 
    { 
     foreach($data as $key => $row){ 
      $_POST[$key] = $row; 
     } 
    } 

    public function query() 
    { 
     echo $this->db->last_query(); 
    } 

    public function go($data = '') 
    { 
     if(isset($data)){ 
      echo '<pre>'; 
      print_r($data);  
     }else{ 
      echo '<pre>'; 
      print_r($this->data); 
     } 
    } 
} 

/**/ 

正如你可以看到我使用菲爾鱘魚的模板庫中,我處理與Jamierumbelow的技術佈局。 當我在表單插入失敗時設置消息很好。我可以這樣

echo $this->message->display(); 

當我在部分導航它不會顯示該消息調用它在_remap顯示它在控制器其工作finebut。可能是什麼問題。我在My_Controller的不同地方嘗試過。它的工作正常,但不是在部分甚至我已經嘗試了它在失敗的形式,我再次加載。 這是我使用

https://github.com/jeroenvdgulik/codeigniter-message

在這裏,我是我的導航部分

<nav> 
    <div id = "navigation"> 
     <ul id="menubar"> 
      <li>Home</li> 
      <li>Downloads</li> 
      <li>About Us</li> 
     </ul> 
    </div> 
    <div id="breadcrumb"> 
     <div class="breadcrumbs"> 
      <!-- Here i will pull breadcrumbs dynamically--> 
     </div> 
     <!--<h3>Dashboard</h3>--> 
    </div> 
    <br clear = "all"/> 
    <div id="message"> 
    <?php 
    $data['message'] = $message ; 
    $this->load->view('messages/success',$data);?> 
    </div> 
</nav> 

消息庫使用會話可能是flashdata,所以我莫名其妙地認爲它會丟失數據的信息庫。雖然我正在使用正確自動加載它的會話。

+0

你能告訴我更多關於什麼是在局部導航回事?我使用了一些Phil的代碼,所以我可能會提供幫助。 – usumoio

+0

我是約翰高爾特我編輯了我的問題 –

回答

1

我發現了這個問題。這很簡單。我用的是在配置文件中的基本URL爲空

$config['base_url'] = ''; 

我必須要改變它像這樣

$config['base_url'] = 'http://localhost/myproject/'; 
+0

http://www.sadtrombone.com/ 哈,很酷,是啊對不起,我忍不住了,我只是無法找到任何明顯的...做得好的傢伙。 – usumoio