【问题标题】:Using _remap with dynamic URL segments将 _remap 与动态 URL 段一起使用
【发布时间】:2011-09-12 15:45:22
【问题描述】:

我正在使用 _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?

【问题讨论】:

    标签: codeigniter remap


    【解决方案1】:

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

    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;
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-27
      • 2017-09-16
      • 1970-01-01
      • 2016-05-07
      • 2017-07-18
      • 1970-01-01
      • 1970-01-01
      • 2021-09-19
      相关资源
      最近更新 更多