mark winkle
mark winkle

Reputation: 206

zf2 using abstract restful controller

How to call the get() or getList() method, but i always get the get() method running what i have done wrong. I have make action to null in the child routes of the users in appointments.

module.config.php

    <?php
return array(
    'controllers' => array(
        'invokables' => array(
            'Users\Controller\Users' => 'Users\Controller\UsersController',
            'Users\Controller\Appointments' => 'Users\Controller\AppointmentsController',
        ),
    ),

    'router' => array(
        'routes' => array(

          //By default profile action is loaded
          'users' => array(
                'type'    => 'segment',
                'options' => array(
                    'route'    => '/api/v1/users[/:id]',
                    'defaults' => array(
                        'controller' => 'Users\Controller\Users',
                        'action' => 'profile',
                    ),
                ),
                'may_terminate' => true,

                'child_routes' => array(


                    //apointments action
                    'appointments' => array(
                      'type' => 'segment',
                      'options' => array(
                         'route' => '/appointments[/:apt_id]',
                         'constraints' => array(
                            'apt_id' => '[0-9]+',
                         ),
                         'defaults' => array(
                           'controller' => 'Users\Controller\Appointments',
                           'action' => null
                         ),
                       ),
                    ),




                ),



            ),

        ),
    ),

    'view_manager'  => array(
        'strategies' => array(
             'ViewJsonStrategy',
             ),
        //'template_path_stack' => array(
        //  'Register'  => __DIR__ . '/../view',
        //),

    ),



);

Appointments Controller:

    <?php
namespace Users\Controller;

use Zend\Validator\AbstractValidator;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\Mvc\Controller\AbstractRestfulController;
use Zend\View\Model\ViewModel;
use Users\Model\UsersTable;
use Zend\Db\Sql\Select;
use Zend\Db\ResultSet\ResultSet;
// use Zend\Debug\Debug;
use Zend\View\Model\JsonModel;

use Zend\Validator\Db\RecordExists;
use Zend\Http\Client as HttpClient;
use Users\Service\UsersService;

class AppointmentsController extends AbstractRestfulController
{


    public function getList() {

       echo 'getList Method'; 
    }


    public function get() {

       echo 'get Method';
    }



}

Upvotes: 0

Views: 1499

Answers (3)

mark winkle
mark winkle

Reputation: 206

Finally I got the answer,

Actually when the url says like /api/v1/users/id it points to the get method, but when the endpoints has /api/v1/users it points to the getList() method.

The key here is the id parameter, as suggested when we change the id to user_id, we wont get the $id value in the controller.

now my route looks likes this,

<?php
 return array(
  'controllers' => array(
     'invokables' => array(
        'Users\Controller\Users' => 'Users\Controller\UsersController',
        'Users\Controller\Appointments' => 'Users\Controller\AppointmentsController',
        'Users\Controller\Vitalrecords' => 'Users\Controller\VitalrecordsController',
    ),
),

'router' => array(
    'routes' => array(

      //By default profile action is loaded
        'users' => array(
            'type'    => 'segment',
            'options' => array(
                'route'    => '/api/v1/users[/:id]',
                 'constraints' => array(
                        'id' => '[0-9]+',
                     ),
                'defaults' => array(
                    'controller' => 'Users\Controller\Users',
                   'action' => null,
                ),
            ),
            'may_terminate' => true,
        ),


        'appointments' => array(
            'type'    => 'segment',
            'options' => array(
                'route'    => '/api/v1/users/:user_id/appointments[/:id]',
                'constraints' => array(
                    'id'     => '[0-9]+',
                    'user_id' => '[0-9]+',
                ),
                'defaults' => array(
                    'controller' => 'Users\Controller\Appointments',
                ),
            ),
            'may_terminate' => true,

        ),





    ),
),

'view_manager'  => array(
    'strategies' => array(
         'ViewJsonStrategy',
         ),
),

);

Upvotes: 1

user3929758
user3929758

Reputation: 233

when we calling the url like this /api/v1/users/appointments, the appointments will be thrown.

To get the list of appointments for the concerned user of id 74, how to make the url. Think i am not getting right, i am pulling my heads

Upvotes: -1

Otto Sandstr&#246;m
Otto Sandstr&#246;m

Reputation: 745

Its because you have the route variable $id defined in your user route. This varible triggers the restful controller to call get($id) instead of getList()

Either move the route down so its not a child of the user-route or add a constraint on the id route and calling /api/v1/users/appointments or change the name of $id in the user route also works

Upvotes: 1

Related Questions