我正在尝试保存视图以进行计数。它仅在经过身份验证的用户访问此页面时保存记录。当未经身份验证的用户访问此功能时,保存失败。
use Cake\ORM\TableRegistry;
use Cake\Event\Event;
public function beforeFilter(Event $event)
{
$this->Auth->allow(['view']);
}
public function view($photoId = null)
{
$photoViewsTable = TableRegistry::get('PhotoViews');
$photoViews = $photoViewsTable->newEntity();
$photoViews->ip_address = $_SERVER['REMOTE_ADDR'];
$photoViews->photo_id = $photoId;
$photoViews->user_id = ($this->request->session()->read('Auth.User.id')) ? $this->request->session()->read('Auth.User.id') : 0;
$photoViews->ip_address = $_SERVER['REMOTE_ADDR'];
$photoViewsTable->save($photoViews);
}