Creating a user in M, V, C

    Create the model in models/Users.php:

    1. use app\models\Users;
    2. use lithium\aop\Filters;
    3. use lithium\security\Password;
    4. Filters::apply(Users::class, 'save', function($params, $next) {
    5. if ($params['data']) {
    6. $params['entity']->set($params['data']);
    7. $params['data'] = [];
    8. }
    9. $params['entity']->password = Password::hash($params['entity']->password);
    10. return $next($params);
    11. });
    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.

    1. namespace app\controllers;
    2. use lithium\security\Auth;
    3. use app\models\Users;
    4. class UsersController extends \lithium\action\Controller {
    5. public function index() {
    6. $users = Users::all();
    7. return compact('users');
    8. }
    9. $user = Users::create($this->request->data);
    10. if (($this->request->data) && $user->save()) {
    11. return $this->redirect('Users::index');
    12. }
    13. return compact('user');
    14. }
    15. }

    Then create the templates.

    views/users/index.html.php:

    1. <h2>Users</h2>
    2. <ul>
    3. <?php foreach ($users as $user) { ?>
    4. <li><?= $user->username ?></li>
    5. <?php } ?>