Reading a session variable inside a behavior in cakephp 2 - session

I have a behavior which enables segregation of user data based on the user id stored in the session. In CakePHP 1.3 you could do this:
App::import('Component', 'Session');
$session = new SessionComponent();
$session->read('Auth.User.id');
But in CakePHP 2, you can't instantiate a component like that in a behavior because the Component __construct requires the Controller's ComponentCollection as a parameter.
Is it possible to access a session variable inside a behavior in CakePHP 2? What's the best way to do it?

If you look at the SessionComponent code, you will see that it is only a wrapper for the CakeSession class.
So you can do the following:
App::uses('CakeSession', 'Model/Datasource');
$user_id = CakeSession::read('Auth.User.id');

In CakePHP 2.0 you can also simply call the Session-methods via the static CakeSession::method() without having to load anything... ;-)

Related

Is it ok to use $_SESSION['variables'] in Drupal 8?

I need a way to store temporary data for anonymous users.
Apparently this is not possible with:
\Drupal::service('user.private_tempstore')
Unless you write a custom constructor for the session management and stuff, which seems a little far-fetched to me?
I tried using
\Drupal::service('user.shared_tempstore')
But that saves the temp data for all anonymous users. So it's not linked to a single user.
Using raw $_SESSION['data'] works fine, but I'm not sure if I'm supposed to be doing this in Drupal and how safe/unsafe it is to do this?
Sessions (Drupal 8) are used via the simple Session implementation of SessionInterface interface. See Complete Tutorial of Sessions (Drupal 8).
Example:
use Symfony\Component\HttpFoundation\Session\Session;
$session = new Session();
$session->start();
// set and get session attributes
$session->set('name', 'Yash');
$session->get('name');
// set flash messages
$session->getFlashBag()->add('notice', 'Profile updated');
// retrieve messages
foreach ($session->getFlashBag()->get('notice', array()) as $message) {
echo '<div class="flash-notice">'.$message.'</div>';
}
I am not answering your specific question (regarding $_SESSION) because I have successfully used:
$session = \Drupal::service('user.private_tempstore')->get('your_module');
$session->set('whatever', $whatever);
from within procedural code (i.e. hooks, themes) without problems.
Pay attention that this private tempstore has to be assigned to a module (for the lack of a better way of saying this) which is the purpose of this line
$session = \Drupal::service('user.private_tempstore')->get('your_module')
After you get the private tempostore you can now set and get the session values:
$session->get('whatever');
$session->set('whatever', $whatever);
EDIT
Sorry, you explained correctly. I didn't get the critical part 100% ;)
You can always access the Session object from the request.
$session = \Drupal::request()->getSession();
$session->set('whatever', 'hello');
$value = $session->get('whatever', 'default');
I've been using plain PHP $_SESSION variables for a while now.
Did some research on them and they should be perfectly safe to use.
They're working correctly everywhere I use them and they have been working correctly for a while.
Don't think there's any issue using them in Drupal 8.

Cakephp 2.0 can't get or read session view file?

I'm working to display or read session in view ctp file, but controller showing is session created and read session is also showing in the controller but can't display or read session in view ctp file?
controller function
var $components = array('Auth','Session','RequestHandler','Email');
$selectedlocation= $_POST['location'];
$this->Session->write('homepagelocation.selectlocation', $selectedlocation);
echo $this->Session->read('homepagelocation.selectlocation');
session reading method in ctp file
echo $this->Session->read('homepagelocation.selectlocation');
In order to access data from the controller in your view, you need to set the data to the view.
var $components = array('Auth','Session','RequestHandler','Email');
$selectedlocation= $_POST['location'];
$this->Session->write('homepagelocation.selectlocation', $selectedlocation);
$this->set('location', $this->Session->read('homepagelocation.selectlocation'));
I will ask however, why are you writing data to the session, reading from it and setting that to the view when you already have access to the data you need in $selectedlocation?
Ello, mate. I think you $_POST[] does not work this way into your controller, you should try:
$this->request->data['location']; //Cake 2.x
$this->data['location']; //Cake 1.3
Then you set up the session to the view:
$this->set('location', $this->Session->read('homepagelocation.selectlocation'));
Now you can print it on your view:
echo $location;
You can check here: http://book.cakephp.org/2.0/en/core-libraries/helpers/session.html
It is possible to use Session read method in view, please set Session as $helper element in Controller.

Joomla 2.5 getUserStateFromRequest load error

I was following the example to implement custom filters in Joomla 2.5 admin component.
But I am getting error at models populateState method:
Call to undefined method
somecompModelsomecomp::getUserStateFromRequest().
$app = JFactory::getApplication('administrator');
// Load the filter state.
$search = $this->getUserStateFromRequest($this->context.'.filter.search', 'filter_search');
Error disappears if I call getUserStateFromRequest using $app:
$app->getUserStateFromRequest($this->context.'.filter.search', 'filter_search');
So whats the problem? In default Joomla components I've seen that it use the same approach and it works. Maybe I miss something in my model class?
Any ideas?
This is happened because $app is an object of your application class. As you defined it in your code.
$app = JFactory::getApplication('administrator');
and getUserStateFromRequest method is defind in that Application class.so you have to use it like this if you want to access this method.
$app->getUserStateFromRequest($this->context.'.filter.search', 'filter_search');
And for your information $this variable is your local object.

How to cache data in symfony2

Is there any built in possibility (or an external bundle) to cache data in Symfony2?
I don't want to cache the page itself, but data inside the application, using a simple key -> value store on the file system for example.
There's no built in solution, but I recommend you giving APC, Redis or Memcache a try (they're all in-memory datastores).
You can use LiipDoctrineCacheBundle to integrate cache drivers from Doctrine common into your Symfony project.
i'm using winzouCacheBundle. it gives you a streamlined cache api on different backends (apc,file,memcache,array,xcache, zenddata).
For now, there is no unique solution for caching in Symfony2. Some parts of the framework use Doctrine Common.
There are discussions about a "standard" caching solution if Symfony2, but we will have to wait for some time...
I think the DoctrineCacheBundle is currently the way to go.
The DoctrineCacheBundle allows your Symfony application to use different caching systems through the Doctrine Cache library.
Docs # Symfony.com
Code # Github
If I understand well, you would like to store data (attached to the session) and reload them when the same session will call again a new controller, in order to avoid to execute the same procedure more times (for example to read a table from a database).
You can use the session system in your controllers:
<?php
namespace YourStuff\YourBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
class YourController extends Controller
{
$session = $this->get("session");
$variabile = 4;
$session->set("variableName",$variable); // setter
if ($session->has("variableName") // to check if the variable exists
{
$variableName = $session->get("variableName"); // getter
}
}
This is an example; the "variableName" could be accessed next time the same session will be called, if the lifetime of the session is not yet expired.
The "session" uses the __SESSION variable of PHP, so be sure to set correctly the session.cookie_lifetime and session.gc_maxlifetime, in order to give the desired lifetime.

Manual dispatching possible in Zend Framework?

i'm wondering how i can manually start execute a controller action of my MVC application. My goal is to integrate the html output of /myController/myAction into another php application (typo3) using a simple include. I thought of manually instantiating the controller, the view and the layout, to bypass the dispatcher. Sadly i can't get it working. My current approach looks like this:
// standard bootstrap ... setting up autoloader, database etc.
$layout = new Zend_Layout();
$layout->setLayoutPath(('/application/default/layouts'));
$layout->setLayout('main');
$layout->setContentKey('content');
$view = new Zend_View();
$controller = new IndexController(new Zend_Controller_Request_Http($currenUrl), new Zend_Controller_Response_Http());
$controller->view = $view;
$controller->init();
$controller->hinweisAction();
$layout->content = $view->render();
echo $layout->render();
Instantiating the layout is no problem, but it becomes complicated when creating the controller. Setting the view instance after calling the constructor does not work, because the view is needed already during the instantiation.
What would be the "right" way for such a scenario? Maybe implement a simple user defined dispatcher which uses predefined controller and action names from me?
Greets
Georg Wächter
If you're using Zend_Application, all you need to do is something like this:
$application->bootstrap();
$request = new Zend_Controller_Request_Http();
// Set some parameters for request possibly
$controller = $controller = new IndexController($request, new Zend_Controller_Response_Http());
$controller->dispatch('hinweisAction');
Zend_Application will take care of setting up your view for you. Calling dispatch will take care of Action Helpers, particularly the ViewRenderer which does all the dirty work for you.
I'd suggest you looking at Zend_Test_PHPUnit_ControllerTestCase source code. It does exactly what you need to run tests against the content generated by a controller. Specifically, read the bootstrap() and dispatch() functions. May be you can just copy that verbatim.
Try:
$layout->content =
$controller->view->render();
$view is referring to the local instance of $view. Once you assign it to $controller->view, any work that $controller->init() and $controller->hinweisAction() does on it will affect $controller->view, not the local $view object.

Resources