0

I'm new in MVC and would like to ask about controller and routing in Codeigniter.

I do my project in my localhost.

When we call localhost/projectname, it goes to the controller Welcome which has index function to view main.

this controller, I also have another function to change to another page, like this :

public function index()
    {
        $this->load->view('main');
    }

    public function changeLanguage(){

        $this->load->view('main_in');

    }
}

How can I move to do a href in the view. In my main.php (view). I have :

<a href="changeLanguage">Link</a>

But it's not working. Then I change the link to :

<a href="index.php/Welcome/changeLanguage">Link</a>

and it's working. But on the second view (main_in.php). I want to make a link to go back to the first place. I call it but got error 404 again. Any clues ? thanks in advance.

user3859812
  • 11
  • 1
  • 6

1 Answers1

0

First of all you should change config.php in config folder

$config['base_url'] = '';
$config['index_page'] = '';

And your link should look like this

<a href="<?= base_url() . 'welcome/changelanguage/'">
user1214083
  • 122
  • 6
  • thanks I got the idea by using base url , I use your solution but the link still needs index , such as : = base_url().'index.php/welcome/changeLanguage – user3859812 Dec 08 '16 at 07:09
  • If you need ```index.php``` and you want to remove it, then you have to use .htaccess file. This answer will help you http://stackoverflow.com/questions/19183311/codeigniter-removing-index-php-from-url – kishor10d Dec 08 '16 at 11:35
  • change .htaccess to: Options +FollowSymLinks Options -Indexes DirectoryIndex index.php RewriteEngine on RewriteCond $1 !^(index\.php|img|css|js|images|robots\.txt|public) RewriteCond %{REQUEST_URI} !\.(css³js³jpg³gif³png)$ RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php?/$1 [L,QSA] – user1214083 Dec 08 '16 at 14:40