bigmike7801
bigmike7801

Reputation: 3968

Codeigniter - configure enable_query_strings and form_open

I want to be able to user query strings in this fashion. Domain.com/controller/function?param=5&otherparam=10

In my config file I have

$config['base_url'] = 'http://localhost:8888/test-sites/domain.com/public_html';
$config['index_page'] = '';
$config['uri_protocol'] = 'PATH_INFO';
$config['enable_query_strings'] = TRUE;

The problem that I am getting is that form_open is automatically adding a question mark (?) to my url.

So if I say:

echo form_open('account/login');

it spits out: http://localhost:8888/test-sites/domain.com/public_html/?account/login

Notice the question mark it added right before "account".

How can I fix this?

Any help would be greatly appreciated!

Upvotes: 3

Views: 3158

Answers (3)

wework4web
wework4web

Reputation: 408

If you want to use query string in your url structure, then you should manually type your url structure in the following order:

<domain.com>?c={controller}&m={function}&param1={val}&param2={val}

in the action of the resepective controller you should get the parameter as $_GET['param1']

your code now should look like this

form_open(c=account&m=login&param1=val)

Please let me know if it doesnt work for you.

Upvotes: 1

mariusz
mariusz

Reputation: 31

Simpler might be to just set follwing in your config.php

$config['enable_query_strings'] = FALSE;

Was the solution in my case.

Upvotes: 2

zokibtmkd
zokibtmkd

Reputation: 2183

The source of your problem is in the Core Config.php file where the CI_Config class resides. The method site_url() is used by the form helper when you are trying to use form_open function. Solution would be to override this class with your own. If you are using CI < 2.0 then create your extended class in application/libraries/MY_Config.php, otherwise if CI >= 2.0 then your extended class goes to application/core/MY_Config.php. Then you need to redefine the method site_url().

class MY_Config extends CI_Config
{
   function __construct()
   {
      parent::CI_Config();
   }

   public function site_url($uri='')
   {
      //Copy the method from the parent class here:
      if ($uri == '')
      {
         if ($this->item('base_url') == '')
     {
        return $this->item('index_page');
     }
     else
     {
        return $this->slash_item('base_url').$this->item('index_page');
     }
      }

      if ($this->item('enable_query_strings') == FALSE)
      {
         //This is when query strings are disabled
      }
      else
      {
         if (is_array($uri))
     {
        $i = 0;
        $str = '';
        foreach ($uri as $key => $val)
        {
           $prefix = ($i == 0) ? '' : '&';
           $str .= $prefix.$key.'='.$val;
           $i++;
        }
            $uri = $str;
         }
         if ($this->item('base_url') == '')
     {
            //You need to remove the "?" from here if your $config['base_url']==''
        //return $this->item('index_page').'?'.$uri;
            return $this->item('index_page').$uri;
     }
     else
     {
            //Or remove it here if your $config['base_url'] != ''
        //return $this->slash_item('base_url').$this->item('index_page').'?'.$uri;
            return $this->slash_item('base_url').$this->item('index_page).$uri;
     }
      }
   }
}

I hope this helps and I think you are using CI 2.0 that wasn't officially released, this was removed in the official CI 2.0 version

Upvotes: 2

Related Questions