开发者

Creating a dynamic controller in codeigniter?

Consider this scenario::- When i upload a view file in admin,A new menu is created in user side i.e dynamic creation of menus..

Ex: when admin upload a help view file, a help menu should appear in user side开发者_运维技巧..is that possible,the controller for help should be created dynamically.


Sure, PyroCMS does this. Take a look at the module management code, specifically details.php. You'll need to do some of the development yourself for this but my code will demonstrate how I've done it.

http://github.com/pyrocms/pyrocms


I got something like this working a while back pretty easily..

Take a look at the simple example of how I did this... It was for a Help Site.

The Model


<?php

class Template_model extends Model {

    function Template_model()
    {
        parent::Model();
    }

    function load_pages()
    {
        $data = array();
        $this->db->where('status',0);
        $this->db->order_by('sort', 'ASC');
        //$this->db->get('pages');
        $query = $this->db->get('pages');
        if ($query->num_rows() > 0){
            foreach ($query->result_array() as $row){
                $data[] = array(
                    "name" => $row['name'],
                    "url" => $row['url']
                );
            }
         }

        $query->free_result();
        return $data;
    }

}

The controller


<?php

class View extends Controller {

    function View()
    {
        parent::Controller();   
    }

    function index()
    {
        redirect('view/topic/orders');
        //echo "oops";
    }

    function topic()
    {
        $page = $this->uri->segment(3);

        //get the page id
        $this->load->model('view_model');
        $id = $this->view_model->get_page_id_by_url($page);
        $id = $id['id'];

        //get the page title
        $data['title'] = $this->view_model->get_page_title($id);

        //load the post by page id
        $data['posts'] = $this->view_model->get_page_posts($id);

        //load the header 
        $this->load->model('template_model');
        $data['pages'] = $this->template_model->load_pages();

        //load the view
        $data['main_content'] = 'view/page';
        $this->load->view('includes/template', $data);

    }

    function article()
    {
        $url = $this->uri->segment(3);

        //get the post by url
        $this->load->model('view_model');
        $data['post'] = $this->view_model->get_post_by_url($url);
        $id = $data['post']['page_id'];
        //print_r($data['post']);

        //get the page title
        $data['title'] = $this->view_model->get_page_title($id);

        //load the post by page id
        $data['posts'] = $this->view_model->get_page_posts($id);

        //load the header 
        $this->load->model('template_model');
        $data['pages'] = $this->template_model->load_pages();     

        //load the view
        $data['main_content'] = 'view/article';
        $this->load->view('includes/template', $data);
    }

}

/* End of file view.php */
/* Location: ./system/application/controllers/view.php */

and the view..


    <div id="navigation">
        <ul>

                <?php 
                if(!empty($pages))
                {   
                    foreach($pages as $page)
                    {
                        ?>

                            <li <?php if($on_page == $page['url']) { echo 'class="selected"'; }?>><a href="<?php echo base_url().'view/topic/'.$page['url'];?>"><?php echo $page['name'];?></a></li>

                        <?   

                    }
                }?>

                <!-- top navigation use selected class for selected item -->

                <div id="rNav" ><ul><li <?php if($on_page == 'admin') { echo 'class="selected"'; }?>><a href="<?php echo base_url();?>admin">Admin</a></li></ul></div>
                <div id="rNav" ><ul><li <?php if($on_page == 'logout') { echo 'class="selected"'; }?>><a href="<?php echo base_url();?>logout">Logout</a></li></ul></div>

        </ul>
    </div>


You should just route the links to a specific controller that handles menus

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜