I have a issue , I am creating session but when we access session in another controller,
so the session variable not found there
Route
Route::get('entry1', 'TestController#entry1');
Route::get('entry2', 'TestController#entry2');
Controller function
public function entry1(Request $request)
{
Session::put('username', 'adminTest');
echo Session::get('username');
}
public function entry2(Request $request)
{
echo Session::get('username');
}
You can use like this :
To set value in session :
Session(['sessionId' => $sessionId]);
To get value from session
$sessionId = Session('sessionId');
Related
I have this two controller methods that both set session data
//Profile
public function profile()
{
$this->session->set_userdata('title', 'some_value');
}
//KYC
public function kyc()
{
$this->session->set_userdata('title', 'KYC');
$this->load->view('basic/basic_app_views/kyc');
}
I am first loading the function kyc and using the session in my view
<title><?php echo $this->session->userdata('title'); ?></title>
I then load profile in my browser and refresh kyc but the value in kyc is still KYC like i had set in the kyc function.
How come title still has the value KYC even after resetting the value in another controller function?.
It is because each time you call your function it will set the userdata as it defines.
When try to set a title in a page, i suggest you to do this instead.
//Profile
public function profile()
{
$data['title'] = 'Profile Page';
$this->load->view('basic/basic_app_views/kyc', $data);
}
//KYC
public function kyc()
{
$data['title'] = 'KYC Page';
$this->load->view('basic/basic_app_views/kyc', $data);
}
then call it inside your kyc.php file.
<title><?php echo $title; ?></title>
hope that helps.
To see the change in the session title variable you would need to change up your code a little...
So your
//Profile
public function profile()
{
$this->session->set_userdata('title', 'some_value');
}
//KYC
public function kyc()
{
$this->session->set_userdata('title', 'KYC');
$this->load->view('basic/basic_app_views/kyc');
}
To something like...
//Profile
public function profile()
{
$this->session->set_userdata('title', 'some_value');
$this->load->view('basic/basic_app_views/kyc'); // View the title value
}
//KYC
public function kyc()
{
$this->session->set_userdata('title', 'KYC');
$this->load->view('basic/basic_app_views/kyc'); // View the title value
}
That way you will see your value change. Each of your two methods individually change the value. But in your previous code only the kyc was displaying it.
I set cookies like this inside my indexController
public function index()
{
Cookie::queue('name',"$name", 60);
Cookie::queue('id',"$cookie_id", 60);
}
now i need to retrive all cookies values in another controller is there is any way to do that?
You should be able to just call Cookie::get() without the $name parameter (untested).
Otherwise you can get it directly from the request:
public function index(\Illuminate\Http\Request $request)
{
dump($request->cookies);
}
function __construct() {
$this->middleware('auth');
$this->middleware(function ($request, $next) {
$this->user = \Auth::user();
});
echo $this->user;
exit;
}
the problem is that when I echo the user inside the middleware function where I get the user his data is printed ok, that means he is loggeed in, but if I echo the same variable outside the function it is null, but the examples shows that it must be filled with data, what I'am doing wrong?
It seems issue of scope of the variable.
As you are defining and assigning value of varialbe inside function, you can not access it from outside.
Instead, if you do like this, it should work:
function __construct() {
public $user = ''; // Declared it here,
$this->middleware('auth');
$this->middleware(function ($request, $next) {
$this->user = \Auth::user()->role; // assigned a value here,
});
echo $this->user; // It will print here.
exit;
}
The variable $this->user will be an object so I believe echo will not work. If you want to check its value then use print_r($this->user); instead and if you want to access it in some other function then return $this->user it.
If you need the user inside middleware to set it in the controller. It is way better to do it simpler, same could be done in the middleware.
public function __construct(Request $request)
{
$this->user = $request->user();
dd($this->user);
}
This solution requires that the user has been loaded by Auth. This can be null at some point and for the route list generation in Laravel this will be called with a null user. If this still does not work, I do not believe your user is set or the Auth middleware has been initialized.
I am going to set a session variable called sliderhash in a controller called Main. In symfony2 I see the session variable and it is going to be set alright. If I do now make an ajax call, I cant't get to the session variable. How can I solve this problem?
MainController.php
MainController {...
public function setSessionAction() {
....
$session = $request->getSession();
$session->set('sliderhash', '1234');
AnalyticsController.php
AnalyticsController {...
public function piccounterAction(Request $request) {
$request->getSession();
$session->get('sliderhash'); //always returns NULL
I need to create function in magento for set session data and getSession data ...
I created on e but this is not working ..
public function mySession($data){
$_session = Mage::getModel('core/session');
$_session->set($data);
}
how can we write session function for set and gat
You can write your function like
public function mySession($custom_variable, $data){
Mage::getSingleton('core/session')->setData($custom_variable, $data);
}
public function getMySession($custom_variable){
Mage::getSingleton('core/session')->getData($custom_variable);
}