How to pass parameters in index function in codeigniter

50,045

Solution 1

  • You need the index segment http://www.example.com/search/index/search-keyword.
  • Or you need to use a route $route['search/(:any)'] = 'search/index/$1';
  • Or you can look at remap

Remember not to trust user input, especially when you are throwing it into your url. The latest version of CI supports $_GET variables now, so you may want to look into using that or flashdata. A searh term as simple as O' Brien will give you a fatal error ("The URI you submitted has disallowed characters.").

Solution 2

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.

Solution 3

function _remap($parameter){

        $this->_index($parameter);

}

Give it a try and tell us how it went.

Solution 4

Most of these solutions will only work if you only have a single function called index() in your controller. If you have this in your routes:

$route['search/(:any)'] = 'search/index/$1';

With the above in your routes, what will happen is that your search function will work but then if you add any other functions to that same controller they'll all be redirected to /search/index/$1.

The only solution I can think of that allows you to use the URL you want while still being able to add any other functions to your search controller is to add a sort of messy conditional to your routes file. Here's what it will look like and what it does:

$request = $_SERVER['REQUEST_URI']; // Only add this for readability if(!strpos($request, 'search/another_function') || !strpos($request, 'search/more_functions')) { $route['search/(:any)'] = 'search/index/$1'; }

What this is doing is basically saying "if the requested URL doesn't contain the name of any of my search controller functions then it must be meant for the index so we'll activate the route rule for index".

This will allow you to take requests for search/any_other_functions_you_have without issue and only activate the rule for hiding the index function when a request URI doesn't match any of them.

One side effect of this is that you'll never get a 404. For example, if someone enters a URL like "yourdomain.com/search/something" and they expect it to show a non-search result page they won't get a 404 alerting them to the fact that there is no page like that and instead the app will assume what they typed is a search term. However, it sounds like this isn't much of an issue for you and I don't see it being such a terrible thing for one controller to be unabe to return 404s.

Solution 5

You need to understand the way code igniter urls work, its basically like this:

http://www.mysite.com/{controller}/{function}

So what your url is actually looking a function called "keyword" in your search controller.

You have multiple options. The easiest will be to simply change the url to:

http://www.mysite.com/search/result/keyword

Then this should work perfectly:

class Search extends CI_Controller{

function result($param){
 echo $param;
}

}

If you really want to use the url as you had it, you can use the same snippet of code as above but also open up "application/config/routes.php" and add this to the bottom.

$route['search/(:any)'] = "search/result";

Alternatively if you want to continue using the index function you can change it to this

    $route['search/(:any)'] = "search/index";

And change your class to something like this:

class Search extends CI_Controller{

  function index($param=FALSE){
    if($param == FALSE) {
      //Show search box
    } else {
      //Show search results
    }
  }

}

Don't forget to update your answer if you figure it out yourself or accept somebodies answer if it answers it :)

Share:
50,045

Related videos on Youtube

user691983
Author by

user691983

Updated on November 22, 2021

Comments

  • user691983
    user691983 over 2 years

    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 controller and I want to pass a parameter in the index method.

    this is currently what it looks like:

    class Search extends CI_Controller{
    
        function index($param){
            echo $param;
        }
    
    }
    

    any suggestions? I can't seem to make it work

  • Wesley Murch
    Wesley Murch about 13 years
    This won't work because the index function is not invoked, its looking for the non-existent function 'search-keyword'
  • Wesley Murch
    Wesley Murch about 13 years
    This won't work because the index function is not invoked, its looking for the non-existent function 'search-keyword'
  • Peter
    Peter about 13 years
    How about declaring the uri segment 2 as your search keyword in a construct?
  • jfoucher
    jfoucher about 13 years
    Very true, you need to add a route anyway, so I guess it's best just to use the route method
  • Wesley Murch
    Wesley Murch about 13 years
    You can use php5 __call as well, similar to how you'd use CI's _remap. I personally like to keep my routes clean and lean, so I am fond of those methods for simple controllers that only do one or two things.
  • Wesley Murch
    Wesley Murch almost 13 years
    This alters the search term, which is not desirable. If I searched for Hello World! you would return results for hello-world
  • M B Parvez
    M B Parvez about 10 years
    That's really cool, it works. But how to access the parameter that is passing to index function?
  • Ugnius Malūkas
    Ugnius Malūkas over 5 years
    How is different from @Bill answer?
  • Barbz_YHOOL
    Barbz_YHOOL over 3 years
    that works fine, but I wonder if it's the right way to do

Related