Welcome Guest, Not a member yet? Register   Sign In
I have a subdirectory in my controllers folder - possibly to set default controller?
#1

I have created a subdirectory called 'admin' in my application/controllers directory. I've got some controllers in there yes sir:
Code:
application/controllers/admin/Dashboard.php
application/controllers/admin/Foo.php
application/controllers/admin/Bar.php

I'm wondering if it's possible to define a default controller::method for that subdirectory in case the url does not specify anything beyond http://example.com/admin

For example, how would I route requests for http://example.com/admin to application/controllers/admin/Dashboard.php?

I really hope I don't have to resort to defining a default controller for every subdirectory in my controllers directory via extensive changes to config/routes.php.

Can anyone tell me if this is possible?
Reply
#2

default_controller applies to all directories. If you set it to 'Dashboard', then application/controllers/Dashboard.php will be the default for example.com/ and application/controllers/admin/Dashboard.php will be the default for example.com/admin/
Reply
#3

(07-23-2015, 05:12 PM)sneakyimp Wrote: I have created a subdirectory called 'admin' in my application/controllers directory. I've got some controllers in there yes sir:

Code:
application/controllers/admin/Dashboard.php
application/controllers/admin/Foo.php
application/controllers/admin/Bar.php

I'm wondering if it's possible to define a default controller::method for that subdirectory in case the url does not specify anything beyond http://example.com/admin

For example, how would I route requests for http://example.com/admin to application/controllers/admin/Dashboard.php?

I really hope I don't have to resort to defining a default controller for every subdirectory in my controllers directory via extensive changes to config/routes.php.

Can anyone tell me if this is possible?

You can do something like this:

PHP Code:
$route['admin'] = 'admin/dashboard'
Romanian CodeIgniter Team :: Translations :: Comunity :: Developers
http://www.codeigniter.com.ro
Reply
#4

(07-24-2015, 06:28 AM)Dracula Wrote: You can do something like this:


PHP Code:
$route['admin'] = 'admin/dashboard'

No, you can't do that with CI3 and it was a bug in versions 2.x that it was allowed.
Reply
#5

(07-24-2015, 06:38 AM)Narf Wrote:
(07-24-2015, 06:28 AM)Dracula Wrote: You can do something like this:



PHP Code:
$route['admin'] = 'admin/dashboard'

No, you can't do that with CI3 and it was a bug in versions 2.x that it was allowed.

Then I apologize. Confused
Romanian CodeIgniter Team :: Translations :: Comunity :: Developers
http://www.codeigniter.com.ro
Reply
#6

Thanks, Narf!
Reply
#7

Hm. It would seem that a default controller in a subdirectory cannot have methods other than index?

For example, I have a subdirectory with a default controller in it:
PHP Code:
// application/controllers/subdir/Welcome.php
defined('BASEPATH') OR exit('No direct script access allowed');

class 
Welcome extends MY_Controller {
     public function 
_remap($method) {
        echo 
"calling " __METHOD__ " with \$method=" $method;
     }
// class subdir/Welcome 
My intuition says that this _remap function should kick in when any method in the subdir folder is called:
http://example.com/subdir
http://example.com/subdir/foo
http://example.com/subdir/bar

However, I get 404/not found for both the foo and bar urls. I am forced to define other controller classes to get those urls served.

Is this supposed to be the case? Is there no way to define a default controller in a subdirectory that handles *all* requests for that subdir?
Reply
#8

(This post was last modified: 10-28-2016, 08:14 PM by wolfgang1983.)

I would create A core/MY_Router.php for CI3

Link MY_Router.php

And use this



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

if( is_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(

=> $class,

=> $method

);

log_message('debug''No URI present. Default controller set.');

}




Then you can use default controller like $route['default_controller'] = 'admin/dashboard';
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB