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
Sessions with paths
  • Hi! is there a reason why Session keys can't be accessed with paths (using Arr::path) like many other classes in Kohana?

  • This work for me:

    ...
    $data = array('somedata' => 'somevalue');
    $session->set('sess_key',$data);
    ...
    
    ...
    echo Arr::path($session->as_array(), 'sess_key.somedata');
    ...
    
  • Hi @devi, I'm using something similar for getting, but what about Session::delete?

  • @endrip I'm sorry, I don't get the point. Do you mean you want to delete Session by using Arr::path ? As far as I know, Arr::path only Gets a value

  • Which other class uses Arr::path? I only found it in the Kohana::config method.

    You could extend the session class with a set_array method and then you can do

    $session_arr = $session->as_array();
    
    Arr::set_path($session_arr, 'sess_key.somedata', null); // or  unset($session_arr['sess_key']['somedata']);
    
    $session->set_array($session_arr);
    
  • Yes, I can extend the class (also, in your example I think is better to return as_array as a reference).
    But that is why I'm asking this in Feature Requests.
    Kohana::message is the other class using Arr::path (I think there are only two then, not "many" :) )
    I think that Session (and maybe Cookie) could be a good place to use Arr::path too, but maybe there's a reason for not doing that (I was asking that at really).

  • What good would it do to return the array as reference? There has to be a a setter method to replace the current array with the changed array as the variable that holds the data is protected.

    This is how it is

    $data = array('somedata' => 'somevalue');
    $session->set('sess_key',$data);
    
    $sess_key = $session->get('sess_key');
    
    if($sess_key['somedata'] == 'somevalue'){}
    
    unset($sess_key['somedata']);
    
    $session->set('sess_key',$sess_key);
    

    And you want

    $data = array('somedata' => 'somevalue');
    $session->set('sess_key',$data);
    
    $somedata = $session->get('sess_key.somedata');
    
    if($somedata == 'somevalue'){}
    
    $session->delete('sess_key.somedata');
    

    I think it's the advantage is too small to make the effort adding a cleaner way to work with multidimensional arrays.

    And the probability of bugs will be great. You can store all datatypes in a session if you want to.

  • It doesn't matter if the var is protected, you have a reference anyway, you can do this:

    $data = & Session::instance()->as_array();
    $data['sess_key']['somedata'] = 'somevalue';
    
    if( $data['sess_key']['somedata'] == 'somevalue'){}
    
    unset($data['sess_key']['somedata']);
    

    that's why the team prepared Session::as_array to return a reference.
    Now, having paths I can make the things dynamically:

    $paths = array(
        'insert' => 'something.anotherthing.onemore.okitsready',
        ....
    );
    
    $myvar = Session::instance()->get( $paths['insert'] );
    $myvar = 'changed';
    Session::instance()->set( $paths['insert'] , $myvar);
    Session::instance()->delete( $paths['insert'] );
    

    Is not only clearer and easier, also useful, because If I want to change the path where I'm saving my data in the session, I just need to change it in one place.
    And for example, using PRG pattern I need to pass data between 4 actions, they must know the session path where the data is saved. And that is for each PRG.

  • I didn't know that, nice!

  • I've opened a Feature Request:
    http://dev.kohanaframework.org/issues/3984
    and also the team noted that this request was already done here:
    http://dev.kohanaframework.org/issues/3645
    And it's approved so I think there isn't any reason for not using paths in Session.
    And for the moment the only solution is to extend the class...

  • I wrote my Session extension for using it with paths, if you need it, it's here:
    https://gist.github.com/963662
    We need to extend Arr too (this is the ugly part, because I have 3 methods practically identical: Arr::set_path (original), Arr::bind_path and Arr::delete_path.
    But I've tested it and it works fine.

    Regards! PS: I don't know how to add syntax highlight in gist :(

  • Please include a link to your patch in the related issue, so it doesn't get lost.

    Regards! PS: I don't know how to add syntax highlight in gist :(

    You are missing your opening php tags ;)

  • Please include a link to your patch in the related issue, so it doesn't get lost.

    here? http://dev.kohanaframework.org/issues/3645

    You are missing your opening php tags ;)

    OK, fixed, thanks !

Howdy, Stranger!

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

In this Discussion