• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] How to select default controller in subfolder?

#1
Thumbs Up 
Hi community,

I'm trying to do a project in Codeigniter 3.x, but I have a question concerning the routes.

If I have the following structure

/application/controllers/Welcome.php

I can put the following in routes.php and have no problem

PHP Code:
route ['default_controller'] = 'welcome'

The problem is that if I have the following structure

/application/controllers/admin/Welcome.php

I am not allowed to configure the routes so

PHP Code:
route ['default_controller'] = 'admin/welcome'

There is the ability to set default controller in a subdirectory of controllers?


Thank you!
Reply

#2
No, you can't do that.
Reply

#3
(01-06-2016, 05:08 PM)Narf Wrote: No, you can't do that.

You can like
/application/admin/[i]controllers/Welcome.php[/i]

and than set default

Am I right Narf?
Reply

#4
(01-06-2016, 03:12 PM)normeno Wrote: Hi community,

I'm trying to do a project in Codeigniter 3.x, but I have a question concerning the routes.

If I have the following structure

/application/controllers/Welcome.php

I can put the following in routes.php and have no problem

PHP Code:
route ['default_controller'] = 'welcome'

The problem is that if I have the following structure

/application/controllers/admin/Welcome.php

I am not allowed to configure the routes so

PHP Code:
route ['default_controller'] = 'admin/welcome'

There is the ability to set default controller in a subdirectory of controllers?


Thank you!

I have asked this question my self before you need to create a MY_Router.php file in application / core

Default Controller In Sub Folder

PHP Code:
<?php
 
class MY_Router extends CI_Router {
 
   protected function _set_default_controller() {
 
        
        if 
(empty($this->default_controller)) {
 
           
            show_error
('Unable to determine what should be displayed. A default route has not been specified in the routing file.');
 
       }
 
       // Is the method being specified?
 
       if (sscanf($this->default_controller'%[^/]/%s'$class$method) !== 2) {
 
           $method 'index';
 
       }
 
        
        
// This is what I added, checks if the class is a directory
 
       ifis_dir(APPPATH.'controllers/'.$class) ) {
 
            
            
// Set the class as the directory
 
           
            $this
->set_directory($class);
 
           
            
// $method is the class
 
           
            $class 
$method;
 
           
            
// Re check for slash if method has been set
 
           
            if 
(sscanf($method'%[^/]/%s'$class$method) !== 2) {
 
               $method 'index';
 
           }
 
       }
 
        
        if 
( ! file_exists(APPPATH.'controllers/'.$this->directory.ucfirst($class).'.php')) {
 
           
            
// This will trigger 404 later
 
           
            return
;
 
       }
 
       $this->set_class($class);
 
       $this->set_method($method);
 
       // Assign routed segments, index starting from 1
 
       $this->uri->rsegments = array(
 
           1 => $class,
 
           2 => $method
        
);
 
       log_message('debug''No URI present. Default controller set.');
 
   }

There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply

#5
(01-06-2016, 10:10 PM)Bhavesh Wrote:
(01-06-2016, 05:08 PM)Narf Wrote: No, you can't do that.

You can like
/application/admin/[i]controllers/Welcome.php[/i]

and than set default

Am I right Narf?

No.
Reply

#6
wolfgang1983 thanks for your help, I will review the class you mention.


If this is not enabled by default for Codeigniter, what is the best way to separate environments? I was doing the following

/application/controllers/admin/
/application/controllers/frontend/
/application/controllers/webservice/
Reply

#7
I have a similar setup.

I have an "admin" sub-folder, and inside that, an index.php controller file that has an index method.

Then you just add additional routing

PHP Code:
$route['admin'] = 'admin/index'
Reply

#8
this has worked for me!


(01-06-2016, 10:42 PM)wolfgang1983 Wrote:
(01-06-2016, 03:12 PM)normeno Wrote: Hi community,

I'm trying to do a project in Codeigniter 3.x, but I have a question concerning the routes.

If I have the following structure

/application/controllers/Welcome.php

I can put the following in routes.php and have no problem

PHP Code:
route ['default_controller'] = 'welcome'

The problem is that if I have the following structure

/application/controllers/admin/Welcome.php

I am not allowed to configure the routes so

PHP Code:
route ['default_controller'] = 'admin/welcome'

There is the ability to set default controller in a subdirectory of controllers?


Thank you!

I have asked this question my self before you need to create a MY_Router.php file in application / core

Default Controller In Sub Folder

PHP Code:
<?php
 
class MY_Router extends CI_Router {
 
   protected function _set_default_controller() {
 
        
        if 
(empty($this->default_controller)) {
 
           
            show_error
('Unable to determine what should be displayed. A default route has not been specified in the routing file.');
 
       }
 
       // Is the method being specified?
 
       if (sscanf($this->default_controller'%[^/]/%s'$class$method) !== 2) {
 
           $method 'index';
 
       }
 
        
        
// This is what I added, checks if the class is a directory
 
       ifis_dir(APPPATH.'controllers/'.$class) ) {
 
            
            
// Set the class as the directory
 
           
            $this
->set_directory($class);
 
           
            
// $method is the class
 
           
            $class 
$method;
 
           
            
// Re check for slash if method has been set
 
           
            if 
(sscanf($method'%[^/]/%s'$class$method) !== 2) {
 
               $method 'index';
 
           }
 
       }
 
        
        if 
( ! file_exists(APPPATH.'controllers/'.$this->directory.ucfirst($class).'.php')) {
 
           
            
// This will trigger 404 later
 
           
            return
;
 
       }
 
       $this->set_class($class);
 
       $this->set_method($method);
 
       // Assign routed segments, index starting from 1
 
       $this->uri->rsegments = array(
 
           1 => $class,
 
           2 => $method
        
);
 
       log_message('debug''No URI present. Default controller set.');
 
   }

Reply

#9
(01-08-2016, 07:47 AM)normeno Wrote: this has worked for me!

There was to many quotes in you previous reply was hard to understand
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply

#10
It only pertains to the default controller, all other controllers in sub-folders will work with the route ( sub-folder/controller).
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.