Home > Software design >  How to create a new request
How to create a new request

Time:11-18

I have a method that accepts a request

public function createUser(Request $request)
{
    ...
}

I want to call it from another method

public function someMethod(){
    $array = [...];

    return $this->createUser($array); <----
}

and how can I pass a new request to it with the array I need?

CodePudding user response:

How about instead of trying to call a controller method, you move the logic to create a user to a service class and then use the service class in your createUser and someMethod methods?

UserService.php

class UserService
{

    public function __construct() { }

    public function createUser(array $userData)
    {
        // TODO use $userData to create a user here

        return $newUser;
    }
}

SomeController.php

public function createUser(Request $request)
{
    $this->userService->createUser($request->all());
}


public function someMethod(){
    $array = [...];

    return $this->userService->createUser($array);
}
  • Related