Reputation: 309
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?
Upvotes: 2
Views: 8625
Reputation: 1310
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]
Upvotes: 6
Reputation: 37701
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).
Upvotes: 0
Reputation: 102735
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.
Upvotes: 4