Creating a user in M, V, C
Create the model in models/Users.php
:
use app\models\Users;
use lithium\aop\Filters;
use lithium\security\Password;
Filters::apply(Users::class, 'save', function($params, $next) {
if ($params['data']) {
$params['entity']->set($params['data']);
$params['data'] = [];
}
$params['entity']->password = Password::hash($params['entity']->password);
return $next($params);
});
This example uses new-style filters, available with 1.1.
Now add the following line to app/config/bootstrap.php to include your new user.php bootstrap file.
namespace app\controllers;
use lithium\security\Auth;
use app\models\Users;
class UsersController extends \lithium\action\Controller {
public function index() {
$users = Users::all();
return compact('users');
}
$user = Users::create($this->request->data);
if (($this->request->data) && $user->save()) {
return $this->redirect('Users::index');
}
return compact('user');
}
}
Then create the templates.
views/users/index.html.php
:
<h2>Users</h2>
<ul>
<?php foreach ($users as $user) { ?>
<li><?= $user->username ?></li>
<?php } ?>