0

As you know you can create a new Request in laravel and pass it to controller methods, like so:

public function create(CreateNewBookRequest $request){
    //todo method logic
}

now I want to implement such a thing in wordpress REST API. As you know when we are registring a new route for wordpress REST API, we do this:

register_rest_route($this->namespace, $this->baseRoute . '/register', array(
    'methods'  => [WP_REST_Server::CREATABLE],
    'callback' => [$this, 'registerRouteCallback'],
));

and in registerRouteCallback method we have:

public function registerRouteCallback(WP_REST_Request $request)
{
    //todo method logic
}

Now how can I implement something like laravel $request in wordpress(specifaclly for validations). I want to use this approach in multiple places not just in REST API callback.

generally I'm looking for a way to have access to a class object inside a function without passing the object as a parameter, I want function to automatically create an object if it was not passed to it.

When I try to do this:

public function registerRouteCallback(RegisterRequest $request)
{
    //todo method logic
}

I'll get a fetal error:

Argument #1 ($request) must be of type RegisterRequest, WP_REST_Request given.
  • 1
    This is a project on itself, not a question for SO. – N69S Dec 28 '22 at 15:54
  • @N69S I wasn't looking for an easy answer. I know that this concept is too big I just needed a road map. So I did my research and the answer is Dependency Injection(you can look for PSR-11 to get the idea). there are multiple types of dependency injection for EX: setter injection, method injection, and constructor injection. link to a YouTube video: https://www.youtube.com/watch?v=igx3bIl1T_c&list=PLr3d3QYzkw2xabQRUpcZ_IBk9W50M9pe-&index=72&ab_channel=ProgramWithGio – Chandler P. Jan 03 '23 at 16:01
  • It's not really about the subject of the dependency injection that you are looking for, but more like the "container" concept that **solves** dependency injections here [some reading](https://medium.com/@sawomirkowalski/dependency-injection-containers-c4c1b7dd5e89) – N69S Jan 03 '23 at 16:29
  • In all cases you question falls in the 3rd case of off-topic question in SO see [On Topic question](https://stackoverflow.com/help/on-topic) `(3)-Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.` – N69S Jan 03 '23 at 16:34

1 Answers1

0

try this function registerRouteCallback ($request) { }