Jochen
Jochen

Reputation: 1776

Routing with Zend Framework 2 Restful Webservice

I want to implement a RESTful webservice by using Zend Framework 2, more precisely 2.1.5. I got a 404 if I visit http://ehcserver.localhost/rest, the corresponding message is 'rest(resolves to invalid controller class or alias: rest)'. What went wrong?

You can see my source code in my github-repository: https://github.com/Jochen1980/EhcServer/blob/master/module/Application/config/module.config.php

The route is defined like this:

return array(
    'router' => array(
        'routes' => array(
            'rest' => array(
                'type' => 'ZendMvcRouterHttpSegment',
                'options' => array(
                'route' => '/:controller[.:formatter][/:id]',
                'constraints' => array(
                    'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'formatter' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'id' => '[a-zA-Z0-9_-]*'
                ),
            ),
         ),
         'home' => array(
         ...

Upvotes: 0

Views: 217

Answers (2)

Andrew
Andrew

Reputation: 12809

Are you sure the type is valid?

type' => 'ZendMvcRouterHttpSegment',

to this

type' => 'Segment',

Upvotes: 0

Crisp
Crisp

Reputation: 11447

Your route doesn't define a namespace to which the controller belongs, you need to add a __NAMESPACE__ to route defaults

        'rest' => array(
            'type' => 'ZendMvcRouterHttpSegment',
            'options' => array(
                'route' => '/:controller[.:formatter][/:id]',
                'defaults' => array(
                    // tell the router which namespace :controller belongs to
                    '__NAMESPACE__' => 'Application\Controller',
                ),
                'constraints' => array(
                    'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'formatter' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'id' => '[a-zA-Z0-9_-]*'
                ),
            ),
        ),

Upvotes: 2

Related Questions