Route undefined for my controller CodeIgniter - php

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]

Related

Codeigniter 3 - Make URLs case insensitive

With CI3 released controllers must now be Ucword-style (for whatever reason). No problem changing these, but upgrading any site now leads to 404s wherever it's applicable (which is pretty much everywhere).
Is there a way to make it so the old URLs still work (in addition)? Ie I have a controller 'Admin.php" the index() fn of which used to be called
http://example.com/admin
now it must be called
http://example.com/Admin
Is there a way to have both work (on CentOS). Maybe via Apache rewrite and/or config?
You can set your routes in :
application/config/routes.php
$route['admin'] = 'admin/index';
You can set your Controller as following code in controller.
application/controllers/Admin.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Admin extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model(array('admin_model'));
}
public function index()
{
$this->template->view('admin/index');
}
}
?>
If case insensitive routes are required, do below changes to URI.php
Location of File: system/core/URI.php
Find $this->_parse_request_uri() and replace it with strtolower($this->_parse_request_uri())

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 controller won't load view

I am new to coding and have fallen in love with codeigniter and recently moved from a wamp server to a LAMP server. I've extracted codeigniter into /var/www/ and set the root directory of the host in apache. I can see the codeigniter welcome page, however if I change the default controller in config.php to my controller and load this controller:
class Site extends CI_Controller {
public function index() {
$this->load->view('home_view');
}
}
I get this text in my browser:
$this -> load -> view ('home_view'); }}
The only line of my index function as well as a codeigniter generated 404 error below it.
base_url() and default controller are set, codeigniter welcome page works fine, but my page does not.
Does anyone have an idea as to why? Your help is very much appreciated.
Make sure your controller is inside application/controllers and the name is site.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Site extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('home_view');
}
}

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):
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?

Can't access a Codeigniter controller in sub folder

I have set up a folder with a controller in it: controllers/admin/home.php, but I get a 404 from the browser when I try to access it.
This is my routes file:
$route['employers'] = "employers/home";
//$route['employers/dash'] = "employers/dash";
$route['default_controller'] = "home";
$route['404_override'] = '';
This is the controller file:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class home extends CI_Controller {
function __construct(){
parent::__construct();
/*
enable profiler
*/
//$this->output->enable_profiler(TRUE);
$this->load->helper('url');
$this->load->library('ion_auth');
$this->load->library('session');
$this->load->library('form_validation');
$this->load->helper('layout');
}
}
.htaccess seems fine standard. Any ideas on what i'm doing wrong?
Note some things:
1) Routes are executed in the order they're written, and your custom routes MUST follow the default ones. So, it should be:
$route['default_controller'] = "home";
$route['404_override'] = '';
$route['employers'] = "employers/home";
This if your controller "home" is inside the folder "employers".
2) Controllers don't need all that stuff you wrote, indeed you don't even need to call the parent constructor unless you're planning to load libraries and resource for the whole controller's methods (which can be achieve also by autoloading them in the autoload.php file), so it could simply be:
file: application/controllers/employers/home.php
class Home extends CI_Controller {
function index()
{
// this is the method you're calling with your URL!
}
}
3) As per above, and as already pointed out by #Wesley, with your url you're trying to access the INDEX method of your controller HOME in your subfolder EMPLOYERS. But you didn't defined an index() method (which is the one called by default if no other is supplied).
It seems, instead, that CI is trying to look for an employers controller and a home method; if it doesnt find it, but you have a employers folder, it tries to access the index method in the home controller in the employers folder. And, since it didn't find it either, you're getting the 404 page.
Hope I'm clear, otherwise just ask.
You failed to say how you were trying to access it via url. It should be:
{YOUR_BASE_URL}admin/home
... followed by optional URL segments (/method/param1/param2/etc).
Without the additional segments, this would by default load the index method. However, since you don't have any methods defined, there's nothing to load.
If this still fails after you define a method, move the controller file out of the sub-directory for starters, and make sure it works.

Categories