Sessions in laravel 5.3 don't start - laravel-5

I am having problems with storing values in a session. I am using laravel 5.3 on xampp local host. I have tried the following but when I try to access the session is empty:
//using global session helper method
$variable = session(['exam'=>$exam]);
$exam = session('exam');
echo $exam; //exam is always empty
//I have also tried this
public function myControllermethod(Request $request)
{
$exam = $request->session()->put('exam',$exam);
//get the session
$exam = $request->session()->get('exam');
echo $exam // this approach returns the following exception
}
RuntimeException in Request.php line 905:
Session store not set on request.
I have tried both the file and database drivers without lack. Sessions are actually not being registered at all. Session::get('key') also do not work,
How can I start sessions in laravel 5.3. Any Help?

Related

No query results error using artisan with object instantiated in service provider

I have created a service provider which provides a class App\Path. This is loaded up through Eloquent using $request->getPathInfo()
$this->app->singleton(Path::class, function($app)
{
$request = $app->make(\Illuminate\Http\Request::class);
$path = Path::with(['template', 'parts'])->findOrFail($request->getPathInfo());
return $path;
});
The app works fine and as expected. However when I want to use Artisan I get the following error:
In Builder.php line 369:
No query results for model [App\Path] /
This prevents me from clearing caches, creating models etc. It seems that Laravel runs register() when running any artisan command and when this done, the request path is "/" which doesn't exist in the DB. Is there a better way to populate the Path object? The only way to solve this seems to add a dummy record for "/".
You can check whether the app is running from console and adjust its logic, for example:
$this->app->singleton(Path::class, function($app)
{
if ($app->runningInConsole()) {
return null;
}
$request = $app->make(\Illuminate\Http\Request::class);
$path = Path::with(['template', 'parts'])->findOrFail($request->getPathInfo());
return $path;
});

How to Check if Session is set in Laravel 5.7

<?php
$request = request();
// if (empty($request)) return false; .// That does not work
$loggedUserAccessGroups = $request->session()->get('loggedUserAccessGroups');
$logged_user_ip = $request->session()->get('logged_user_ip');
In my Laravel 5.7 application, I want to check if the user has the right access level in the session. It works ok but I made automatic tests and got the error:
local.ERROR: Session store not set on request.
I added checks to see if the session is set and it fails to return false.
Which is the correct way? Thanks!
You may also use the global session PHP function to retrieve and store data in the session as outlined here:
// Retrieve a piece of data from the session with the global session helper...
$loggedUserAccessGroups = session('loggedUserAccessGroups');
$logged_user_ip = session('logged_user_ip');
// Store a piece of data in the session...
session(['key' => 'value']);
For more information look at the section of The Global Session Helper in the official documentation.

Laravel, Behat, Faker and GuzzleHttp - DB Rollback before I can get the data

I'm try to create a fake data for testing, and have the following code on the Behat Context
$modelFake = factory(User::class)->create();
$client = new GuzzleHttp\Client();
$res = $client->request('GET', url($this->apiURL . '/user/' . $modelFake->id));
After the class declaration I have:
use DatabaseTransactions;
The user fake it's created and I get the current Id to do the request, but the code return an empty object, If I remove the use DatabaseTransactions; the user persist on the database and the request return the object, but I can't realize what is my issue.
I hope someone can help me with this.
Regards
Edit:
When I try to get the user User::find($modelFake->id) I'm getting the object :(
Why when I made the request I can't get the object?
Edit 2:
The main problem it's the data persist only on the context and the request belong to another instance of the DB

CodeIgniter sess_destroy does not delete user_data session?

I am creating test case for my CodeIgniter app. However I just found something that I thought should not be happen :
in login.php controller :
public function logout()
{
$this->session->sess_destroy();
redirect('/');
}
So I just created a test to just make sure that session is really destroyed :
public function test_logout()
{
$this->CI = set_controller('login');
// make sure that all session is destroyed
$this->CI->session->set_userdata('test_session', 'some_value');
$this->CI->logout();
// userdata 'test_session' should be removed!
$this->assertTrue(($this->CI->session->userdata('test_session')==null || $this->CI->session->userdata('test_session')==''));
}
However I find that upon running the test case, my test case fails! Upon debug on the last line of test case, I found that the userdata is still exist with value = 'some_value'. I thought that sess_destroy should also delete all the set user data, as per what they described in their website documentation:
This function should be the last one called, and even flash variables will no longer be available. If you only want some items destroyed and not all, use unset_userdata().
I am using Kenji's CIUnit for unit testing.
Is this the correct behaviour or is there something that I missed?
Just found that CIUnit routes the Session to CIU_Session instead of original CodeIgniter's CI_Session. It miss a line that CI_Session does :
$this->userdata = array();
So turns out this is CIUnit's issue instead of CodeIgniter's. Create an issue in their bitbucket page.

core php session is lost in codeigniter when redirecting controller to controller

core php session is lost in codeigniter when redirecting from one controller to another.
In the first controller, it works perfectly when we assign value to it. Also, in the first controller, the session is correctly handled but after a redirect to another controller, the session is lost.
Here is first controller:
$_session['user'] = $data[0]['u_name']; // assign value to session
echo $_session['user'] // works fine here and print user name
redirect("useraccount",'refresh'); // redirection to user account
its not working in useraccount.php controller
here second controller
public function index() {
if(isset($_session['user']))
echo $_session['user'];
else
echo "no session";
//$data['main'] = 'users/dashboard';
//$this->load->view('index',$data);
}
it is printing no session. How do I pass the session to 2nd controller after redirecting
to second controller?
note: i want to use core php session and not codeigniter session.
Is there something wrong with:
$this->session->set_userdata('user', $data[0]['u_name']);
PHP variable names are case sensitive. it should be
if (isset($_SESSION['user'])) {
^^^^^^^--- note: all CAPS
Use session_start() property to access core php session
Try this
public function index()
{
session_start();
if(isset($_session['user']))
echo $_session['user'];
else
echo "no session";
//$data['main'] = 'users/dashboard';
//$this->load->view('index',$data);
}

Resources