教程6: Vökuró

Vökuró是另一个去学习phalcon的示例应用。Vökuró实现了安全特性,管理用户和权限。可以从 Github 克隆项目。

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.

目录结构

当克隆完项目后会看到如下目录结构:

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

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

项目结构和INVO类似,用浏览器访问http://localhost/vokuro 会看到如下所示:

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.

加载类和依赖关系

项目使用Phalcon\Loader加载控制器、模块、表单等。使用composer_ 去处理依赖关系。在终端中输入如下命令:

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ó 使用swift去通知确认注册用户。composer.json如下所示:

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.4.0",
  4. "ext-phalcon" : ">=2.0.0",
  5. "swiftmailer/swiftmailer" : "5.0.*",
  6. "amazonwebservices/aws-sdk-for-php" : "~1.0"
  7. }
  8. }

app/config/loader.php 中写有自动加载文件的配置,在文件最后有composer自动加载器去加载依赖类。

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 __DIR__ . '/../../vendor/autoload.php';

再次和INVO不同的是,vokuro使用命名空间去组织项目,(app/config/loader.php)这个文件看起来和INVO不太相同。

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. $loader = new \Phalcon\Loader();
  3. $loader->registerNamespaces(array(
  4. 'Vokuro\Models' => $config->application->modelsDir,
  5. 'Vokuro\Controllers' => $config->application->controllersDir,
  6. 'Vokuro\Forms' => $config->application->formsDir,
  7. 'Vokuro' => $config->application->libraryDir
  8. ));
  9. $loader->register();
  10. // ...

我们使用registerNamespaces替代了registerDirectories。每个命名空间指向了(app/config/config.php)文件中定义的目录。例如,命名空间Vokuro\Controllers指向了app/controllers,在命名空间中应用需要的类定义如下:

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. }

注册

首先检查用户是否在vokuro中注册。当用户点击注册按钮,sessioncontroller控制器被调用。signup方法被执行。

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. }

这个动作只传递一个表单SignUpForm实例给视图,展示给用户并让用户输入登录数据:

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

结论

As we have seen, develop a RESTful API with Phalcon is easy. Later in the documentation we’ll explain in detail how to use micro applications and the PHQL language.