-3

I am trying to use:

href=<?php echo base_url('contactus.php');?>

where contactus.php is a file under views and I have loaded helper url under autoload. Please help me with correct syntax.

Also, I have loaded the file in controller but it is still not working. My controller looks like this:

<?php

class Home extends CI_Controller
{
  function index()
  {
    $this->load->view('homeview');
  }

  function contact()
  {
    $this->load->view('contactus');
  }
}

?>
Mischa
  • 42,876
  • 8
  • 99
  • 111
score
  • 521
  • 1
  • 8
  • 22

3 Answers3

5

base_url is a function without arguments and you are passing the parameter that's why it is not echoing.

Use this code:

href="<?php echo base_url().'index.php/home/contact'; ?>"

Or better yet:

href="<?php echo site_url('home/contact'); ?>"

Or even better:

<?php echo anchor('home/contact', 'Contact Us'); ?>

References:

Community
  • 1
  • 1
Code Lღver
  • 15,573
  • 16
  • 56
  • 75
  • It says the page requested not found when i did: base_url().'index.php/home/contactus.php' "home is my controller page" – score Jul 19 '13 at 06:40
  • 404 page not found! The request page not found!! still the same problem!! sorry to bug you all for sth that might be silly questions... :( – score Jul 19 '13 at 06:42
  • Contact US this is my actual line of code following is the code in controller page home.php load->view('homeview'); } function contact() { $this->load->view('contactus'); } } ?> – score Jul 19 '13 at 06:45
  • 1
    Do you have a controller name with home and a function in it with name contact? – Sona Rijesh Jul 19 '13 at 06:46
  • @SHANKAR your controller method is called `contact` not `contactus`, so it should be `base_url().'index.php/home/contact'`, although I'd advice you to use `site_url` like this: `site_url('home/contact')` – Mischa Jul 19 '13 at 06:49
  • @SHANKAR as the Sona say that check that do you have controller with name home and action contactus in home controller. – Code Lღver Jul 19 '13 at 06:50
  • 1
    And please check you have a php page in VIEWS folder which you have mentioned in method – Sona Rijesh Jul 19 '13 at 06:52
  • @Mischa: you way got some better: now its saying unable to load the file but I have loaded like: function contact() { $this->load->view('contactus'); } – score Jul 19 '13 at 06:54
  • Are you sure that contactus.php in VIEWS folder? – Sona Rijesh Jul 19 '13 at 06:56
  • 2
    Hey guys thankx!! Its working now... thank you all for your prompt help!! I can move furthe... :) – score Jul 19 '13 at 06:58
  • @Mischa: i m quite new to stackoverflow: It had to be donw within 15 min.. but its already quite late... can't do it... :( – score Jul 19 '13 at 07:41
4
//use following to redirect page using base_url()
<a href="<?php echo base_url().'index.php/controller_class/functionName';?>">contact us</a>
1

why are you linking view file? View files are supposed to be rendered not linked directly. You should write controller and load view inside that controller. Codeigniter is a MVC framework. Research more on MVC pattern. And you can use site_url($path) function which accepts arguments instead of base_url() if you want to pass paramater to generate urls.

Yalamber
  • 7,360
  • 15
  • 63
  • 89