CodeIgniter does not load the functions from my controller - php

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):
Change the base_url in config.php into http://localhost/application-name/
Empty the index_page (the default value should be index.php)
Go to root folder of your application and check that index.php is there
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?

Related

Why are my MVC controllers showing 404?

I've got an MVC project I'm trying to set up on a new computer. I'm using MAMP and codeigniter. For some reason I get a 404 error when I try to go to any controller.
But the weird thing is that the default controller, defined in the routes file seems to work ok.
Here's an example controller:
class Test_controller extends CI_Controller {
public function index(){
echo 'hello';
}
}
When I go to localhost:8888/test_controller I get 404.
However, adding the following line to my routes.php...
$route['default_controller']="test_controller";
...and then navigating to localhost:8888 seems to load the controller ok.
You are missing the .htaccess, by default CI will need /index.php/controller to work, to remove the index.php you can use the HTaccess provided by Codeigniter or this one:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Place this outside the application folder with the name ".htaccess"
On Codeigniter CI3 which is case sensitive.
Make sure the file name has first letter upper case
Test_controller.php
And then controller class
<?php
class Test_controller extends CI_Controller {
public function index() {
echo 'hello';
}
}
How to use Controllers Link
Codeigniter Doc's Link
Also make sure your base_url is set
$config['base_url'] = 'http://localhost/project/';

CodeIgniter controller model 404 error

I'm getting a 404 error on my CodeIgniter site, I've had a look around at the other answers and playing with the .htaccess but yet to solve. Its located on my domain.co.uk/admin
Controller -
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Projects extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function index() {
$this->load->model('project');
$data['projects']=$this->project->get_projects();
echo"<pre>";print_r($data['projects']);echo"</pre>";
}
}
Model -
<?php
class Project extends CI_Model{
function get_projects($num=10,$start=0){
$this->$db->select()->from('projects')->where('active',1)->order_by('date_added','desc')->limit($num,$start);
$query=$this->db->get();
return $query->result_array();
}
}
I've also added these routes -
$route['default_controller'] = 'projects';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
Currently have this .htaccess in my domain/admin folder
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|images|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
Can anyone see why I might be getting a 404 error
What do you have on your $route['default_controller'] variable? (It shoulds be defined in \application\config).
If you have $route['default_controller'] = "Projects", your index URL should be domain/admin/index.php/projects or domain/admin/projects*
*You can easily remove this file by using a .htaccess file with some simple rules. Here is an example of such a file, using the "negative" method in which everything is redirected except the specified items:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
$config['index_page'] = '';
On codeigniter 3 you should always have your first letter of your class and file name as upper case on models and controllers and libraries.
Also I would rename projects model to Model_project.php because codeigniter may get confused if have same names.
Model Model_project.php
class Model_project extends CI_Model {
}
Controller Project.php
class Project extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('model_project');
}
}
You may need to set up your routes
http://www.codeigniter.com/user_guide/general/routing.html
If you are using a codeigniter 3.0 then update your MODEL and CONTROLLER starting with capital letters and also don't give a same name for both model and controller as you have given in the above code.
For example:
class Projects extends CI_Controller {
//code comes here
}
In this Projects the name is absolutely fine but mark it that you file name should also start with capital letter that is Project.php
Another for model:
class Projects_model extends CI_Model {
//code comes here
}
This is the way to make a proper way to work with codeigniter and also i have already told you that the MODEL and CONTROLLER filename must be started with the capital letter.
Thank You.. hope it will help you

CodeIgniter index function not triggered even though the controller has been called

The short: The code sample below does not echo "Here I am!" It will not echo from the _construct function. It will echo if I move the echo statement above the class so I know CI is reaching this controller.
The backstory:
I've inherited a project done in CodeIgniter 2.1.2. I'm supposed to replicate an application to another directory with a different subdomain and pointing the database config to a different database. I have the database updated in the config. I've updated the base bath. I've set environment to development so I can get error messages. There are no errors.
if (!defined('BASEPATH'))
exit('No direct script access allowed');
if (!ini_get('date.timezone')) {
date_default_timezone_set('my-time-zone');
}
class Login extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('mod_login');
$this->load->helper('date');
}
function index() {
echo "Here I am!"; //Nothing echos
}
function logout{
//logout function here.
}
}
Any CodeIgniter fans here have an idea why?
Your controller should be like this.
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Login extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('mod_login');
$this->load->helper('date');
}
public function index()
{
echo "Here I am!";
}
}
and controller name should be loging.php
and in config/routes.php
$route['default_controller'] = "login";/set default conntoller
Extra Notes: (to remove index.php in URL)
in config/config.php
$config['base_url'] = '';
$config['index_page'] = '';
and .htaccess (place outside application folder)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Class name of controller must be uppercase. From documentation:
Note: Class names must start with an uppercase letter.
In other words, this is valid:
<?php
class Blog extends CI_Controller {
}
?>
This is not valid:
<?php
class blog extends CI_Controller {
}
?>
More: https://ellislab.com/codeigniter/user-guide/general/controllers.html#hello

codeigniter not working on linux

I've made a website using codeigniter framework it's work perfect on my localhost, however didn't work on Linux server ,
I've made changes to baseurl , database , .htaccess
and I've removed index.php perfectly !
but log file show this error when I try to access my url
PHP Parse error: syntax error, unexpected T_FUNCTION in
/home/httpd/vhosts/Domain.com/httpdocs/application/libraries/format.php
on line 231
I tried to make simple controller to echo"Hello" when I call function test ,
<?php
class test extends CI_Controller {
function __construct() {
parent::__construct();
echo "here";
}
function test(){
echo "Hello inside the function";
}
}
and the answer is
> here
404 Page Not Found
The page you requested was not found.
there's no function access :/
but when I make this:
<?php
class test extends CI_Controller {
function test(){
echo "Hello inside the function";
}
}
the answer will be
Hello inside the function
404 Page Not Found
The page you requested was not found.
there's function access :3
anyone can help ??!
btw this is my .htaccess code
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
This is an answer regarding your "TEST" and not your initial question.
The short answer is you cannot have a method that has the same name as the controller. It will be looking for a method called index, which you don't have and hence the error.
I've written some test code based upon what you had...
class Test extends CI_Controller {
public function __construct() {
parent::__construct();
echo 'Hello from the Constructor<br>';
}
public function index() {
echo 'Hello from the Index Function<br>';
}
public function test() {
echo 'Hello from the Test Function<br>';
}
}
/* End of file test.php */
/* Location: ./application/controllers/test.php */
The results. (CI 2.2.0)
If you use localhost/test you will get
Hello from the Constructor
Hello from the Index Function
Note: Without the index method I get the same error you are seeing.
If you use localhost/test/index you will get
Hello from the Constructor
Hello from the Index Function
If you use localhost/test/test you will get
Hello from the Constructor
Hello from the Index Function
Now you do not have an index method. If you see what happens above you will notice that using localhost/test/ Or localhost/test/test, it is looking for the default index method. And the test method isn't reachable as it has the same name as the controller.

Route undefined for my controller CodeIgniter

I'm on CodeIgniter and I have a problem.
I created a Controller called View.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class View extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->model('client');
}
public function index(){
echo "Test";
/*$client = $this->client->getClientByID($idClient);
echo json_encode($client);*/
}
}
But when I call him in the browser, he says : The requested URL /Modules/CommExpert/View/ was not found on this server.
index() in the Controller View is defined and just has an echo in it.
Can someone explains me ? Thanks :D
Part I.
If you get stuck - perform a test by doing something a little different.
For instance - Create a new module called test
Under your new modules/test/controllers/ create a new controller called test.php or for CI 3 Test.php
So now you have /application/modules/test/controllers/Test.php ( for CI 3 )
In your test.php controller put
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Test extends CI_Controller {
public function __construct(){
parent::__construct();
}
public function index(){
echo "This is the Test controller from my test module";
}
}
And see if you can get that to work!
Part II:
I see that you have tried /CommExpert/View in your URL and got a 404.
Be careful of case. Windows and MAC's don't care about case sensitivity. Linux Systems do.
Did you try yourdomain.com/commexpert/view.
After a little experimenting I found that you can have a controller called view (personally, I'd try to go for something different).
Also you can have either view or View in the URL and both work.
What is important is the case of the controller.
In my testing where I have a module called admin, and I have created a controller called view, I get the following... **
NOTE: This is for CI 3.0
URL Result
domain.com/admin/view -> Works ( Lower case v in view )
domain.com/admin/View -> Works ( Upper case V in View )
domain.com/Admin/view -> 404 ( Big Admin, Lower case v in view )
domain.com/Admin/View -> 404 ( Big Admin, Lower case V in view )
See if that helps you any!
Here's my .htaccess :
Options -Indexes
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|uploads|editor|css|js|scripts|images|img|media|xml|user_guide|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

Categories