我正在使用 laravel 并按照教程进行操作http://technetlk.blogspot.com.au/2012/09/laravel-backbonejs-coffeescript_3527.html
。目前正在处理本教程的第 7 部分。
在routes.php
我写过
Route::any('api/category/(:num?)',
array('as' => 'api.category',
'uses' => 'api.category@index')
);
在api/category.php
,
<?php
class Api_Category_Controller extends Base_Controller
{
public $restful = true;
public function get_index($id = null)
{
if (is_null($id ))
{
$allCats = Category::all();
return BaseModel::allToJson($allCats);
}
else
{
$cat = Category::find($id);
return $cat->toJson();
}
}
public function post_index()
{
$cat = Input::json();
$dbCat = new Category();
$dbCat->code = $cat->code;
$dbCat->name = $cat->name;
$dbCat->save();
return $dbCat->toJson();
}
public function put_index()
{
$cat = Input::json();
$dbCat = Category::find($cat->id);
$dbCat->code = $cat->code;
$dbCat->name = $cat->name;
$dbCat->save();
return $dbCat->toJson();
}
public function delete_index($id = null)
{
$dbCat = Category::find($id);
$dbCat->delete();
}
}
?>
并且在BaseModel.php
<?php
class Category extends BaseModel
{
public static $table = 'tbl_category';
}
class BaseModel extends Eloquent
{
public function toJson()
{
return json_encode($this->to_array());
}
public static function allToJson($array)
{
$temp = array();
foreach($array as $t)
{
$temp[] = $t->to_array();
}
return json_encode($temp);
}
}
?>
当我试图跑步时
curl -X POST http://lbc.dev/api/category -H "Content-Type: application/json" –d '{"code":"cat1", "name":"Category One"}'
我收到以下错误
未处理的异常
信息:
找不到类“类别”位置:
C:\xampp\htdocs\NewBlog\application\controllers\api\category.php 在第 9 行