Pada tutorial kemarin kita sudah membuat feature untuk menambahkan data user pada database, tutorial kali kita akan membuat feature untuk mengedit data user pada database
Oke kita mulai saja experimentnya,
Pertama, buat terlebih dahulu routenya dengan cara mengedit file router.php pada path app/config/ edit menjadi seperti ini,
<?php
$router = $di->getRouter();
// Define your routes here
/**
* Get user lists data by query
*/
$router->addGet(
"/user",
[
"controller" => "user",
"action" => "index"
]
);
$router->addGet(
"/user/{userId}",
[
"controller" => "user",
"action" => "show"
]
);
$router->addGet(
"/user/create",
[
"controller" => "user",
"action" => "create"
]
);
$router->addPost(
"/user/create",
[
"controller" => "user",
"action" => "save"
]
);
$router->addPut(
"/user/edit/{userId}",
[
"controller" => "user",
"action" => "edit"
]
);
$router->addGet(
"/test",
[
"controller" => "test",
"action" => "test"
]
);
$router->handle();
Selanjutnya, tambahkan method baru pada controller dengan cara mengedit file UserController.php pada path app/controllers/ edit menjadi seperti ini,
<?php
class UserController extends ControllerBase
{
public function indexAction()
{
$users = Users::find();
$this->view->title = "Phalcon - Users";
$this->view->users = $users;
// dd($users);
}
public function showAction($userId)
{
$conditions = ['userId'=>$userId];
$user = Users::findFirst([
'conditions' => 'id=:userId:',
'bind' => $conditions,
]);
$this->view->title = "Phalcon - User";
$this->view->user = $user;
// dd($users);
}
public function createAction()
{
$this->view->title = "Phalcon - Create user";
}
public function saveAction()
{
$name = $this->request->get('name');
$email = $this->request->get('email');
$address = $this->request->get('address');
$user = new Users();
$user->name = $name;
$user->email = $email;
$user->address = $address;
if ($this->request->isPost()) {
if ($user->create()) {
return $this->response->redirect('user');
}
} else {
$this->flash->error('User form is not valid.');
}
}
public function editAction($userId)
{
$conditions = ['userId'=>$userId];
$user = Users::findFirst([
'conditions' => 'id=:userId:',
'bind' => $conditions,
]);
$this->view->title = "Phalcon - User";
$this->view->user = $user;
if ($this->request->isPost()) {
$name = $this->request->get('name');
$email = $this->request->get('email');
$address = $this->request->get('address');
$user->name = $name;
$user->email = $email;
$user->address = $address;
if ($user->save()) {
return $this->response->redirect('user');
} else {
$messages = $user->getMessages();
foreach ($messages as $message) {
$this->flash->error($message);
}
}
}
}
}
Kemudian, buat file view baru dengan nama edit.volt pada path app/views/user/ dan isikan code berikut,
{% extends 'layouts/default1.volt' %}
{% block content %}
<br/>
Hello codedoct!!!
<br><br>
<div class="row">
<div class="col-md-12">
<form method="POST">
<div class="form-group">
<label>Nama user</label>
<input type="text" name="name" class="form-control" placeholder="Type your name" required value="{{ user.name }}">
</div>
<div class="form-group">
<label>Email user</label>
<input type="email" name="email" class="form-control" placeholder="Type your email" required value="{{ user.email }}">
</div>
<div class="form-group">
<label>Address user</label>
<textarea name="address" class="form-control" placeholder="Type your address" required>{{ user.address }}</textarea>
</div>
<input type="submit" value="Save"> <button type="button" onclick="window.location.href='../../user'">Cancel</button>
</form>
</div>
</div>
{% endblock %}
Terakhir, tambahkan link untuk kehalaman update dengan cara edit file index.volt pada path app/views/user/ menjadi seperti ini,
{% extends 'layouts/default1.volt' %}
{% block content %}
<br/>
Hello codedoct!!!
<br><br>
<div class="row">
<div class="col-md-12">
user list:
<br><br>
<table width="100%">
<tr>
<th>Name</th>
<th>Action</th>
</tr>
{% for user in users %}
<tr>
<td>{{ user.name }}</td>
<td><a href="user/{{ user.id }}">show</a> || <a href="user/edit/{{ user.id }}">edit</a></td>
</tr>
{% endfor %}
</table>
<br>
<button onclick="window.location.href='user/create'">Create</button>
</div>
</div>
{% endblock %}
Sehingga tampilan halaman editnya akan tampak seperti ini,
===DONE!===