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.
Related
I need to be able to store some custom session variables that exist for the custom, regardless of whether they're signed in or not, but for some reason my sessions keep deleting themselves.
I have used this example to help me add my session code in.
Here's my code
Block file
<?php
namespace MyVendor\MyModel\Block;
use Magento\Framework\View\Element\Template;
class ProductSearch extends Template {
protected $_customSession;
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Customer\Model\Session $customSession,
array $data = []
){
parent::__construct($context, $data);
$this->_customSession = $customSession;
}
//Get the car model from the session
public function getSessionCarModel(){
return $this->_customSession->getCarModel();
}
//Unset the car model from the session
public function unsetSessionCarModel(){
return $this->_customSession->unsCarModel();
}
}
and heres the top of my template file that sorts the session when its loaded
productsearchbanner.phtml
<?php
//If the user has selected a new model, unset our session then start a new one
if(isset($_POST['modelSelect'])){
//Unset the other sessions
$block->unsetSessionCarModel();
//Set the model session
$block->setSessionCarModel($_POST['model']);
}
echo '<pre>';
var_dump($_SESSION);
echo '</pre>';
?>
The way the code is meant to work is, if the $_POST['modelSelect'] is set, the user has come from the model select page so we need to start the process again and reset their session, but if they haven't, the session should remain the same.
My issue is, when I come from the model select page, my session variable shows in the var dump no problem, as shown below.
But then as soon as I go to any another page on my site (for example, the homepage) and then back to the product search page, the session has cleared?
What am I doing wrong? Why does my session clear every time I load a page? I just need to be able to set the equivalent of $_SESSION['carModel'] and it be persistent for that user, regardless of if they're logged in or not, or where on the site they go.
Can someone please point me in the right direction?
Setting sessions in Blocks or Template files is a problem. This is because of full page cache. Magento's execution cycle changes with FPC turned on.
Controllers or Models are the best places to update session data.
But, if you need to update your session in a template / block, then you can call a custom action via AJAX and have it update the session info.
Generally, one would need to take these steps in Magento 2:
create a new controller / action pair in an existing or a new module, that would update session info. This controller should ideally accept AJAX requests only.
have a template rendered in container before_body_end and toss some jQuery code in there, that will query the controller / action pair to have the session info updated.
This way, whenever the page will load, it will trigger a session update ( or you can have it trigger on any other event, say when a user clicks something etc. ) by requesting your controller / action, say, /my-module/my-controller/my-session-updater-action.
i have some question in Codeigniter session class:
whats different between these
$this->session->all_userdata();
$this->session->userdata
both of them return array of all user data in ci session (cooke).
and why this code is wrong:
$this->session->userdata();
but this one is correct:
$this->session->userdata
why?
for 1:
$this->session->all_userdata(); is method to get CI-session's data
$this->session->userdata is variable of CI-session's class, You shouldn't get session's data this way.
for 2:
$this->session->userdata(); is correct because this way You'll use setters-getters mechanism (read more here), $this->session->userdata is incorrect because You try to get data directly (read link above to get more info)
I am trying to add an activity for the current user through a function hooked to wp_ajax
add_action("wp_ajax_add_pair", "some_function");
function some_function(){
$args = array("action"=>"action string");
bp_activity_add($args);
}
some_function is called through ajax but I get bp_activity_add does not exist. This makes me think that BP hasnt loaded yet, but var_dump($bp) shows the variable is set. So need help to record activity for a user through ajax
Don't forget to enable activity streams from the backend! worked for me!
I'm working on getting joomla registration form values that user enters
at the time of registration. After two days of searching I reached to the
file Joomla2.5.7\components\com_users\controllers\registration.php. In the register() method I
tried to echo $data and $requestData variables but didn't see any output, on registering a new entry. I also tried to echo javascript but was unsuccessful. I'm trying to connect joomla database with my own database, so that whenever new user registers , he is also registered to my website. How can I get the registration form variables, any kind of help is really appreciated.
Ty this
On registration controller you can find a function call to model like this
$return = $model->register($data);
after that just
echo "<pre/>";
print_r($data);
Also in the registration model
components\com_users\model\registration.php
register() method is defined you can check that for more info.
In addition if you want to add the users info to the other DB like your website DB.
The best place to write mysql query is :
// Store the data.
if (!$user->save()) {
$this->setError(JText::sprintf('COM_USERS_REGISTRATION_SAVE_FAILED', $user->getError()));
return false;
}else{
//Your custom mysql query to other DB or tables
}
find the above section in the registration model inside register()method.
Hope this may solve your problem..
I hope this tutorial helps you. You will find it explains the process of joomla registration form very well.
http://youtu.be/2AyCzb2vTaU
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... ;-)