zf2使用抽象的restful控制器

时间:2015-02-12 12:17:51

标签: php zend-framework2

如何调用get()或getList()方法,但我总是让get()方法运行我做错了。我已在约会中的用户的子路由中执行null操作。

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',
        //),

    ),



);

约会控制人:

    <?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';
    }



}

3 个答案:

答案 0 :(得分:1)

这是因为您在用户路由中定义了路由变量$id。 此变量触发其余控制器调用get($id)而不是getList()

将路径向下移动,使其不是用户路由的子节点或在id路由上添加约束并调用/api/v1/users/appointments或更改用户路由中$id的名称也可以< / p>

答案 1 :(得分:1)

最后我得到了答案,

实际上当url说像/ api / v1 / users / id时它指向get方法,但是当端点有/ api / v1 / users时它指向getList()方法。

这里的关键是id参数,当我们将id更改为user_id时,我们不会在控制器中获得$ id值。

现在我的路线看起来像这样,

<?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',
         ),
),

);

答案 2 :(得分:-1)

当我们调用这个/ api / v1 / users / appointmentments这样的url时,会抛出约会。

要获取id为74的相关用户的约会列表,如何制作网址。想想我没有做对,我正在拉我的脑袋