1

I have sub directories under controllers folder like this:

controllers
--main
----home.php
----login.php
--admin
----home.php
----login.php

So how to make the default controller load main/home.php when entering "example.com" ?

As I read from this doc that I can't add a directory in $route['default_controller'] in CI 3. So I did this:

In config/routes.php

$route['default_controller'] = 'Home';
$route['Home'] = "main/home";

In config/config.php

$config['base_url'] = 'www.example.com';
$config['index_page'] = '';

.htaccess

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

So when I type example.com CI won't redirect me to main/home.php How can I fix this?

  • Can you access the `main/home` statically? Have you tried `$route['default_controller'] = 'main/home';` ? – Roshan Apr 13 '19 at 05:15
  • Yes I can access main/home statically, and yes I've tried '$route['default_controller'] = 'main/home'' and didn't work because directories don't work in CI-3 – Mustafa Alsuhaibi Apr 14 '19 at 08:17
  • Why not create a defined core-controller which redirects you with folders/subfolders rotues and extend it to your Controller? :) Just an idea. – Roshan Apr 15 '19 at 00:39
  • The MY_Router.php in https://stackoverflow.com/questions/6529026/codeigniter-default-controller-in-a-sub-directory-not-working appears to work. I've tried it and it "appears" to work for what you want. – TimBrownlaw Apr 23 '19 at 14:58

0 Answers0