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
Related
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');
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 haven't gotten the hang of the extract() function, and transferring variables.
I have a method in a user controller where some variables are defined, and sent in an array to a view function in a parent controller, where the array is extracted. Then the view is required. But the variables turn out undefined. The array contents can be printed though.
Here is the user controller with a simplified profile function:
class User extends Controller{
public function profile(){
$profiledetails = $this->profiledetails();
$profilestatus = $this->profileStatus();
$this->view('profile', [$profiledetails, $profilestatus]);
}}
The variables are sent to the view function in parent Controller:
class Controller {
public function view($view, $variables =[]){
extract($variables);
require_once './app/views/' . $view . '.php';
}}
And in the view, 'profile.php', the undefined variable error is shown. I thought that the "extract()" function would make $profiledetails and $profilestatus available as variables in the view.
What am I doing wrong? Maybe I'm using the wrong type of array, or I should use "variabe variables" or something.. (in that case, how?).
extract works with an associative array.
$this->view('profile',
[
'profiledetails' => $profiledetails,
'profilestatus' => $profilestatus
]);
I'm saving the ID of the conected user in a static variable at MainController, but I need to access this variable in others controllers. When I try to get the value from the variable, the result is always the initial value of the variable, even when I have already modified it.
class MainController extends AppController {
//...
public static $loggedClienteId;
//functions
public function loginCliente(){
//code...
self::$loggedClienteId = $cliente['Cliente']['id'];
var_dump(MainController::$loggedClienteId); //returns the correct value.
return $this->redirect(array('controller' => 'clientes', 'action' => 'index'));
}
}
So, in another controller...
include "MainController.php";
class ClientesController extends AppController {
public $helpers = array('Html', 'Form');
public function index() {
var_dump(MainController::$loggedClienteId); //null, althought it already has a value...
$this->set('clientes', $this->Cliente->find('all'));
}
//functions...
}
Why is that happening?
Use $this->Auth->user('id') to get the current logged in user's id.
The reason your code does not work is because once the request for the login action is completed, the script is over. Setting a variable does not persist across requests. You have to save variables in the session for that.
If it's not the logged in user's id you need, what you have to do is use the SessionComponent and use $this->Session->write('key', 'value'); and to read it in another request/controller $this->Session->read('key');.