zf2返回json

1、在module.conf.php里面的view_manager里添加:    

        'strategies' => array(

            'ViewJsonStrategy',

        ),

如:

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

2、在action里面引入“JsonModel”(use Zend\View\Model\JsonModel;),然后再替换之前rturn的ViewModel(return new JsonModel($data)):

public function indexAction() {           $result = new JsonModel(array(  'some_parameter' => 'some value',  'success'=>true,  ));   return $result; }

你可能感兴趣的:(json,PHP,zendframework2,zf2)