Reputation: 11
I am a beginner with Cakephp and I have a big problem with sessions...
What's happening exactly
I start initialize the session with ajax calling a php file (a controller)
this is the url from ajax->url: http ://domain.com/things/view/0
I tried also without ajax by calling this url: http ://domain.com/things/view/1
In both of cases session is iniatialized. Below is the controller I use to iniatialize session
class ThingsController extends AppController {
var $name = 'Things';
var $components = array('Session');
var $helpers = array('Html', 'Javascript', 'Ajax');
var $uses = null;
function index() {
// preload dynamic data
$this->set('data1', 'content will update here');
$this->set('data2', 'here too');
$this->render('neat');
}
function view($id, $idd) {
if ($idd == 0) {
$this->Session->write('language', 'ro');
} else {
$this->Session->write('language', 'en');
}
if (is_numeric($id)) {
if ($id == 0) {
$this->Session->write('language', 'ro');
} else {
$this->Session->write('language', 'en');
}
}
$this->render('neat');
} //view()
} //ThingsController
As you can see if SESSION['language'] == 0 is ro else is en
The rooter rule used is:
Router::connect('/things/:id/*', array('controller' => 'things', 'action' => 'view',
'things'), array('pass' => 'id'));
But works also without this rule :)
Now in the page about the session works: http: //domain.com/despre-noi
I used this rooter:
Router::connect('/despre-noi/*', array('controller' => 'about', 'action' => 'display', 'about'));
And this controller
function display() {
if (isset($_SESSION["language"])) {
$language = $_SESSION["language"];
$this->Session->write('language', $_SESSION["language"]);
} else {
$this->Session->write('language', 'ro');
$language = "ro";
}
include('views/pages/'.$language.'.ctp');
$this->layout = 'default';
$this->About->id = 1;
$this->set('post1', $this - > About - > read());
}
But in page partners : http: //domain.com/parteneri
Where I used this rooter:
Router::connect('/parteneri/*', array('controller' => 'partner', 'action' => 'display', 'partner'));
and the folowing controller:
function display() {
if (isset($_SESSION["language"])) {
$language = $_SESSION["language"];
$this->Session->write('language', $_SESSION["language"]);
} else {
$this->Session->write('language', 'ro');
$language = "ro";
}
include('views/pages/'.$language.'.ctp');
$this->layout = 'default';
$this->set('posts', $this - > Partner - > find('all'));
}
it doesn't work..
This 2 pages are veri similiar with the difference that in the partner page I used $this->set('posts', $this->Partner->find('all'));
and then in view with a foreach()
I list the items and in the page about I read a row
$this->About->id = 1;
$this->set('post1', $this->About->read());
Might be this the problem...the 2 functions read() vs find()..or is just a coincidence?
Thanks in advance
Upvotes: 0
Views: 6906
Reputation: 2303
First make sure you have:
Configure::write('Session.save', 'php');
in your app/config/core.php
file. The Sessions Chapter of the CakePHP book give some good examples on how to configure and use the Session Component.
Also, if you're going to use sessions (i.e. the Session
component), you better put:
var $components = array('Session');
In your AppController
. In this way every controller will have that component available.
Second, if you are using CakePHP's Session class to write session data, then you should not access that data directly (i.e. by calling $_SESSION['language']
). This is bad practice since, if you ever decide to use 'database'
as a session storage or if CakePHP decides that Session data should be encrypted, then your code would break and you'd have no idea why.
In short, please use $this->Session->read('language')
in your code (and also, take a look at the aforementioned Sessions Chapter, it also gives some good background.
Also, since what you want to achieve is Internationalization, after fixing your Controllers as per the pastes in my comments you have to absolutely take a look at the easy way for Localization by reading through the CakePHP Internationalization Section. There is a way to do things more easily and separate presentation logic from presentation contents in your views -- but that's material for another question ;)
Upvotes: 2