Symfony session doesn't work on ubuntu - php

Hello I have a problem with storing data to session using Symfony.
Here is my code:
public function currentAction() {
$session = $this->get('session');
$userArray = $session->get('test');
if($session->get('test') == null) {
$userArray = User::toArray($this->getUser());
$session->set('test', $userArray);
}
$this->setModel($userArray);
$this->sendResponse();
}
When I try to execute the code for the secound time the $session->get('test') still returns null. Can anybody helps me?
I also use (use Symfony\Component\HttpFoundation\Session;) from the beginning of my controller class.

Related

Anybody knows how ->dump() works?

Anybody knows how $myModelClass->dump() works?
I tried several times with models in different states but switched to
var_dump($myModelClass->toArray());
because ->dump() doesn't output (or return) anything for me.
Edit:
And var_dump($myModelClass->dump()); dumps always an empty array.
Edit2:
Here' an example how I use it in the indexAction of IndexController:
public function indexAction() {
$this->view->disable();
$u = new User();
$u = $u::findFirstByUsersId(56);
var_dump($u->dump()); //empty array
$u = new User();
$u = $u->find('usersId = '.(56))->getFirst();
var_dump($u->dump()); //empty array
var_dump($u->toArray()); //correct output
}
Hierarchy for User is:
User <- BaseUser <- UsersStorage <- Phalcon\Mvc\Model
Which should not matter, because the above example gives same results with UsersStorage objects.
Per the PhalconPHP API, ->dump() must be used in conjunction with var_dump().
$myModelClass = SomeClass::find();
foreach ($myModelClass as $record) {
var_dump($record->dump());
exit;
}
or
$myModelClass = SomeClass::findFirst(1);
var_dump($myModelClass->dump());
exit;
http://docs.phalconphp.com/en/latest/api/Phalcon_Mvc_Model.html

Making a PUT request in laravel?

This was my first crack at making a PUT request, after making a successful GET I was excited then following a tutorial I get an error that I don't quite understand.
Here is the error
"{"error":{"type":"ErrorException","message":"Undefined property: Symfony\\Component\\HttpFoundation\\ParameterBag::$name","file":"C:\\wamp\\www\\basketball-app-framework\\app\\controllers\\PlayersController.php","line":67}}"
I get that its on line 67 and the property is obviously undefined, but I followed the tutorial and I am not sure how else to write this, or why it's not working like the tutorial. I feel it could be because of mod_rewrite, I had an issue with that, and had to set some things in the apache config file just to make the GET request, so possibly I need to change some things to make the PUT.
Here is the code to PUT data.
public function update($id)
{
$input = Input::json();
$player = Player::find($id);
$player->name = $input->name; // this is line 67
$player->save();
}
again the error occours with $player->name = $input->name;
In my show method when I send a GET
public function show($id)
{
return Player::find($id);
}
Player::find($id); obviously works
so the $player variable inside my update shouldnt be causing an issue in accessing the name attribute, so I am at a loss and hoping someone can help this noob out.
Thanks.
Try the following:
public function update($id)
{
$input = Input::all();
$player = Player::find($id);
$player->name = $input['name'];
$player->save();
}
Or Try this:
public function update($id)
{
$player = Player::find($id);
$player->name = Input::get('name');
$player->save();
}

Illegal string offset Warning PHP '#type' in VBridgeApp->create()

I get a strange PHP error after updating my php version to 5.4
This is my function
protected function create() {
//if (VBRIDGE_DEBUG)
//drupal_set_message(__CLASS__ .'::'.__METHOD__);
$path = $this->vbridge_root_path;
$path_vbridge = $path . '/' . VBridge::VBRIDGE_CLASS_PREFIX;
$subclass = $this->getClass();
foreach ($this->_objclass as $objclass) {
if (!$this->createObj($path, $objclass, $subclass)) {
$this->createObj($path_vbridge, $objclass);
}
}
if (self::getStatus()) {
return false;
}
// Set User Session Qookie
//$this->getUser()->setQookie($this->getQookie());
// Set User Session
$this->getUser()->setSession($this->getSession());
$this->getSession()->setQookie($this->getQookie());
//$this->getUser()->setAuth($this->getAuth());
// Set User Pass
$this->getUser()->setPass($this->getPass());
// Set Auth
$this->setAuthMethods();
$this->setAuthStorages();
//
foreach ($this->getConfig() as $config) {
if ($config['#type'] == '#class') {
//createObj($config['#name'], $config['#type'], $config['#class'], $config['#path'], $appData['#config']);
}
}
return true;
}
This is the line that gives
if ($config['#type'] == '#class') {
I've looked at similar questions but haven't figured out how to fix this. Any assistance would be helpful.
Edit: Yes, I did put wrong code up last night. I was very tired after trying to tangle with this.
The error message you've posted doesn't match the line of code you say it originates from. You should get a different error for that, specifically to do with only using variables by reference.
Your code should be
$accounts = user_load_multiple(array(), array('name' => $login));
$account = array_shift($account);
But Drupal already has a helper method for that, so you might as well use it:
$account = user_load_by_name($login);

Initializing array returns null in PHP

I'm doing a project using HTML5 WebSockets, with a PHP server using a websockets library right here at Github.
My project also depends on knowing how many players are online. The library has three abstract methods, connected, closed, and process. In connected and closed, it takes a parameter $user, which is a custom class that has a random alphanumeric string as the variable id.
I have a protected $users = []; at the beginning of my class, inside my class, which extends the WebSocketServer that the library provides. In my connected method, I array_push the $user provided to the $users array. Then, in my closed method, I loop through the $users array, checking if the element in $users has the same $id as the $user provided, and then array_splicing that element away if that is try.
So. Here's my problem. When I run my PvPASCIIServer.php as root, and connect using a test web page, everything works fine. BUT, when I disconnect, it says:
PHP Warning: array_splice() expects parameter 1 to be array, null given in /var/www/PvPASCII/PvPASCIIServer.php on line 24
Shouldn't array() not initialize $users as null? Why would it? I've also tried using the literal format of initializing arrays, [], but even that didn't work. And even wierder, my array_push at the beginning of my connected function did not return an error message. Logically, it should have worked and pushed a $user to the end of the $users array, so even if it was initialized null, it should have not been null after that.
My code, if you need it:
#!/usr/local/bin/php
<?
require_once("websockets.php");
class PvPASCIIServer extends WebSocketServer
{
protected $users = [];
protected function connected($user)
{
$this->send($user, "say Pong!");
array_push($this->users, $user);
echo $user->id;
return true;
}
protected function closed($user)
{
echo "Client " + $user->id + " disconnected from the server.";
for($i = 0; $i < sizeof($this->users); $i++)
{
if($this->users[$i]->id == $user->id)
{
array_splice($users, $i, 1); // <-- Line with the error
}
}
}
protected function process($user, $message)
{
// Yet to be determined.
}
}
$host = "localhost";
$port = 3000;
$server = new PvPASCIIServer($host, $port);
$server->run();
?>
$users needs to be $this->users just like everywhere else in your class:
if($this->users[$i]->id == $user->id)
{
array_splice($this->users, $i, 1); // <-- Line with the error
}
array_splice($users, $i, 1); // <-- Line with the error
Should be:
array_splice($this->users, $i, 1); // <-- Line with the error
Since you want to use the class variable $users and not the function variable $users
EDIT:
What John Conde also says (he was a little faster with typing ;-) )

Accessing array in class structure

I want to validate a form with php.
Therefor I created a class "benutzer" and a public function "benutzerEintragen" of this class to validate the form:
class benutzer
{
private $username = "";
private $mail = "";
private $mail_check = "";
private $passwort = "";
private $passwort_check = "";
public $error_blank = array();
public $error_notSelected = array();
public $error_notEqual = array();
public function benutzerEintragen () {
$textfields[0] = $this->username;
$textfields[1] = $this->mail;
$textfields[2] = $this->mail_check;
$textfields[3] = $this->passwort;
$textfields[4] = $this->passwort_check;
foreach ($textfields as $string) {
$result = checkFormular::emptyVariable($string);
$error_blank[] = $result;
}
In the function "benutzerEintragen" i filled the variables "username,mail" and so on with the appropriate $_POST entries (not shown in the code above). The call
checkFormular::emptyVariable($string)
just returns "TRUE" if the field is not set or empty otherwise FALSE.
Now when i try to create a new instance of this class, execute the function and get access to $error_blank[0] the array is empty!
if (($_SERVER['REQUEST_METHOD'] == 'POST')){
$BENUTZER = new benutzer();
$BENUTZER->benutzerEintragen();
echo $BENUTZER->error_blank[0];}
So the last line is leading to a "Notice: Undefined offset: 0". It seems to be related to the array structure, because if i do
echo $BENUTZER->mail;
I get any input I wrote in the form, which is correct. Also the foreach loop seems to do the right thing when i run the debugger in phpEd, but it seems like the array "error_blank" is erased after the function is executed.
Any help would be greatly appreciated. Thanks in advance.
There is a scope problem here. You do have a class attribute with the name. Unlike in Java where using a local variable with the same name as a class variable automatically selects the class attribute this is not the case in PHP.
Basically you are saving your output in a local variable which gets discarded once you leave the function. Change $error_blank[] = $result; to $this->error_blank[] = $result; and you should be fine.
First of all this seems overly complicated way to do a simple task, but that wasn't actually the question.
You are creating a new $error_blank variable that is only in function scope. If you wish to use the class variable you should use $this->error_blank[]

Categories