简体   繁体   English

在 laravel 中的两个不同控制器之间传递数据

[英]Pass Data between two different controllers in laravel

I am building an application in Laravel.我正在 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,我有一个 controller SellerRegisterController,它有一个方法 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).上面的 veirfySeller 方法适用于一些逻辑并创建一个变量 ID,我想将该变量 ID 数据传递给不同 controller (sellerDetailsController) 中的另一个方法 (submitSellerDetails)。

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.如果我是你,我会在同一个 controller 中定义两种方法verifySeller()submitSellerDetails()

Your controller will look like this:您的 controller 将如下所示:

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:如果您不想传递$request变量而是传递其他变量,则可以执行以下操作:

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.您可以在 controller 中定义一个变量,在verifySeller()方法中为其分配您想要的任何数据,然后调用submitSellerDetails()方法并在其中使用该变量。

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.希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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