Tutorial 6: Vökuró

Vökuró is another sample application you can use to learn more about Phalcon. Vökuró is a small website that shows how to implement a security features and management of users and permissions. You can clone its code from Github.

Project Structure

Once you clone the project in your document root you’ll see the following structure:

  1. vokuro/
  2. app/
  3. config/
  4. controllers/
  5. forms/
  6. library/
  7. models/
  8. views/
  9. cache/
  10. public/
  11. css/
  12. img/
  13. schemas/

This project follows a quite similar structure to INVO. Once you open the application in your browser http://localhost/vokuro you’ll see something like this:

../_images/vokuro-1.png

The application is divided into two parts, a frontend, where visitors can sign up the service and a backend where administrative users can manage registered users. Both frontend and backend are combined in a single module.

Load Classes and Dependencies

This project uses Phalcon\Loader to load controllers, models, forms, etc. within the project and composer to load the project’s dependencies. So, the first thing you have to do before execute Vökuró is install its dependencies via composer. Assuming you have it correctly installed, type the following command in the console:

  1. cd vokuro
  2. composer install

Vökuró sends emails to confirm the sign up of registered users using Swift, the composer.json looks like:

  1. {
  2. "require" : {
  3. "php" : ">=5.5.0",
  4. "ext-phalcon" : ">=3.0.0",
  5. "swiftmailer/swiftmailer" : "^5.4",
  6. "amazonwebservices/aws-sdk-for-php" : "~1.0"
  7. }
  8. }

Now, there is a file called app/config/loader.php where all the auto-loading stuff is set up. At the end of this file you can see that the composer autoloader is included enabling the application to autoload any of the classes in the downloaded dependencies:

  1. <?php
  2. // ...
  3. // Use composer autoloader to load vendor classes
  4. require_once BASE_PATH . "/vendor/autoload.php";

Moreover, Vökuró, unlike the INVO, utilizes namespaces for controllers and models which is the recommended practice to structure a project. This way the autoloader looks slightly different than the one we saw before (app/config/loader.php):

  1. <?php
  2. use Phalcon\Loader;
  3. $loader = new Loader();
  4. $loader->registerNamespaces(
  5. [
  6. "Vokuro\\Models" => $config->application->modelsDir,
  7. "Vokuro\\Controllers" => $config->application->controllersDir,
  8. "Vokuro\\Forms" => $config->application->formsDir,
  9. "Vokuro" => $config->application->libraryDir,
  10. ]
  11. );
  12. $loader->register();
  13. // ...

Instead of using registerDirectories(), we use registerNamespaces(). Every namespace points to a directory defined in the configuration file (app/config/config.php). For instance the namespace Vokuro\Controllers points to app/controllers so all the classes required by the application within this namespace requires it in its definition:

  1. <?php
  2. namespace Vokuro\Controllers;
  3. class AboutController extends ControllerBase
  4. {
  5. // ...
  6. }

Sign Up

First, let’s check how users are registered in Vökuró. When a user clicks the “Create an Account” button, the controller SessionController is invoked and the action “signup” is executed:

  1. <?php
  2. namespace Vokuro\Controllers;
  3. use Vokuro\Forms\SignUpForm;
  4. class RegisterController extends ControllerBase
  5. {
  6. public function signupAction()
  7. {
  8. $form = new SignUpForm();
  9. // ...
  10. $this->view->form = $form;
  11. }
  12. }

This action simply pass a form instance of SignUpForm to the view, which itself is rendered to allow the user enter the login details:

  1. {{ form("class": "form-search") }}
  2. <h2>
  3. Sign Up
  4. </h2>
  5. <p>{{ form.label("name") }}</p>
  6. <p>
  7. {{ form.render("name") }}
  8. {{ form.messages("name") }}
  9. </p>
  10. <p>{{ form.label("email") }}</p>
  11. <p>
  12. {{ form.render("email") }}
  13. {{ form.messages("email") }}
  14. </p>
  15. <p>{{ form.label("password") }}</p>
  16. <p>
  17. {{ form.render("password") }}
  18. {{ form.messages("password") }}
  19. </p>
  20. <p>{{ form.label("confirmPassword") }}</p>
  21. <p>
  22. {{ form.render("confirmPassword") }}
  23. {{ form.messages("confirmPassword") }}
  24. </p>
  25. <p>
  26. {{ form.render("terms") }} {{ form.label("terms") }}
  27. {{ form.messages("terms") }}
  28. </p>
  29. <p>{{ form.render("Sign Up") }}</p>
  30. {{ form.render("csrf", ["value": security.getToken()]) }}
  31. {{ form.messages("csrf") }}
  32. <hr>
  33. {{ endForm() }}