How to pass parameters in index function in codeigniter

后端 未结 12 1870
傲寒
傲寒 2020-12-14 03:02

here\'s an example of url: http://www.example.com/search/search-keyword

I am trying to make this work, I removed the index.php using .htaccess, search is the contro

相关标签:
12条回答
  • 2020-12-14 03:39
    class Search extends CI_Controller{
    
        function _remap($param) {
            $this->index($param);
        }
    
        function index($param){
            echo $param;
        }
    
    }
    

    You should then be able to access that as: /search/123123 :and the page would echo out "123123" or whatever you put in place of that.

    0 讨论(0)
  • 2020-12-14 03:39

    This is my solution:

    From CI userGuide

    public function _remap($method)
    {
        if ($method == 'some_method')
        {
            $this->$method();
        }
        else
        {
           $this->default_method();
        }
    }
    

    So I've created my controller in this way:

    class Shortener extends CI_Controller {
    function shortener() {
        parent::__construct();
    }
    
    function index($getVar) {
        echo "Get var: " . $getVar;
    }
    
    function config() {
        echo "another function";
    }
    
    function _remap($method) {
        if($method == "config") {
            $this->$method();
        }
        else {
    
            $this->index($method);
        }
    }
    }
    

    Hope this can be helpful to someone...

    BUG: if you call /shortener _remap function pass "index" to index() function...it can be solved adding an if condition or something else

    0 讨论(0)
  • 2020-12-14 03:40
    function _remap($parameter){
    
            $this->_index($parameter);
    
    }
    

    Give it a try and tell us how it went.

    0 讨论(0)
  • 2020-12-14 03:42

    I had to also check if some variable was passed or not - so here is the solution that I took.

    <?php
    
    defined('BASEPATH') OR exit('No direct script access allowed');
    
    class Story extends CI_Controller{
    
        function story() {
            parent::__construct();
        }
    
        function _remap($parameter){
            $this->_index($parameter);
        }  
    
        public function _index($story_id) {        
            if($story_id == 'index'){
                //No variable passed
                redirect('auth', 'refresh');
            }else{
                $data['title'] = "Story";
                $this->load->view('story', $data);                
            }
        }
    
    }
    

    Hope this helps someone!

    0 讨论(0)
  • 2020-12-14 03:43

    I finally found a workaround since I cant simply put post variables into the URL.

    What I did was create another function, then redirect it to that function.

     class Search extends CI_Controller{
       function index(){
            $search_item = $this->input-post('search_item');
            redirect("search/q/".url_title($search_item));
      }
    
      function q($key){
        // process search
      }
     }
    
    0 讨论(0)
  • 2020-12-14 03:49

    This easiest way is just to use uri segments:

    class Search extends CI_Controller{
      function index(){
        $param=$this->uri->segment(2);
        echo $param;
      }
    }
    

    Or like Madmartigan said, use routes, which is probably the better way of doing this.

    0 讨论(0)
提交回复
热议问题