"The requested URL could not be Matched by routing" error using ZF2

Asked

Viewed 364 times

0

I am using apigility (ZF2). The following error occurs when submitting a request: The requested URL could not be Matched by routing. The error occurs when I try to make a request get through the link: http://0.0.0.0:8888/leads using Postman (Chrome plugin).

Supposedly the error refers to some route error. However, you cannot notice this error yet.

The directory structure follows below:

The code of my chat/module.php file is as follows:

<?php
   namespace chat;

   use chat\V1\Rest\Leads\LeadsEntity;
   use chat\V1\Rest\Leads\LeadsMapper;
   use Zend\Db\ResultSet\ResultSet;
   use Zend\Db\TableGateway\TableGateway;

   use ZF\Apigility\Provider\ApigilityProviderInterface;

 class Module implements ApigilityProviderInterface
 {
   public function getConfig()
   {



      return array(
        'factories' => array(
            'chatLeadsTableGateway' =>  function($sm) {
                $dbAdapter = $sm->get('Adaptador');
                $resultSetPrototype = new ResultSet();
                $resultSetPrototype->setArrayObjectPrototype(new LeadsEntity());
                return new TableGateway('nomedatable', $dbAdapter, null, $resultSetPrototype);
            },
            'chat\V1\Rest\Leads\LeadsMapper' => function($sm) {
                $tableGateway = $sm->get('chatLeadsTableGateway');
                return new LeadsMapper($tableGateway);
            }
        ),
     );

   }

   public function getAutoloaderConfig()
   {
    return array(
        'ZF\Apigility\Autoloader' => array(
            'namespaces' => array(
                __NAMESPACE__ => __DIR__,
            ),
        ),
    );
  }
 }

The code of the chat/module.config.php file is as follows::

<?php
   return array(
      'router' => array(
         'routes' => array(
            'chat.rest.leads' => array(
              'type' => 'Segment',
                 'options' => array(
                    'route' => '/leads[/:leads_id]',
                       'defaults' => array(
                          'controller' =>          'chat\\V1\\Rest\\Leads\\Controller',
                ),
            ),
        ),
    ),
), 

Below project folder structure

inserir a descrição da imagem aqui

1 answer

0

The module.php file is wrong. The getConfig method is not taking the path configuration that is set in module.config.php. For that change this method to :

public function getConfig()
{
    include __DIR__ . '/../../config/module.config.php';
}

The previous content of this method should actually be within getServiceConfig(), as dried below:

public function getServiceConfig()
 {
    return array(
        'factories' => array(
            'chatLeadsTableGateway' =>  function($sm) {
                $dbAdapter = $sm->get('Adaptador');
                $resultSetPrototype = new ResultSet();
                $resultSetPrototype->setArrayObjectPrototype(new LeadsEntity());
                return new TableGateway('nomedatable', $dbAdapter, null, $resultSetPrototype);
            },
            'chat\V1\Rest\Leads\LeadsMapper' => function($sm) {
                $tableGateway = $sm->get('chatLeadsTableGateway');
                return new LeadsMapper($tableGateway);
            }
        ),
    );
}

This will solve the route problem.

  • Don’t forget to hit the Return in getConfig()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.