Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 707
bigmike7801 Avatar asked Dec 07 '25 04:12

bigmike7801


2 Answers

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

like image 200
zokibtmkd Avatar answered Dec 08 '25 19:12

zokibtmkd


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

$config['enable_query_strings'] = FALSE;

Was the solution in my case.

like image 36
mariusz Avatar answered Dec 08 '25 20:12

mariusz