0

I would really appreciate help on this, as i have tried and tried.

I have made this very simple endpoint to som basic info from a custom post type called 'ydelse'.

Here is my class for the creation of the custom rest endpoint

class jw_salonbooking_ydelseliste_REST_Controller extends WP_REST_Controller { 

    public function register_routes() {
        $version = '1';
        $namespace = 'jw_salonbooking/v' . $version;

        $base = '/ydelseliste/';
        register_rest_route( $namespace + $base, array(
            'methods' => 'GET',
            'callback' => array ($this, 'my_awesome_func'),
        ) );
    }


    public function register_hook() {
        add_action ( 'rest_api_init', array( $this, 'register_routes' )     );
    }

    public function my_awesome_func( $data ) {
        $posts = get_posts( array(
            'type' => 'ydelse',
        ) );

        if ( empty( $posts ) ) {
            return null;
        }

        $ydelseliste = array();
        $counter=0;

        foreach ($posts as $post) {
            $ydelseliste[$counter]['titel']       = $post->title;
            $ydelseliste[$counter]['pris']        = $post->jw_salon_booking_ydelse_pris;
            $ydelseliste[$counter]['varighed']    = $post->jw_salon_booking_varighed;
            $ydelseliste[$counter]['beskrivelse'] = $post->content;        
            $counter++;
        }

    return $ydelseliste;
    }
}

I then do this, in my constructor of my plugin.:

     require_once (dirname( __FILE__ ) . '/rest-routes/jw-salonbooking-ydelseliste-rest-controller.php');
     //* Registrer ydelseliste REST controller 
     $ydelseliste_route = new jw_salonbooking_ydelseliste_REST_Controller();
     $ydelseliste_route->register_hook();

I know my constructor is working because all the other add_actions are executing just fine. And while I was developing the route class I was getting errors from Postman when i was executing af GET request like this:

    https://zebrafinken.dk/testsite/wp-json/jw_salonbooking/v1/ydelseliste/

But now i am just getting this result:

{
    "code": "rest_no_route",
    "message": "Ingen rute blev fundet der matchede URL og anmodningen",
    "data": {
        "status": 404
    }
}

I hope there's someone who will guide me in the right direction or have an answer to this.

3
  • 2
    Is that $namespace + $base just a typo in the question?
    – Sally CJ
    Nov 22, 2018 at 14:34
  • No is there a type? Nov 23, 2018 at 16:31
  • 1
    If it's not, then the + should be a . (dot) - $namespace . $base
    – Sally CJ
    Nov 23, 2018 at 16:39

1 Answer 1

0

It was a simple typo as posted by @Sally CJ.

Instead of

$namespace + $base

Use

$namespace . $base

Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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