var $route to Auth controller |
Hello everyone,
sorry for that silly question but i'm new to CodeIgniter. I Use CodeIgniter 3 and Ion Auth. I would like to know the best way to share the variable $route['default_controller'] in /application/config/routes.php to /application/controllers/Auth.php in public function login() for redirect('/','refresh'). I did not find information on how to share this type of data in the documentation. I suppose it is possible to go through the global session variable but it is not very clean I think, and I do not know where to set it (in routes.php)? CodeIgniter being in MVC should I suppose pass the variable routes via the function __construct () of the controllers Auth.php? But I don't know where to initialize the controller when loading ... can you help me? Thanks for your help Ok I find the good way I think. Auth is extends CI_Controller and I can access to $this parameters. with print_r($this), I can find my parameter $this->router->default_controller. And I can directly use it in my auth Controller. Code: redirect('/'.$this->router->default_controller, 'refresh'); I have other question. If I need add new global parameters to $this is it possible ? Where I the best to do that ? And how ?
(02-14-2020, 08:54 AM)nicolas33770 Wrote: Using redirect() without a parameter should send the browser to the $base_url address. Typically the "defalut_controller" is the one assigned to $base_url. So, all you really need is Code: redirect(); As to "global" variables, by simply setting a symbol attached to $this you dynamically create a property on the class. For example: PHP Code: $this->foo_var = "foo"; In CI v3.x.x $this is more often than not the controller class. It would be better to explicitly define a class property named "foo_var" rather than creating it dynamically. PHP Code: Class Foobar extends CI_Controller Defining a class property is the better practice because it makes for easier to read code and avoids a few potential gotchas.
Thanks a lot for your help.
If I need this data on multiple controllers, do I need a custom Libraries ? And load the libraries on each controllers ?
(02-16-2020, 11:23 AM)nicolas33770 Wrote: Thanks a lot for your help. Whether it's adding properties to a controller, or creating custom classes that controllers use, the easiest way to use them in multiple controllers is to use inheritance.
|
Welcome Guest, Not a member yet? Register Sign In |