Navigate to my-project/app/controllers/IndexController.php
:
phpCopy code<?php
use Phalcon\Mvc\Controller;
class IndexController extends Controller
{
public function indexAction()
{
// Fetch all users from the database
$users = Users::find();
// Pass users data to the view
$this->view->users = $users;
}
public function addAction()
{
// Handle POST request to add a new user
if ($this->request->isPost()) {
$user = new Users();
$user->username = $this->request->getPost('username');
$user->email = $this->request->getPost('email');
if ($user->save()) {
echo "User added successfully!";
} else {
echo "Error adding user: ";
foreach ($user->getMessages() as $message) {
echo $message->getMessage(), "<br>";
}
}
}
}
}
Leave a Reply