将_remap与动态网址段一起使用

时间:2011-06-10 16:32:35

标签: codeigniter remap

我正在使用_remap:

function _remap( $method )
    {
        // $method contains the second segment of your URI
        switch( $method )
        {
            case 'hello':
                $this->index();
                break;
        }
    }

我想将网址http://localhost/blog更改为http://localhost/blog/hello

我的CI_Controller是:

  class Blog extends CI_Controller {
    function __construct()
    {
        parent::__construct();                
    }

    function _remap( $method )
        {
            // $method contains the second segment of your URI
            switch( $method )
            {
                case 'hello':
                    $this->index();
                    break;
            }
        }
    /**/
    function index()
    {

                $g_subject = $this->input->get('id', TRUE);          
                $query = $this->db->get_where('miniblog', array('id' => $g_subject));
                foreach ($query->result() as $row)
                {
                $data = array(
                    'subject' => $row->subject,
                    'title' => $row->title,                
                    'image_path' => $row->image_path,
                    'alt' => $row->alt,
                    'text' => $row->text,
                    'date' => $row->date,
                );
                }

                 $this->load->view('miniblog/blog', $data);
                 //add customer size to databe on customer

                 //$this->customer_size_model->show();

    }
    function ipv6()
    {
        $this->load->view('miniblog/ipv6');
    }
}

如何将此用于任何动态ID并将$row->subject替换为hello?

1 个答案:

答案 0 :(得分:0)

不是将所有传递的方法路由到索引,而是将其路由到另一个函数,并将该方法作为参数传递给该函数:

function _remap( $method ){
      // $method contains the second segment of your URI
      switch( $method ){
           case 'index':
              $this->index();
              break;
           default:
              $this->all_encompasing_method($method);
      }
 }

 function all_encompasing_method($url_param){
      // here's my param 
      echo $url_param;
 }