WordPress.org

Make WordPress Core

Opened 9 months ago

Closed 9 months ago

Last modified 9 months ago

#50717 closed defect (bug) (invalid)

rest_no_routes with PHP 7.3

Reported by: yozana Owned by:
Milestone: Priority: normal
Severity: normal Version: 5.5
Component: REST API Keywords:
Focuses: Cc:

Description (last modified by SergeyBiryukov)

When calling an endroute with PHP 7.3.x i get the following no_route error.
route example (job is a custom post type)
http://localhost/libi-smart-backoffice/wp-json/api/v2/job/334
results with:

{
    "code": "rest_no_route",
    "message": "No route was found matching the URL and request method",
    "data": {
        "status": 404
    }
}

The same call works well with PHP 7.1.9.
PHP error log shows:
PHP Warning: preg_match(): Compilation failed: subpattern name expected at offset 24 in C:\wamp64\www\libi-smart-backoffice\wp-includes\rest-api\class-wp-rest-server.php on line 897

Change History (4)

#1 @yozana
9 months ago

Sorry: missed the register route:

register_rest_route('api/v2', '/job/(?P<>[0-9]+)', [
                'methods' => WP_REST_Server::READABLE,
                'callback' => array( $this, 'get_job'),                
                //'permission_callback' => array( $this, 'get_items_permissions_check' ),                  
                'permission_callback' => array( $this, 'get_permissions_worker_or_candidate' ),                 
                'args' => $this->get_collection_params(),
            ]);   
Last edited 9 months ago by SergeyBiryukov (previous) (diff)

#2 follow-up: @swissspidy
9 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

The group structure in your regex for the route is invalid.

Try (?P<id>[0-9]+) instead of (?P<>[0-9]+). Or even simpler: (?P<id>[\d]+)

Check how WP_REST_Posts_Controller registers the routes, and verify your regex using something like https://regex101.com/

#3 @SergeyBiryukov
9 months ago

  • Description modified (diff)

#4 in reply to: ↑ 2 @yozana
9 months ago

Replying to swissspidy:

The group structure in your regex for the route is invalid.

Try (?P<id>[0-9]+) instead of (?P<>[0-9]+). Or even simpler: (?P<id>[\d]+)

Check how WP_REST_Posts_Controller registers the routes, and verify your regex using something like https://regex101.com/

Thank you!
this has indeed solved the problem.

Note: See TracTickets for help on using tickets.