zf-fr / zfr-rest

A module for Zend Framework 2 that aims to simplify RESTful

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Attempting to follow the Quickstart but receiving the following error

chrismatta opened this issue · comments

Hi, this looks like a very promising project and I'd like to get a minimal configuration working so I could check it out, hopefully this is the correct forum for this.

I'm following the quick start guide and when I attempt to get my collection of objects with the url /api/v1.1/projects I get the following error:

Fatal error: Call to a member function getOutsideClassMetadata() on a non-object in /opt/ssp/apps/www/html/chris/ssp2/vendor/zfr/zfr-rest/src/ZfrRest/Mvc/Router/Http/ResourceGraphRoute.php on line 318

I checked into the ResourceGraphRoute.php and confirmed that $this->resource is in fact an object of type Doctrine\ORM\EntityRepository, and the $resource->getClassName() returns the correct class name, but it seems that $this->metadataFactory->getMetadataForClass($resource->getClassName()); is returning NULL.

Any ideas about where I could look to resolve this?

Hi,

Please can you show me your route configuration? :)

Btw: ZfrRest is still immature, I would not recommend it using it in production yet, mostly because two bugs need to be fixed in Doctrine first (I've made a PR and hopefully this will be merged in Doctrine 2.5). However I'd LOVE people to play with ZfrRest, as I lack a lot of feedback to make this project better :(.

The relevant route entry is called project.v.1.1

namespace Api;

return array(
     'controllers' => array(
         'invokables' => array(
             'Api\V1\Controller\Api' => 'Api\V1\Controller\IndexController',
             'Api\V1\Controller\Project' => 'Api\V1\Controller\ProjectController',
             'Api\V1\Controller\Array' => 'Api\V1\Controller\ArrayController',
             'Api\V1_1\Controller\ProjectController' => 'Api\V1_1\Controller\ProjectController',
             'Api\V1_1\Controller\ProjectsController' => 'Api\V1_1\Controller\ProjectsController'
         ),
     ),
     'router' => array(
        'routes' => array(
            'index' => array(
                'type' => 'literal',
                'options' => array(
                    'route' => '/api/v1',
                    'defaults' => array(
                        'controller' => 'Api\V1\Controller\Api',
                        'action'     => 'index',
                    ),
                ),
                'may_terminate' => true,
            ),
            'project' => array(
                'type' => 'segment',
                'options' => array(
                    'route' => '/api/v1/project[/:id]',
                    'constraints' => array(
                        'id' => '[0-9]*'
                    ),
                    'defaults' => array(
                        'controller' => 'Api\V1\Controller\Project'
                    )
                ),
                'may_terminate' => true,
            ),
            'project.v1.1' => array(
                'type' => 'ResourceGraphRoute',
                'options' => array(
                    'route' => '/api/v1.1/projects',
                    'resource' => 'ProjectObjectRepository'
                )
            ),
            'array' => array(
                'type' => 'segment',
                'options' => array(
                    'route' => '/api/v1/array[/:id]',
                    'constraints' => array(
                        'id' => '[0-9]*'
                    ),
                    'defaults' => array(
                        'controller' => 'Api\V1\Controller\Array'
                    )
                ),
                'may_terminate' => true,
            )
        )
     ),
     'view_manager' => array(
        'template_path_stack' => array(
            'api' => __DIR__ . '/../view',
        ),
         'strategies' => array(
            'ViewJsonStrategy',
         ),
     ),
     // set up doctrine ORM
    'doctrine' => array(
        'driver' => array(
            __NAMESPACE__ . '_driver' => array(
                'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
                'cache' => 'array',
                'paths' => array(
                    __DIR__ . '/../src/'. __NAMESPACE__ . '/V1/Entity',
                    __DIR__ . '/../src/'. __NAMESPACE__ . '/V1_1/Entity'
                )
            ),
            'orm_default' => array(
                'drivers' => array(
                    __NAMESPACE__ . '\V1\Entity' => __NAMESPACE__ . '_driver',
                    __NAMESPACE__ . '\V1_1\Entity' => __NAMESPACE__ . '_driver'
                )
            )
        )
    )
 );

and I've registered the object repository in the Module.php:

public function getServiceConfig() {
        return array(
            'factories' => array(
                'ProjectObjectRepository' => function ($sm) {
                    $objectManager = $sm->get('doctrine.entitymanager.orm_default');
                    return $objectManager->getRepository('Api\V1_1\Entity\PrjProject');
                }
            )
        );
    }

Actually this won't work because of the path:

'route' => '/api/v1.1/projects',
'resource' => 'ProjectObjectRepository'

In order to provide its routing capability, ZfrRest parses the URI to automatically get resources. For instance, if you have an URL "/projects/1/folders", it will automatically be able to get all folders from projects 1, without having to specify routes for folders.

However in your case you specified "/api/v1.1/projects". Therefore, ZfrRest router thinks "/api" is the beginning of the traversal, and try to match "v1.1" as the identifier of a project.

For ZfrRest to work, you must create a base Literal route whose route is "/api/v1.1", and then put all your ZfrRest route as children.

Something like that:

'router' => array(
        'routes' => array(
            'api' => array(
                'type' => 'literal',
                'options' => array(
                    'route' => '/api/v1.1',
                ),
                'may_terminate' => false,
            ),
            'child_routes' => array(
            'projects' => array(
                'type' => 'ResourceGraphRoute',
                'options' => array(
                    'route' => '/projects',
                    'resource' => 'ProjectObjectRepository'
                )
            ),
        )
     ),

I assume you mean like this?

'api.v1.1' => array(
    'type' => 'literal',
    'options' => array(
        'route' => '/api/v1.1'
    ),
    'may_terminate' => false,
    'child_routes' => array(
        'projects.v1.1' => array(
            'type' => 'ResourceGraphRoute',
            'options' => array(
                'route' => '/projects',
                'resource' => 'ProjectObjectRepository'
            )
        ),
    )
),

Unfortunately that's still not working, same exact error. I appreciate the help.

Strange. Looks correct. Can you try to isolate and remove all other routes, only leaving the zfrrest one?

Yea, only left that one and I am getting the same issue.

Grrr... Even if you remove the parent and only let zfrrest route? That would be very surprising :(.

Envoyé de mon iPhone

Le 14 janv. 2014 à 23:57, chrismatta notifications@github.com a écrit :

Yea, only left that one and I am getting the same issue.


Reply to this email directly or view it on GitHub.

Here's the whole routes entry now:

 'router' => array(
        'routes' => array(
            'projects' => array(
                'type' => 'ResourceGraphRoute',
                'options' => array(
                    'route' => '/projects',
                    'resource' => 'ProjectObjectRepository'
                )
            )
        )
     ),

And it's STILL giving the exact same error:

Fatal error: Call to a member function getOutsideClassMetadata() on a non-object in /opt/ssp/apps/www/html/chris/ssp2/vendor/zfr/zfr-rest/src/ZfrRest/Mvc/Router/Http/ResourceGraphRoute.php on line 318

...very strange.

Indeed strange because I used zfrrest today and it worked.

Haaaaaa wait. Are you sure you are using dev-master? I suspect from the error that you are using the only tagged version but this is super old. Actually the doc only applies to dev-master.

I suspect I will do a few major BC in the next weeks so using dev-master is not really stable but you can use a specific hash commit.

However as of tonight, use dev master and it should work :)

Yea, I'm using the tagged release (0.0.2 I think) I'll attempt to run this with the dev-master branch and let you know.

Should be fixed. There were a lot of new versions.