Я работаю над приложением блога с Codeigniter 3.1.8 и AngularJS v1.7.8 .
Панель инструментов приложенияявляется «чистым» Codeigniter, с моделями, контроллерами и представлениями, в то время как передняя часть состоит из JSON, управляемых и отображаемых AngularJS.
Мне не удалось добавить оставлять комментарии изинтерфейс через AngularJS.
Мой контроллер AngularJS выглядит следующим образом:
// Post comment
.controller('PostCommentController', ['$scope', '$http', '$routeParams',
function($scope, $http, $routeParams) {
const slug = $routeParams.slug;
$http.get('api/' + slug).then(function(response) {
let post_id = response.data.post.id
$scope.newComment = {
slug: $routeParams.slug,
post_id: post_id,
name: $scope.name,
email: $scope.email,
comment: $scope.comment
};
$scope.createComment = function(){
console.log($scope.newComment);
$http.post('api/comments/create/' + post_id, $scope.newComment);
};
});
}])
Мой контроллер комментариев Codeigniter (внутри API):
class Comments extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function create($post_slug){
$data = json_decode(file_get_contents('php://input'), TRUE);
$this->Comments_model->create_comment($post_id);
}
}
В Comments_model модель, которую я имею:
public function create_comment($post_id) {
$data = [
'post_id' => $post_id,
'name' => $this->input->post('name'),
'email' => $this->input->post('email'),
'comment' => $this->input->post('comment'),
'aproved' => 0,
'created_at' => date('Y-m-d H:i:s')
];
return $this->db->insert('comments', $data);
}
Строка console.log($scope.newComment);
внутри функции createComment
возвращает объект со всеми необходимыми "деталями":
{
comment: "Test Angular",
email: "razvan_zamfir80@yahoo.com",
name: "Razvan Zamfir",
post_id: "67",
slug: "who-loves-a-butterfly"
}
Тем не менее, я получаю ошибку Failed to load resource: the server responded with a status of 500 (Internal Server Error)
для этого URL: api/comments/create/67
, и оператор вставки MySQL выглядит следующим образом (я вижу его в браузере по адресу url / api / comments / create / 67):
INSERT INTO `comments` (`post_id`, `name`, `email`, `comment`, `aproved`, `created_at`) VALUES (NULL, NULL, NULL, NULL, 0, '2019-10-31 22:06:01')
Где моя ошибка?