Automatic tests fails (cookies)
PermalinkWe just submitted our first add-ons to the marketplace.
In our add-on we use cookies. We use the setcookie() for writing the cookie and $_COOKIE[] for retrieval.
We created 2 functions to get and set the cookie, because we need to create the cookie value. The functions are called _getCookie() and _setCookie(). Is this why the automatic tests fails?
Thanx!
4Concrete5
http://www.concrete5.org/api/class-Concrete.Core.Cookie.Cookie.html...
My second question is:
how can I use $_SESSION variable? How can I set and get $_SESSION variable? I cannot find clearly answer in concrete5 documentation. Regards,
\Core::make('cookie')
should work. ;)
For sessions, I think:
$session = \Core::make('session'); $session->has('blabla'); $session->get('blabla'); $session->set('blabla', 'foofoo);
but i have still problem with cookies... please see on my code. I try in this way:
public function getPackage()
{
$ch = \Core::make('cookie');
$cookie = $ch->get('my_cookie');
print_R($cookie);
exit;
if (!$this->package) {
$this->package = \Package::getByHandle('chatwee');
}
return $this->package;
}
I get this error (see attachment)
Thanks,
I have still problem with Cookie Class.
problem was found with a typo. Please look on this:
$session = \Core::make('session');
$session->get("my_session"); // value of my_session
$session = \Core::make('cookie');
$session->get("my_cookie"); // error !!
It is very weird. How to resolve it?
$this->cookies = \Core::make('Cookie');
$session->get("my_cookie"); // value of my_cookie
Small difference but very significant. Thanks for reply!
problem was found with a typo. Please look on this:
$session = \Core::make('session');
$session->get("my_session"); // value of my_session
$session = \Core::make('cookie');
$session->get("my_cookie"); // error !!
It is very weird. How to resolve it?
$this->cookies = \Core::make('Cookie');
$session->get("my_cookie"); // value of my_cookie
Small difference but very significant. Thanks for reply!
I try on this way:
$sessionId ="43JFA2NCZA1";
$CookieDomain = ".concrete.org"; //for example
$cookies = \Core::make('Cookie');
$cookies->set("chch-SI", $sessionId, time() + 2592000, "/", $CookieDomain);
but this doesn't work
Here's a piece of code (working) from a plugin that I made (and use).
Baking cookie:
// Json encode data. $data = Json::encode($data); // Cookie vars. $ttl = time() + (60 * 60 * 24); // just a timestamp in real app! $path = '/'; $domain = $_SERVER['HTTP_HOST']; // Get cookie 'helper'. $ch = \Core::make('cookie'); // Set cookie. $ch->set('my_recipe', $data, $ttl, $path, $domain);
Eating cookie:
// Cookie 'helper'. $ch = \Core::make('cookie'); // Get cookie. $cookie = $ch->get('my_recipe');
You should use "\Core::make('cookie');" (lowercase cookie). You can see it in here:
(notice the 'cookie' in the register function).
namespace Concrete\Core\Cookie; use Concrete\Core\Foundation\Service\Provider as ServiceProvider; class CookieServiceProvider extends ServiceProvider { public function register() { $this->app->singleton( 'cookie', '\Concrete\Core\Cookie\CookieJar' ); } }
Use the concrete5 way to get/set cookies.
Something like:
[code]
$ch = \Core::make('cookie');
$cookie = $ch->get('my_cookie');
[\code]