Tuesday, September 11, 2012

CodeIgniter URL Repeating Issue

CodeIgniter is one of good PHP framework to develop full featured web applications. Also it has very good, clean user guides and resources as well. With using its large number of built-in functions, it's really easy to develop functionalities in your web application rather than developing from the scratch.

Since it follows the MVC architecture, your application is capable of MVC features like,

  • Code Reusing
  • Separating of Concerns
  • Less Coupling between the layers.
At least now, let's move to the Title :D. Most of the CodeIgniter beginners suffering from this URL repeating issue when submitting forms specially.

Ex: Here I'm trying to create a new news item with using the create function which is in the news model class in my application. The link to load the news form is
                    localhost/CodeIgniter_2.1.2/index.php/news/create in my case.

When submit the form after adding the news item, application trying to go to a URL, localhost/CodeIgniter_2.1.2/index.php/news/create/localhost/CodeIgniter_2.1.2/index.php/news/create 
(repeating the URL)

I could able to fix this problem by modifying the config.php file(located at application\config directory) as follows.
$config[‘base_url’] = ‘http://localhost/file_upload/’;
$config[‘index_page’] = ‘index.php’;

Earlier I had those fields like this,

$config[‘base_url’] = ‘localhost/file_upload/’;
$config[‘index_page’] = ‘’;

You may note that in $config[‘base_url’] field, I have added the 'http://'  at the starting. This may happen because, the URL version without "http://" is considered as a relative path (which is added to the actual URL when setting an anchor :)).

Hope this may useful to some others on the planet :D.
Cheers!!!