Welcome Guest, Not a member yet? Register   Sign In
Get and Override Default Controller Programmatically
#1

[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
#2

[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.
#3

[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
#4

[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?
#5

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

[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?
#7

[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.
#8

[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.
#9

[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);
        //...
        }
//...
}
#10

[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.




Theme © iAndrew 2016 - Forum software by © MyBB