Property CWebApplication.Cookies is not defined - php

I am using this link to use the cookies functionality in yii framework.
When I accessed the application in localhost it gives following error:
Property CWebApplication.Cookies is not defined.
Please help me guys that where I am doing mistake.
I have included
'Cookies' => array (
'class' => 'application.components.CookiesHelper'
),
in config->main.php and i am trying to use the function putCMsg
as follows
$this->putCMsg('someCookieName','SomeValue');
But this error comes on this line of index.php
Yii::createWebApplication($config)->run();

Creating cookie in config/main.php is not a good solution. It's better to set cookie in the controller's action like this:
$cookie = new CHttpCookie('someCookieName','SomeValue');
$cookie->expire = 200000; //times in milliseconds

Related

Symfony2 - Error on prod - Cannot use object of type Symfony\Component\HttpFoundation\Request as array

I have just uploaded my Symfony (2.7) project online and I have a 500 error happening only online in prod environnement (app.php). I have set $kernel = new AppKernel('prod', true); in app.php file in order to see the error message:
Error: Cannot use object of type Symfony\Component\HttpFoundation\Request as array
in vendor/symfony/symfony/src/Symfony/Component/HttpKernel/EventListener/RouterListener.php at line 143
}
if (null !== $this->logger) {
// Below is line 143
$this->logger->info(sprintf('Matched route "%s".', isset($parameters['_route']) ? $parameters['_route'] : 'n/a'), array(
'route_parameters' => $parameters,
'request_uri' => $request->getUri(),
));
(This file is part of Symfony, see complete code here.)
In local (WAMP), I have no issue using app.php or app_dev.php .
Online, app_dev.php is working well but when try to access http://mydomain.fr/web/, I have this error.
I am a bit lost here, if you need more information, just ask me which file or else I should copy in this question.
Just to see what happens I commented the logger line in RouterListener.php, I have another different error showing. I guess there's something wrong with my server's config or something like that... but I have no idea what I should look at.
Some controller code is trying to access values by keys on an object as if it were an Array;
<?php
$moo = (object) ['foo' => 'bar' ];
/* run-time error below: */
$moo['foo'];
This happens when you upgrade "the library" or API without updating client code. It happens also when client code (your code) confuses the order of parameters in function invocations.

How to include a separate file inside bootstrap.php that is meant for application wide constants and other application-wide Configure::write values?

I am using CakePHP3.
I prefer to put my application-wide PHP constants and Configure::write values inside a separate php file, usually called constants.php
And then at the end of the bootstrap.php, I will include this constants.php file.
I have no problems with the PHP constants. However, I have issues using Configure::write inside this separate file.
Is there a way to use Configure::write in a separate php file? I have tried using
use Cake\Core\Configure;
inside constants.php but I still get errors.
EDIT
Example of constants.php
<?php
/**
* provide all the kinds of site ID
*/
Configure::write('ADMINISTRATOR_SIDEBAR', array(
'quotations' => [
'link' => '/quotations',
],
'projects' => [
'sub_menu' => [
[
'title' => 'Project 1',
'icon' => '',
'link' => '/job_projects/view/5',
]
],
'link' => '/job_projects'
]
));
When I write that and then
require __DIR__ . '/constants.php';
in the last line of bootstrap.php
I get
Error: Class 'Configure' not found
File ...constants.php
Line: 8
When I then added use Cake\Core\Configure; at the top of constants.php, the error is removed.
Does this mean my issue is solved?
Do you have that many constants? Use class constants instead:
class UserType {
const ADMIN = 'admin';
const USER = 'user';
}
IMO it is better to keep them organized in classes than having tons of global constants. I dislike constants most of the time because you can't really never ever change them at run time. So this leaves just two use cases for them:
Things that should never ever change at runtime (which is rare)
Using them for "identifiers" instead of strings
Explaining 2. a little more: $userRole === 'admin' can fail because of a typo, you won't get an error and might end up with a pretty shitty to debug situation while doing $userRole === UserRole::ADMIN will throw an error if it is not present.
You're not showing any code, so no idea what you're doing wrong with Configure. Why are you not simply using Configure::load() and the `$config = []' array in the file you're going to load with that method?

Yii: $_SESSION undefined, but Yii::app()->session works

I have enabled session within my application like this (in config.php, components section):
'session' => array(
'class' => 'system.web.CDbHttpSession',
'connectionID' => 'db',
'timeout' => 86400,
'sessionName' => 'SOMEABSTRACT_PHPSESSID',
),
In UserIdentity I try to do this:
$_SESSION['userid'] = $model->id;
Which means that I will set that new session after I log in my user. But when I try to access the $_SESSION['userid'] or just $_SESSION — for reading — I get this exception:
Undefined variable: _SESSION
This is the way I'm trying to access this session array:
echo '<pre>';
die(var_dump($_SESSION, $_SESSION['userid']));
I don't have any clues about this, so my two simple questions for this issue are:
Why is this happening only when Yii's sessions are enabled and autoStart => true?
How to fix this?
PS I need this for CometChat integration with Yii framework.
You can have #session_start();. This will suppress the warnings if it session already starts and also set the variable like this Yii::app()->session['userid'] = $model->id; and retrieve the same with Yii::app()->session['userid'] wherever it is required.
Also you can set the logged in user like this after session start
$this->_id=$model->id;
$this->setState('title', $model->username);
$this->setState('role', $model->role);
Above are the default variables in yii for logged in users. To create new user entry in session you can have this Yii::app()->session['variablename']
Try like this, in your UserIdentity.php -
//After successfully authentication, create session
Yii::app()->session;
Yii::app()->session['userid'] = $user->id;
//Now you can get anywhere like this -
echo Yii::app()->session['userid'];

CakePHP Attachment component problem

I am trying to use the Attachment component (seen here). I followed the instructions and added this to my view:
<?= $this->Form->create('Event', array('type' => 'file'); ?>
<?= $this->Form->file('image_attach'); ?>
Then added this into my controller's add function:
$this->data['Event']['image'] = $this->Attachment->upload($this->data['Event']['image_attach']);
I also declared this at the top of my controller class:
var $components = array('Attachment' => array(
'photos_dir' => 'events'
));
When the form is submitted, I have it var_dump'ing my data variable to see what's being added. But before that happens, I get this error.
Notice (8): Undefined index: event [APP/controllers/components/attachment.php, line 67]
I've tried looking in the component's file, and all I can tell is it's trying to find an index that is the name of my model in the array I pass through. But, I already figured that out from the error above.
I just want to know if I've done something wrong, or what might be causing this to happen, whether it's my fault or the component's?
Thanks,
hmmm after reading the component... it seems to me that you need to call the upload() method like this:
$this->Attachment->upload($this->data['Event'],'image_attach');
Good Luck!

PHP (CodeIgniter) Pass Object Through Session

I am using PHP5 and CodeIgniter and I am trying to implement a single-sign on feature with facebook (although I don't think that facebook is relevant to the question). I am somewhat of a novice with PHP and definitely one with CodeIgniter, so if you think my approach is just completely off telling me that would be helpful too.
So here is in short what I am doing:
//Controller 1
$this->load->plugin("facebook");
$facebook = new Facebook(array (
'appId' => $fbconfig['appid'],
'secret' => $fbconfig['secret'],
'cookie' => true,
)
);
$fbsession = $facebook->getSession(); //works fine
$this->session->set_userdata('facebook', serialize($facebook);
Now I would like to grab that facebook object in a different controller.
//Controller 2
$facebook = unserialize($this->session->userdata('facebook'));
$fbsession = $facebook->getSession();
Produces the error: Call to undefined method getSession. So I look up more about serialization and think that maybe it just doesn't know what the facebook object's attributes are.
So I add in a
$this->load->plugin('facebook');
To controller 2 as well and I get a "Cannot redeclare class facebook." I am strongly suspecting that I am misunderstanding sessions here. Do I have to somehow tell PHP what kind of object it is? Thanks for the help.
Turns out I made a very stupid beginner mistake. My second controller was named Facebook! That's a little embarrassing....

Categories