user3851557
user3851557

Reputation: 91

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

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 ?

Upvotes: 1

Views: 966

Answers (1)

ryanwinchester
ryanwinchester

Reputation: 12137

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

Upvotes: 1

Related Questions