Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Why it returns empty in variable X

class ProduitController extends Controller
{
  private $X ;

  public function uploaderFils(){
      .
      .
      .
       $this->X=$imageName;
  }

  public function AddProduct(Request $request){
      . . .
    return   ("Image Name". $this->X);
  }
}


What I have tried:

Why it returns empty in variable X
Posted
Updated 29-Aug-22 7:27am

1 solution

I don't know Laravel at all, but in other Controller-based frameworks, a controller will only exist for the current request and then it dies afterwards, taking any variables with it.

So, in your example, if a client calls uploaderFils, the controller instance will die after that call ends, taking the value of $X with it. The client call to AddProduct gets a new controller instance with nothing in it, and $X will be empty.

You seem to be thinking in terms of a normal desktop application, not a web application. A web application is stateless, and works in a request->response paradigm. Once the response is sent back to the client, the controller no longer exists, and a new controller is created upon a new request.

You're also thinking in terms of a single-user application. Web apps are multi-user, so setting a "global" variable can be overwritten by a new user request.

If you want to save state between requests for individual users, you have to save that state information somewhere else, like a database, or cookies if the state is small enough. Laravel should have some mechanism to save state somewhere.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900