I have this very simple class:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
public function __construct()
{
// $this->middleware('auth');
}
public function home(Request $request)
{
echo "setting help key";
session()->put('help', 'me');
session(['sos' => 'me']);
dump(session('help'));
dump(session('sos'));
}
public function home2(Request $request)
{
dump(session('help'));
dump(session('sos'));
}
//...
which dumps the vars successfully in the home() page, but when i access to the home2() page, it fails. Maybe it has something to do with me disabling the default middleware('auth'), but i'm not sure (also, if that is the case, how to use sessions without forcing the login)
try
session(['key' => 'value']); // to store
session()->get('key'); // to get the value
session()->forget('key'); // to unset the session attribute
read also: https://laravel.com/docs/5.8/session (change the Laravel version at the top-right corner...)
P.S. Sometimes you can't just dump data. Try var_export or what's better, just debug.
Related
This is a part of the code.
class StudentController extends Controller
{
public function __construct(Request $request)
{
$school = session('school_data');
$this->middleware($school);
}
}
Now, I've also tried checking the $school by using dd($school) but it returns null
NOTE: The session variable works in other functions inside the same controller.
your sessions are not ready yet.
if you want to use them, use like below:
class StudentController extends Controller
{
public function __construct(Request $request)
{
$this->middleware(function ($request, $next) {
// fetch session and use it in entire class with constructor
$this->school_data = session()->get('school_data');
return $next($request);
});
}
}
from here: laravel - Can't get session in controller constructor
I am trying to integrate Socialite in my Laravel project. I am trying to store a session in buyerSignup.blade.php file and then trying to get that session value in Socialite's handleProviderCallback() method. But it is not showing any value don't know why. Although the other method
redirectToProvider() showing the session value.
I need that session value in handleProviderCallback() method to process it and take actions based upon the value of session. Below is a actual of code that I am using.
Storing a session value in buyerSignup.blade.php
#php
\Session::put('buyerSignupFb','true');
#endphp
Trying to get the above stored value in LoginController's handleProviderCallback() method.
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Auth;
use App\sellerData;
use App\buyerData;
use App\sellerDealCat;
use App\subCatData;
use App\Session;
use App\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\DB;
use Socialite;
class LoginController extends Controller
{
use AuthenticatesUsers;
protected $redirectTo = '/home';
public function __construct()
{
$this->middleware('guest')->except('logout');
$this->middleware('guest:seller')->except('logout');
$this->middleware('guest:buyer')->except('logout');
}
public function redirectToProvider()
{
return Socialite::driver('facebook')->redirect();
}
public function handleProviderCallback()
{
//cant get the value of the session defined in buyerSignup.blade.php
// echo doesn't work too
return \Session()->get('buyerSignupFb');
}
}
Any suggestion what I am doing wrong or how to make it work. TIA
U doing all good, but not enought, first u set a redirect provider
public function redirectToProvider()
{
return Socialite::driver('facebook')->stateless()->redirect();
}
it's good, when user will give access to his token, u need to handle callback in handleProviderCallback(), where u need to exchange user auth code to acces token. All of this socialite makes automatically, all what u need its just call it
public function handleProviderCallback()
{
$externalUser = Socialite::driver('facebook')->stateless()->user();
\\check if user exists, if not create
$auth->login($user, true);
if ($user->type = 'seller'){$this->redirectTo = '/forSellers'} else {$this->redirectTo = '/forOtherGroup'}
return redirect($this->redirectPath());
}
I'm getting the user_id from the session and using it quite a bit throughout my contrpller. So am looking at ways of retrieving that variable.
I have set everything up to get it (How I understand) but the Variable is returning
null
My Controller looks as follows :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\VideoLog;
class VideoController extends Controller
{
private $user_id;
public function __construct(Request $request)
{
$user_id = session('id');
$this->user_id = $user_id;
}
public function log_watched(Request $request)
{
dd($this->user_id);
// See If Video Has Been Watched Before....
$video_watched = VideoLog::where('user_id', $this->user_id);
}
}
Is it something to do with the session?
How would I retrieve it?
The reason you're having this issue is because the controller __construct method is run before the middleware that starts the session.
(more information here)
As the post says, you can get round this issue by using the middleware method in the controller's __construct method:
public function __construct()
{
$this->middleware(function ($request, $next) {
$this->user_id = session('id');
return $next($request);
});
}
This will allow you to set the user_id on the controller.
Hope this helps!
I want a variable to be shared by other controller methods. This variable can be updated by one controller method and the change should be reflected in other methods? any suggestions ? what is the best practice to do that ? this is my code:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use Session;
class test extends Controller
{
public $global;
public function __construct()
public function a(Request $request){
$this->global="some value"
}
public function b(Request $request){
echo $this->global;
//it always return a null
}
}
Set the variable inside your constructor.
function _construct() { $this->global = "some value";}
So, you don't only want a global variable, you also want that this variable should be changed by other routes as well. The one way to achieve this is using session.
function a() {
session()->put('global_variable', 'set by method a');
//your other logic
}
and from method b...
function b() {
//get the variable set by method a here
dd(session()->get('global_variable'));
}
You can create a new file in config and use
config('your_new_file_name.key')
Check this : https://laracasts.com/discuss/channels/general-discussion/laravel-5-global-variables
I have controller class in laravel in which i have a function create() and a variable attachment i am calling function by ajax
my class code is.
class AttachmentController extends Controller
{
public $_attachments;
public function create()
{
$this->_attachments[]= 'test';
var_dump($this->_attachments);
}
problem is every time when i call it by ajax it return me "test" at 0 index of attachment array . but i want if i call create function 1st time it give me test on 0 index but when next time when i call it . it give me "test" on both 0 and 1 index and so on ..
how it is possible please help me
To preserve the values between requests you need to store them somewhere, an alternative is to use sessions, as the example below, for more information see https://laravel.com/docs/session
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class AttachmentController extends Controller
{
public function create(Request $request)
{
$request->session()->push('attachments', 'test');
var_dump($request->session()->get('attachments'));
}
}
Try something like this.
$_attachments[] = Session::get('test');
$_attachments[] = 'test';
Session::get('test', $_attachments);
dd(Session::get('test'));
let me know if this is what you want.
Since HTTP driven applications are stateless, sessions provide a way
to store information about the user across requests.
class AttachmentController extends Controller
{
public function create()
{
$attachments = Session::get('attachments', array());
$attachments[] = 'test';
Session::put($attachments);
var_dump($this->_attachments);
}
}
Further reading: Laravel Session