Laravel路由:如何将参数/参数从一个控制器传递到控制器

Laravel路由:如何将参数/参数从一个控制器传递到控制器

问题描述:

我有两个控制器,一个控制器呼叫另一个.我完成了对控制器1的一些处理,并希望将数据传递给控制器​​2进行进一步处理.

I have two controller and one controller calls another. I finish some processing on Controller 1 and want to pass the data to controller 2 for further processing.

控制器1:

public function processData() 
  {
    $paymenttypeid = "123";
    $transid = "124";
    return Redirect::action('Controller2@getData');
  }

控制器2:

public function getData($paymenttypeid, $transid ) 
{
}

错误:

Missing argument 1 for Controller2::getData() 

如何将参数从控制器1传递给控制器​​2?

How do I pass arguments from Controller 1 to Controller 2 ?

那确实不是一个好方法.

That is really not a good way to do it.

但是,如果您确实要重定向和传递数据,则这样的话会更容易:

However, if you really want to redirect and pass data it would be easier if they were like this:

<?php

class TestController extends BaseController {

    public function test1()
    {
        $one = 'This is the first variable.';
        $two = 'This is the second variable';

        return Redirect::action('TestController@test2', compact('one', 'two'));
    }

    public function test2()
    {
        $one = Request::get('one');
        $two = Request::get('two');

        dd([$one, $two]);
    }

}

请注意,我在控制器方法中不需要参数.

note that I am not requiring arguments in the controller method.

有很多方法可以给猫剥皮,而您尝试做的并不是一个好方法.我建议先看看如何使用服务对象,如此视频中所述,还有无数的教程.

There are many ways to skin the cat, and what you are trying to do is not a good one. I'd recommend starting off by looking at how to use service objects instead, as described in this video, and countless tutorials.

如果您不小心,很快就会使您的控制器变得笨拙.更糟糕的是,当您想从另一个控制器调用一个控制器方法时会发生什么? kes!一种解决方案是使用服务对象,将我们的域与HTTP层隔离.

If you're not careful, very quickly, your controllers can become unwieldy. Worse, what happens when you want to call a controller method from another controller? Yikes! One solution is to use service objects, to isolate our domain from the HTTP layer.

-Jeffrey Way

-Jeffrey Way