I have been for hours to make a simple anchor link working without success.
My controller is
class Welcome extends CI_Controller {
public function index()
{
$this->load->view('template');
}
public function contact()
{
$this->load->view('contact');
}
}
My template.php
view is basically a file with
<a class="menuhref"> <?php echo anchor('welcome/contact/','Contato')?> </a>
I also have a contact.php in the views directory.
My config.php
is
$config['base_url'] = 'localhost';
$config['index_page'] = 'index.php';
When the template.php
loads and I click in the link "contato" I would like to drive the system to the function contact()
that would be responsible to open the page contact.php
. However, I have got the following error:
"/localhost/index.php/welcome/contact" was not found on this server
What is missing?
The
base_url
config MUST contains the protocol and a trailing slash as well.From the
config.php
file:Hence, you could set the
base_url
as follows, or in this case simply leave it blank:Side-note: in order to use URL helper functions such as
anchor()
, load the helper file at first:$this->load->helper('url');
(or load the helper automatically viaautoload.php
config file).Why does
base_url
config affectanchor()
function?anchor()
function, usessite_url()
helper function to determine the URL address of the hyperlink.And the
site_url()
itself, uses twobase_url
andindex_page
configs to create the URL address.Hence if you assign a wrong value to
base_url
and/orindex_page
configs, theanchor()
function won't work properly.