0

I've been trying to figure this out for the last day or so to no avail.

I'm trying to create an endpoint to gather data from a set of tables. I'm trying to write it in OOP because I need to learn.

here's my class:

         class b2_items_rest_route extends WP_REST_Controller{
         private $wpdb;

         public function __construct(){
             global $wpdb;
             $this->wpdb = $wpdb;
         }

         public function b2_register_route(){
             $version = '1';
             $namespace = 'b2rest/v' . $version;
             $base = 'route';

             register_rest_route( $namespace, '/' . $base,
                array(
                    array(
                        'methods'             => WP_REST_Server::READABLE,
                        'callback'            => array($this, 'items_by_id'),
                        'permission_callback' => array($this, 'get_items_permissions_check' ),
                        'args'                => array(),
                    ),
                )
            );

            register_rest_route( $namespace, '/' . $base . '/trees/(?P<id>[\d]+)',
                array(
                    array(
                        'methods'             => WP_REST_Server::READABLE,
                        'callback'            => array($this, 'items_by_id'),
                        'permission_callback' => array($this, 'get_item_permissions_check' ),
                        'args'                => array(),
                    )
                )
            );
         }

         function items_by_id($request){
             global $wpdb;
             //$tree_id = $request->get_param('id');
             $tree = $wpdb->get_results("SELECT * FROM bb_bower__tree_configuration WHERE tree_id=47");
         }

         public function get_items_permissions_check($request){
             return true; // make endpoint available for everyone to read.
         }

     }

     $b2_items_rest_route = new b2_items_rest_route;
     add_action('rest_api_init', $b2_items_rest_route->b2_register_route);

the problem is that I keep getting rest_no_route in postman but when I re-write it regularly it works fine.

any direction would be greatly appreciated, thanks!

1
  • Did you flush permalinks after setting up your endpoints? I noticed your add_action call doesn't pass a valid callable object?
    – Tom J Nowell
    Commented Mar 6, 2018 at 14:57

1 Answer 1

3

This isn't valid:

add_action('rest_api_init', $b2_items_rest_route->b2_register_route);

By all rights this should generate either a warning or a fatal error

add_action takes a PHP callable as a second parameter, e.g.:

  • 'function_name'
  • function( $im_a_closure_yay) {}
  • array( 'class_name', 'static_method' )
  • array( $object, 'method' )
1
  • Yes, turns out that was wrong, I ended up re-writing the whole class to something that made more sense
    – GeneralCan
    Commented Mar 7, 2018 at 13:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.