Chandler P.
Chandler P.

Reputation: 1

add $request parameter to wordpress api callback like laravel

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.

Upvotes: 0

Views: 200

Answers (1)

janice macbeth
janice macbeth

Reputation: 1

try this function registerRouteCallback ($request) { }

Upvotes: 0

Related Questions