首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >消息库、My_Controller和__remap问题

消息库、My_Controller和__remap问题
EN

Stack Overflow用户
提问于 2012-12-13 14:46:57
回答 1查看 150关注 0票数 3

我正在创建一个应用程序,并用MY_Controller处理常见的事情。我正在使用消息库来显示常见消息。

下面是MY_Controller.php:

代码语言:javascript
复制
<?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);
        }
    }   
}   

/**/

正如你所看到的,我正在使用Phil Sturgeon的模板库,并且我正在使用Jamierumbelow的技术来处理布局。当我在表单插入失败时设置了一条消息,它是正常的。我可以将它显示在_remap中,如下所示

代码语言:javascript
复制
echo $this->message->display();

在控制器中,它工作得很好,但当我在部分导航中调用它时,它不会显示消息。可能的问题是什么。我已经在My_Controller的不同地方试过了。它工作得很好,但不是在部分,甚至我已经尝试了失败的形式,我再次加载。这是我正在使用的消息库

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

这是我的导航部分。

代码语言:javascript
复制
<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,所以我认为它以某种方式丢失了会话数据。尽管我正确地使用了会话自动加载它。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-14 14:27:35

我找到了问题所在。这很简单。我在配置文件中使用的基本url为空。

代码语言:javascript
复制
$config['base_url'] = '';

我必须像这样改变它

代码语言:javascript
复制
$config['base_url'] = 'http://localhost/myproject/';
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13854488

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档