Krishna Sarma
Krishna Sarma

Reputation: 1860

accessing controller in subfolder codeigniter

Just downloaded CI 2.2, renamed the folder to Test1, copied into htdocs folder of xampp.

Tested the site by accessing http://localhost/Test1, it launched the Welcome message. Now added a folder to controllers ("admin") and a controller in it (home.php). so the structure is:

controllers
    --> admin
          --> home.php
    --> index.php
    --> welcome.php

and the code in home.php is

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends CI_Controller {
    public function index()
    {
        echo 'Hello';
    }
}

When I try accessing http://localhost/Test1/admin/home or http://localhost/Test1/admin/home/index it says object not found (404 error).

I also tried adding $route['admin'] = "admin/home"; to application\config\routes.php so that I can access as http://localhost/Test1/admin but of no use. Am I missing a setting?

Upvotes: 0

Views: 7456

Answers (3)

Robin Rumeau
Robin Rumeau

Reputation: 61

Did you configure CodeIgniter so it can run without the index.php ? Otherwise the correct url should be something like :

http://localhost/index.php/admin/home

or

   http://localhost/index.php/admin/home/index 

...witch are pretty much the same for CI.

If you try one of the above url and you have the Welcome message, it's a good news ! And here's the solution : you have to tell CI not to use index.php in your URL.

  1. Open your .htaccess or create one in your website root,

  2. Put the next lines in it :

    RewriteEngine on
    RewriteCond $1 !^(index\.php|images|robots\.txt)
    RewriteRule ^(.*)$ /index.php/$1 [L]

  3. Check you've removed the lines you added in the config/routes.php for your tests.

  4. Try to access your admin folder with the same URL but without the index.php.

    Tell us what's gong on.

Upvotes: 3

Tariq
Tariq

Reputation: 402

create a file htaccess.htaccess and past the following code in it. save it in your Test1 folder

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /Test1/index.php/$0 [PT,L] 

Upvotes: 2

Vivek Aasaithambi
Vivek Aasaithambi

Reputation: 929

RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

you should create .htaccess file and paste above mentioned lines. and put that file into you project folder(Test1).

Upvotes: 1

Related Questions