adwairi
adwairi

Reputation: 191

laravel, How to route from controller to another resource controller using request object

I have two controllers (Workflow and Stage), I need to route to Stage.store function from the Workflow.store function using a request object I have been created :

Workflow controller:

public function store(WorkflowRequest $request)
{
    $oWorkflow = new WfWorkflow();
    $oWorkflow->name = $request->get('workflow_name');
    if($oWorkflow->save()){
        $aStages = $request->get('wf_stage');
        $params = [
            '_token' => $request->get('_token'),
            'wf_id' => $oWorkflow->id,
            'stages' => $aStages
        ];
        $oStageRequestObject = Request::create(url('stage'), 'POST', $params);
    }
}

Now, how can i use the Request object $oStageRequestObject to route to stage.store using POST method ?

Upvotes: 1

Views: 76

Answers (2)

Peter Reshetin
Peter Reshetin

Reputation: 925

If you want to reuse stage.store logic, then I would suggest to create StageService service class that handles this logic. So StageController would look like this:

public function store(Requests\StoreStageRequest $request, StageService $stageService)
{
    $stageService->handleRequest($request); // put controller logic to this function

    return redirect('/somewere');
}

And then in WorkflowController you may manually create StoreStageRequest request and pass it to handleRequest():

public function store(WorkflowRequest $request, StageService $stageService)
{
    // ...

    $params = [
        '_token' => $request->get('_token'),
        'wf_id' => $oWorkflow->id,
        'stages' => $aStages
    ];

    $oStageRequest = new Requests\StoreStageRequest($params);

    $stageService->handleRequest($oStageRequest);

    // ...
}

As for validation, think the only way is to manually create Validator, and pass to it data and rules.

Hope this helps.

Upvotes: 1

Dhaval
Dhaval

Reputation: 1436

$params = [
        '_token' => $request->get('_token'),
        'wf_id' => $oWorkflow->id,
        'stages' => $aStages
    ];

You want to send this $params variable to route to stage.store using POST method .

For that you can set session .

For ex.

In Workflow.store method set session like this.

Session(['param' => $params]);

Now you can access this session in stage.store method using POST method .

Upvotes: 2

Related Questions