00cong.com      


  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
var $route to Auth controller

#1
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 ?
Reply

#2
(02-14-2020, 08:54 AM)nicolas33770 Wrote:
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 ?

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
{
    protected $foo_var;

    function __construct()
    {
        parent::__construct();
        $this->foo_var "foo";
    }

    function index()
  {
        echo "This is a bunch of "$this->foo_var;


Defining a class property is the better practice because it makes for easier to read code and avoids a few potential gotchas.
Reply

#3
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 ?
Reply

#4
(Yesterday, 11:23 AM)nicolas33770 Wrote: 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 ?

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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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