简体   繁体   中英

Pass Data between two different controllers in laravel

I am building an application in Laravel. I need to pass some data between two methods in two different controllers. Let me explain You,

I have a controller sellerRegisterController which has a method verifySeller,

class sellerRegisterController extends Controller
{

public function verifySeller()
{
//some logic here

}

}

The above veirfySeller method works on some logic and create a varible ID, I want to pass that varible ID data to another method (submitSellerDetails) in different controller (sellerDetailsController).

class sellerDetailsContorller extends Controller
    {

    public function submitSellerDetails()
    {
    //some logic here
    }

    }

In a nutshell I want to know how can I pass data/variable from one method to another in different controllers? Thanks

If I were you, I would define both methods verifySeller() and submitSellerDetails() in the same controller.

Your controller will look like this:

class sellerRegisterController extends Controller
{

    public function verifySeller( Request $request )
    {
         //some logic here

         // passing data to another method in the same controller
         $this->submitSellerDetails( $request );
    }

    public function submitSellerDetails( Request $request )
    {
      //some logic here
    }

}

If you don't want to pass $request variable but some other variable instead, you can do something like this:

class sellerRegisterController extends Controller
{

    public function verifySeller( Request $request )
    {
         //some logic here

         // passing data to another method in the same controller
         $this->submitSellerDetails( $variableContainingYourData );
    }

    public function submitSellerDetails( $someArgument )
    {
      //some logic here
    }
}

There's another way to handle it. You can define a variable in the in your controller, assign it whatever data you want inside verifySeller() method and then call submitSellerDetails() method and use that variable inside it.

Update:

class sellerRegisterController extends Controller
{
    private $someVariable;

    public function verifySeller( Request $request )
    {
         //some logic here

         // assign data which you want to access inside submitSellerDetails()
         $this->someVariable = 'some data';

         // calling submitSellerDetails() method
         $this->submitSellerDetails();
    }

    public function submitSellerDetails()
    {
      //some logic here

      //do whatever you want with $this->someVariable;
    }
}

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM