0

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'] = '';
8
  • 1
    Have you set the base_url in your config.php ? Does both localhost:8899/index.php and localhost:8899/index.php/site/index work for loading your index method? It may also be worthwhile posting the contents of your routes.php file. Commented Oct 31, 2013 at 14:51
  • Yes. localhost:8899/index.php/site/index also loads my index method just fine Commented Oct 31, 2013 at 14:56
  • 1
    Try setting it to http://localhost:8899/index.php/ and see if it makes a difference. If not, add the contents of your routes.php to your question, as it most likely needs changing. Commented Oct 31, 2013 at 15:15
  • 1
    Well, your routes.php looks fine. What exactly is happening when you go to localhost:8899/index.php/site/about ? Do you get a white screen? If you add an echo 'test'; to your about method, does it not get displayed? Commented Oct 31, 2013 at 15:23
  • 1
    @Jeemusu Ok i fixed the issue. For some reason In my config.php i had $config['uri_protocol']= 'QUERY_STRING'. That was the problem all along. I'm not quite sure why it was not on AUTO as it should Thanks for your time and help anyway! Commented Oct 31, 2013 at 15:28

2 Answers 2

1

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]
    
Sign up to request clarification or add additional context in comments.

Comments

0

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?

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.