TIP: Use Markdown or, <pre> for multi line code blocks / <code> for inline code.
These forums are read-only and for archival purposes only!
Please join our new forums at discourse.kohanaframework.org
Cannot use Session::instance() in before() method
  • Hello All Is there a reason that prohibits or prevents to use Session::instance() in the before() method in a controller?

    I tried to do it in sevrea ways (directly, by extending the controller_template...) and i can't.

    public function before()
    {
        $this->session= Session::instance();
                ....
    

    }

    And i'm getting SYSPATH/classes/kohana/session.php [ 326 ]

    321 } 322 catch (Exception $e) 323 { 324 // Error reading the session, usually 325 // a corrupt session. 326 throw new Session_Exception('Error reading session data.', NULL, Session_Exception::SESSION_CORRUPT); 327 } 328 329 if (is_array($data)) 330 { 331 // Load the data locally

    SYSPATH/classes/kohana/session.php [ 125 ] » Kohana_Session->read(arguments)
    
    SYSPATH/classes/kohana/session.php [ 54 ] » Kohana_Session->__construct(arguments)
    
    APPPATH/classes/controller/tracking.php [ 25 ] » Kohana_Session::instance()
    
    {PHP internal call} » Controller_Tracking->before()
    
    SYSPATH/classes/kohana/request/client/internal.php [ 103 ] » ReflectionMethod->invoke(arguments)
    
    SYSPATH/classes/kohana/request/client.php [ 64 ] » Kohana_Request_Client_Internal->execute_request(arguments)
    
    SYSPATH/classes/kohana/request.php [ 1138 ] » Kohana_Request_Client->execute(arguments)
    
    DOCROOT/index.php [ 109 ] » Kohana_Request->execute() 
    

    Any clue?

  • I don't know, dude, I just tried it and it works for me, although I wasn't using a Controller_Template...

    Here is my code:

    class Controller_Test extends Controller
    {
         public $session;
         
         public function before()
         {
          $this->session = Session::instance();
         }
         
         
         public function action_index()
         {
          echo $this->session->get('mykey');
          $this->session->set('mykey', 'Hello World!');
         }
    }
    
    

    It first set's the session variable and then upon a page refresh it prints "Hello World!"..

  • :-( not working here, and not a clue why...

  • Is it giving you the same message? Do your sessions work otherwise in other methods? Have you tried going through the error stack trace to see what might have happened?

    I bet it's something with the session/cookies configuration.

    What type of session are you using - native, database,...? What does your session config look like? What about your bootstrap.php session and cookies configuration?

    Also, try cleaning your browser's cookies and cache, or try another browser too.

  • hello. I can get sessions working in other circumstances... but if i add the Session::instance(); to the before method or the constructor of the controller... it doesn't work... no idea... i'm using native sessions. Error... pasted all above.

  • I'm having the same issue...any more info for this one?

  • I am not sure why the Session class would be giving you problems, but if you are using the Controller_Template then you need to get the before() method up to speed, in any case:

    public function before() {
      parent::before();
      ...
    }
    

    Maybe it will fix your problem.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion