将 _remap 与动态 URL 段结合使用

发布于 2024-11-14 15:48:38 字数 1834 浏览 3 评论 0原文

我正在使用 _remap:

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

我想将 url 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?

I'm using _remap:

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

I want to change the url http://localhost/blog to http://localhost/blog/hello

my CI_Controller is:

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

How can I use this for any dynamic id and replace $row->subject with hello?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离不开的别离 2024-11-21 15:48:38

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

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

Instead of routing all passed methods to index, you could route it to another function and pass the method as the parameter to that function:

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;
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文