Jump to content

How to get sessions on a plugin class


netorica

Recommended Posts

I have a plugin class and I want to check on Appcontroller.structure event if a client is currently logged in. But

 $this->Session->read("blesta_client_id");

returns null. This is my current code right now

<?php
class OverridesPlugin extends Plugin {
    
    private $Record;

    public function __construct() {
        $this->Record = new Record();
        $this->loadConfig(dirname(__FILE__) . DS . "config.json");
    }

    public function install($plugin_id) {
    
    }

    public function getEvents() {
        return array(
            array(
                'event' => "Appcontroller.preAction",
                'callback' => ["this", "preAction"]
            ),
            array(
                'event' => "Appcontroller.structure",
                'callback' => ["this", "structure"]
            ),
        );
    }

    public function preAction($event){
        Loader::loadModels($this, ['Overrides.OverridesSettings']);
    }

    public function structure($event){

        
       $logged_in_client = $this->Session->read("blesta_client_id");
        $url_details = $event->getParams();
        echo '<pre>'.var_export($logged_in_client,true).'</pre>';
        exit(1);

    }

}

Can you tell me what is the right way to check the client sessions in a plugin class?

 

Thank you

Link to comment
Share on other sites

Are you saying that calling the read() method returns null, or $this->Session is null? Because it looks like $this->Session is null because it's not loaded anywhere. Your Plugin class is not a controller, so it does not inherit the Session object from AppController.

Loader::loadComponents($this, ['Session']);
$this->Session->read('blesta_client_id');

Depending on what you're trying to do, you may want to also consider the fact that staff can log in as a client, so it may not really be the client that's logged in.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...