简体   繁体   中英

CodeIgniter does not load the functions from my controller

I started with CodeIgniter today, and I'm following the beginner tutorial from phpacademy. Right now I got an odd problem, I got the following very simple controller:

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

 class Site extends CI_Controller {
 public function index()
 {
 }

 public function home()
 {
     $this->load->view("view_home", $data);
 }

 function about()
 {
     $data['title'] = "About!";
     $this->load->view("view_about", $data);
 }

 function test()
 {
     echo "test";
 }
} 

It always loads the index function fine. When I test it, it echoes whatever I want it to echo. But when I call the other functions nothing happen.

I didn't set up my .htacces yet, but when I visit the following address: localhost:8899/index.php/site/home

it doesn't load the home function. same goes for the other function (“about” and “test”);

When I call one of the functions (“home”, “about” and “test”) from within the index() function it does call it how it should:

class Site extends CI_Controller {
public function index()
{
    $this->home();
}

public function home()
{
    $this->load->view("view_home", $data);
} 

I'm not sure what is going wrong here. Hopefully, someone can guide me in the right direction!

My Routes:

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

$route['default_controller'] = "site";
$route['404_override'] = '';

If somebody came across this since no solutions above worked, ok you can try these steps where i got the thing worked (local with xampp):

  1. Change the base_url in config.php into http://localhost/application-name/
  2. Empty the index_page (the default value should be index.php )
  3. Go to root folder of your application and check that index.php is there
  4. Make .htaccess file in there and write into that these:

     RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L]

i would make sure the controller class has a constructor to load the base codeigniter base classes.

function __construct()
{
    parent::__construct();
}

in each controller class tells your class to use the code igniter system. if this is not present it won't know to how to load these functions.

Just to be clear, make sure it calls the other methods by url/site/about etc... Also, have you got php errors displaying on you development server?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM