CodeIgniter Forums
Get and Override Default Controller Programmatically - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: Get and Override Default Controller Programmatically (/showthread.php?tid=53619)

Pages: 1 2


Get and Override Default Controller Programmatically - El Forum - 07-31-2012

[eluser]goFrendiAsgard[/eluser]
Dear, everyone.

I know how to change the default controller by editing application/config/routes.php and changing this line
Code:
$route['default_controller'] = "Blog";

Now, I want to get the $route['default_controller'] value as string. Does anyone know how to do that?

Further I also want to change the $route['default_controller'] programmatically. I think I can do that by using file_put_contents in the risk of not permitted operation, so I still hope for better way to do it.

Thanks


Get and Override Default Controller Programmatically - El Forum - 08-01-2012

[eluser]NeoArc[/eluser]
You can access the variable anytime using:

Code:
$route['default_controller'] = "Blog";  
define('DEFAULT_CONTROLLER', $route['default_controller'] );

If you want to edit it before calling the controller you can do it overriding the CI_Router class.


Get and Override Default Controller Programmatically - El Forum - 08-01-2012

[eluser]goFrendiAsgard[/eluser]
[quote author="NeoArc" date="1343832379"]You can access the variable anytime using:

Code:
$route['default_controller'] = "Blog";  
define('DEFAULT_CONTROLLER', $route['default_controller'] );

If you want to edit it before calling the controller you can do it overriding the CI_Router class. [/quote]
Thanks, I'll try it, and post the result as soon as possible


Get and Override Default Controller Programmatically - El Forum - 08-01-2012

[eluser]TWP Marketing[/eluser]
I'm trying to access the default controller but the array var $route doesn't appear to be available in my controller. I've tried $this->$route['default_controller'] without success.
How do I read this var from within my controller?


Get and Override Default Controller Programmatically - El Forum - 08-01-2012

[eluser]NeoArc[/eluser]
Make the necessary changes in the config/routes.php file


Get and Override Default Controller Programmatically - El Forum - 08-01-2012

[eluser]TWP Marketing[/eluser]
[quote author="NeoArc" date="1343851828"]Make the necessary changes in the config/routes.php file[/quote]

This doesn't address the question.

How can we read the $routes array from the controller?

Say I wanted to do a conditional action based on the value of $routes['default_controller'].
How can I read that value?


Get and Override Default Controller Programmatically - El Forum - 08-01-2012

[eluser]NeoArc[/eluser]
[quote author="NeoArc" date="1343832379"]You can access the variable anytime using:

Code:
$route['default_controller'] = "Blog";  
define('DEFAULT_CONTROLLER', $route['default_controller'] );

If you want to edit it before calling the controller you can do it overriding the CI_Router class. [/quote]

I posted this code before, I hope it will be useful if you know how to use constants.


Get and Override Default Controller Programmatically - El Forum - 08-01-2012

[eluser]TWP Marketing[/eluser]
NeoArc.
I do understand your suggestion. It would work for my example usage,

I simply wondered how to access the $routes var via standard php or CI coding.

It ($routes) does not appear to be part of the CI super object, since it isn't accessible using $this->
hence it cannot be overwritten for the same reason (a good thing).

Thanks for the suggestion. I hope the OP can use it also.



Get and Override Default Controller Programmatically - El Forum - 08-01-2012

[eluser]NeoArc[/eluser]
You can create custom configuration values, when overriding the _set_routing() method of the CI_router class:

Code:
<?php
//core/MY_Router.php

class MY_Router extends CI_Router {

//...

function _set_routing()
{

        //...
        $this->routes = ( ! isset($route) OR ! is_array($route)) ? array() : $route;  
        $this->config->set_item('route_config', $this->routes);
        //...
        }
//...
}



Get and Override Default Controller Programmatically - El Forum - 08-01-2012

[eluser]TWP Marketing[/eluser]
[quote author="NeoArc" date="1343855310"]You can try creating custom configuration values, when overriding the _set_routing() method of the CI_router class

$this->config->set_item('route_config', $this->routes);[/quote]

$this->routes fails. I think the CI super object does not give access to $routes in such a direct manner, again, that is a good thing.

We have stolen the OP's thread enough I think. Unless they want to continue the discussion.