cakephp将数据输出为json
我是cakephp新手,我正在尝试创建一个简单的RESTAPI来处理车辆。我想以json格式输出我从mysql数据库获得的数据 这是我的控制器cakephp将数据输出为json,cakephp,json,Cakephp,Json,我是cakephp新手,我正在尝试创建一个简单的RESTAPI来处理车辆。我想以json格式输出我从mysql数据库获得的数据 这是我的控制器 class VehiclesController extends AppController { var $name = 'Vehicles'; var $layout = 'default'; function index() { $this->set('vehicles', $this->Vehi
class VehiclesController extends AppController {
var $name = 'Vehicles';
var $layout = 'default';
function index() {
$this->set('vehicles', $this->Vehicle->find('all'));
$this->set('title_for_layout','Home');
}
}
如何在视图中将内容显示为json?在这里创建一个视图
Views/Vehicles/json/index.ctp
在您看来,您应该能够使用:
<?php echo json_encode($vehicles); ?>
要输出JSON
然后,您可以通过以下url访问它:
/vehicles/index.json
在这里创建一个视图视图/vehicles/json/index.ctp
在您看来,您应该能够使用:
<?php echo json_encode($vehicles); ?>
要输出JSON
然后,您可以通过以下url访问它:
/vehicles/index.json
嗨,谢谢,它可以正常工作。但是如果它作为一行文字出现的话嗨,谢谢,它是有效的。但它应该作为一行文字出现吗