You should create functions in a controller like insert, update, delete, and clear cart etc.
eg : for insert new item in cart write below code that accepts value.
$cartItem = array(
'id' => 'MOTOG5',
'qty' => 5,
'price' => 100.99,
'name' => 'Motorola Moto G5 - 16 GB',...