Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

laksh's avatar
Level 1

constructor logic doesn't work on parent class function

i made a constructor function in my class and in which set that if anybody can call the function from class it will generate a token if it doesn't previously set in the session and it works on different classes but if i used it on function which is present is that constructor class the logic doesn't working and it generates token on every call

here's my code for constructor

public function __construct(){

    $sessionName = '';
    
    $loginObj = $this->crmLogin($this->ws_url,$this->username,$this->accesskey);
    
    if($loginObj->sessionName){
        $sessionName = $loginObj->sessionName;
        session()->put('sessionName',$sessionName);
    }
    
    $data['accountholder_no'] = "AH00000059";
    
    $postParams = array(
        'operation' => 'GenerateToken',
        'sessionName' => $sessionName,
        'element' => json_encode($data)
    );
    
    
    $token_response = $this->postHttpRequest($this->ws_url, $postParams);
    
    $token_response = json_decode($token_response);
    
    if(Session()->has('bearer_token')){
        return null;
    }else{
        $this->generateToken($data);
    }
0 likes
13 replies
Sinnbeck's avatar

Show how you call the class as well. And this is on the parent class and you call a child class?

And I assume some other code sets the bearer_token in the session as this code does not?

laksh's avatar
Level 1

@Sinnbeck this is the function which generating token everytime

public function myBids(Request $request) {

    $bank_accounts = session('auth-user')['account_holder']->bankaccounts;
    $data['mode'] = 'mine';
    $data['accountholder_no'] = session('auth-user')['account_holder']->accountholder_no;
    $data['buying_currency'] = $request->buying_currency;
    $data['selling_currency'] = $request->selling_currency;
    $data['status'] = $request->status;
    $data['marketplace_no'] = $request->search_input;
    $data['limit'] = 10;
    $data['mode'] = 'mine';
    
    $limit = $data['limit'];
    
    if ($request->ajax() && $request->has('page')) {
        $data['page'] = $request->page;
    } else {
        $data['page'] = 1;
    }
    $postParams = array(
        'operation' => 'GetMarketPlace',
        
        'element' => json_encode($data)
    );
    // dd($postParams);
    $response = $this->postHttpRequest($this->ws_url, $postParams);
    $response = json_decode($response);
    
    dd($response);
    if ($request->ajax()) {
        if ($response && $response->success == true) {
            if ($response->result->success == true) {
                $data = $response->result->data;
                return response()->json([
                    'status' => 'success',
                    'msg' => 'Data fetched Successfully',
                    'limit' => $limit,
                    'count' => count($data),
                    'view' => view('dashboard.more-content.more-bids')->with(['data' => $data])->render()
                ]);
            } elseif ($response->result->success == false) {
                $result = array('status' => 'error', 'msg' => $response->message, 'data' => null);
                return response()->json($result);
            }
        } else {
            $result = array('status' => 'error', 'msg' => 'something went wrong', 'data' => null);
            return response()->json($result);
        }
    } else {
        if ($response && $response->success == true) {
            if ($response->result->success == true) {
                $data = $response->result->data;
                return view('dashboard.my-bids', compact('data','bank_accounts','limit'));
            } elseif ($response->result->success == false) {
                return redirect()->back()->with('error', $response->result->message);
            }
        } else {
            return redirect()->back()->with('error', 'Server Error');
        }
    }
    return null;
}
Sinnbeck's avatar

@laksh And where do you call that constructor (new up the class) ?

And you still dont set bearer_token in the session?

laksh's avatar
Level 1

@Sinnbeck no in this i doesn't call the constructor but it automatically generating token from constructor i don't know what's reason behind that

laksh's avatar
Level 1

and these both were in same class constructor and function

Sinnbeck's avatar

@laksh Sorry I am not following. But the problem is that it hits the else here?

if(Session()->has('bearer_token')){
        return null;
    }else{
        $this->generateToken($data);
    }

Except you never actually add bearer_token to the session?

laksh's avatar
Level 1

@Sinnbeck yeah it hits else everytime i want that if bearer token is present it doesn't regenerate the token and when the token is expires it automatically regenerate the new token

laksh's avatar
Level 1

@Sinnbeck the token will generate and add to session through this function here's the code for that function

public function generateToken($accountholder_no)

{ $sessionName = '';

    $loginObj = $this->crmLogin($this->ws_url,$this->username,$this->accesskey);


    if($loginObj->sessionName){
        $sessionName = $loginObj->sessionName;
        session()->put('sessionName',$sessionName);
    }

    $data['accountholder_no'] = $accountholder_no;

    $postParams = array(
        'operation' => 'GenerateToken',
        'sessionName' => $sessionName,
        'element' => json_encode($data)
    );
    
    $token_response = $this->postHttpRequest($this->ws_url, $postParams);
    $token_response = json_decode($token_response);
    
    print_r($token_response);
    
    if ($token_response && $token_response->success) {
        if ($token_response->result && $token_response->result->success) {
            $token = $token_response->result->token;
            session()->put('bearer_token', $token);
            return $token;
        } else {
            return null;
        }
    } else {
        return null;
    }
}
Sinnbeck's avatar

@laksh Check if it is actually set in the session. Does the code ever reach it?

 if ($token_response && $token_response->success) {
        if ($token_response->result && $token_response->result->success) {
            $token = $token_response->result->token;
            dd('SESSION SET');
            session()->put('bearer_token', $token);
            return $token;
        } else {
            return null;
        }
    } else {
        return null;
    }
Sinnbeck's avatar

Also make sure that the actual code does not use dd() anywhere. That will terminate the request before session is saved if I recall correctly.

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@laksh Then you need to debug this part (fixed the case of the helper as its session() not Session())

dd(session()->all());
if(session()->has('bearer_token')){

Please or to participate in this conversation.