2

I have a fresh install of codeigniter. I am simply trying to use a function in my default controller like this:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {

    public function index()
    {

        $data = array(
           'title' => 'Welcome',
           'description' => 'Welcome Page'
        );

        $this->load->view('layouts/header',$data);  
        $this->load->view('home/home');
        $this->load->view('layouts/footer',$data);
    }

    public function contact()
    {

        $data = array(
           'title' => 'Contact Us',
           'description' => 'Contact Page'
        );

        $this->load->view('layouts/header',$data);  
        $this->load->view('home/contact');
        $this->load->view('layouts/footer',$data);
    }
}

I have removed index.php successfully using htaccess. Now when I go to example.com/welcome/contact it works, but not example.com/contact/.

Why is this, shouldn't this work by default without using routes?

3 Answers 3

6

use route inside codeigniter, so you can rerwrite new uri for each of them

$route['contact'] = 'welcome/contact';

and don't forget about htaccess file

RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Sign up to request clarification or add additional context in comments.

Comments

4

The "default controller" is only used when there are no URL segments. It only calls one method, and the default method of a controller is index().

Generally, the first part of your URL maps to a controller:

This would invoke the index method of the contact controller:

http://example.com/contact

This would invoke the hello method of the contact controller:

http://example.com/contact/hello

This would invoke the hello method of the contact controller and pass world as the first argument:

http://example.com/contact/hello/world

Read all about it in the user guide: http://codeigniter.com/user_guide/general/urls.html

You need a contact controller for this URL to work, or you can use routing.

1 Comment

This is exactly what I am using, but still I have been redirected to default 404 page of CI
0

example.com/contact/ calls the Contact controller, and what you have is a method in the Welcome controller (which is your default controller, like @Madmartigan explained).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.