Laravel routing: how to pass parameter/arguments from one controller to controller

断了今生、忘了曾经 提交于 2019-12-11 11:47:32

问题


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.

Controller 1:

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

Controller 2:

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

Error:

Missing argument 1 for Controller2::getData() 

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


回答1:


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.

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



来源:https://stackoverflow.com/questions/28059775/laravel-routing-how-to-pass-parameter-arguments-from-one-controller-to-controll

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!