Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Cookies problem

I have problem with cookies with phalcon 2.0.x

in controllers:

      $this->cookies->set(SITE_DOMAIN . '-I18n-lang', $lang, time() + $cookieLifeTime);

if :

    var_dump($this->cookies->has(SITE_DOMAIN . '-I18n-lang'));

Response: true

But:

    var_dump($this->cookies->get(SITE_DOMAIN . '-I18n-lang'));

Response:

    object(Phalcon\Http\Cookie)[53]
  protected '_readed' => boolean true
  protected '_restored' => boolean true
  protected '_useEncryption' => boolean true
  protected '_dependencyInjector' => 
    object(Phalcon\Di\FactoryDefault)[11]
      protected '_services' => 
        array (size=28)
          'router' => 
            object(Phalcon\Di\Service)[49]
              ...
          'dispatcher' => 
            object(Phalcon\Di\Service)[63]
              ...
          'url' => 
            object(Phalcon\Di\Service)[14]
              ...
          'modelsManager' => 
            object(Phalcon\Di\Service)[15]
              ...
          'modelsMetadata' => 
            object(Phalcon\Di\Service)[47]
              ...
          'response' => 
            object(Phalcon\Di\Service)[17]
              ...
          'cookies' => 
            object(Phalcon\Di\Service)[18]
              ...
          'request' => 
            object(Phalcon\Di\Service)[19]
              ...
          'filter' => 
            object(Phalcon\Di\Service)[20]
              ...
          'escaper' => 
            object(Phalcon\Di\Service)[21]
              ...
          'security' => 
            object(Phalcon\Di\Service)[50]
              ...
          'crypt' => 
            object(Phalcon\Di\Service)[36]
              ...
          'annotations' => 
            object(Phalcon\Di\Service)[24]
              ...
          'flash' => 
            object(Phalcon\Di\Service)[35]
              ...
          'flashSession' => 
            object(Phalcon\Di\Service)[26]
              ...
          'tag' => 
            object(Phalcon\Di\Service)[27]
              ...
          'session' => 
            object(Phalcon\Di\Service)[48]
              ...
          'sessionBag' => 
            object(Phalcon\Di\Service)[29]
              ...
          'eventsManager' => 
            object(Phalcon\Di\Service)[30]
              ...
          'transactionManager' => 
            object(Phalcon\Di\Service)[31]
              ...
          'assets' => 
            object(Phalcon\Di\Service)[32]
              ...
          'config' => 
            object(Phalcon\Di\Service)[33]
              ...
          'viewCache' => 
            object(Phalcon\Di\Service)[37]
              ...
          'dataCache' => 
            object(Phalcon\Di\Service)[39]
              ...
          'voltService' => 
            object(Phalcon\Di\Service)[41]
              ...
          'db' => 
            object(Phalcon\Di\Service)[43]
              ...
          'dblogs' => 
            object(Phalcon\Di\Service)[45]
              ...
          'view' => 
            object(Phalcon\Di\Service)[64]
              ...
      protected '_sharedInstances' => 
        array (size=12)
          'cookies' => 
            object(Phalcon\Http\Response\Cookies)[52]
              ...
          'session' => 
            object(Phalcon\Session\Adapter\Redis)[54]
              ...
          'crypt' => 
            object(Phalcon\Crypt)[55]
              ...
          'router' => 
            object(Phalcon\Mvc\Router)[56]
              ...
          'view' => 
            object(Phalcon\Mvc\View)[65]
              ...
          'dispatcher' => 
            object(Phalcon\Mvc\Dispatcher)[66]
              ...
          'Frontend\Controllers\GeneralController' => 
            object(Frontend\Controllers\GeneralController)[67]
              ...
          'config' => 
            object(Phalcon\Config)[3]
              ...
          'request' => 
            object(Phalcon\Http\Request)[68]
              ...
          'viewCache' => 
            object(Phalcon\Cache\Backend\Memcache)[70]
              ...
          'assets' => 
            object(Phalcon\Assets\Manager)[72]
              ...
          'response' => 
            object(Phalcon\Http\Response)[81]
              ...
      protected '_freshInstance' => boolean true
      protected '_eventsManager' => null
  protected '_filter' => null
  protected '_name' => string 'feedback-I18n-lang' (length=18)
  protected '_value' => string 'es_ES' (length=5)
  protected '_expire' => int 1432220527
  protected '_path' => string '/' (length=1)
  protected '_domain' => string '' (length=0)
  protected '_secure' => boolean false
  protected '_httpOnly' => boolean false

But, i expect the value save!.. not the object...! it is a bug???



33.6k
Accepted
answer

Not a bug!

Just call getValue() in the returned object:

var_dump($this->cookies->get(SITE_DOMAIN . '-I18n-lang')->getValue());

Also, it supports toString, so you can use:

$value = (string) $this->cookies->get(SITE_DOMAIN . '-I18n-lang');

Perfect,

$this->cookies->get(SITE_DOMAIN . '-I18n-lang')->getValue()

this works too:

(string) $this->cookies->get(SITE_DOMAIN . '-I18n-lang');

Thanks!