ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# 教程 6: Vkuró # Tutorial 6: Vkuró Vkuró is another sample application you can use to learn more about Phalcon.Vkuró is a small website that shows how to implement a security features andmanagement of users and permissions. You can clone its code from [Github](https://github.com/phalcon/vokuro). ### Project Structure Once you clone the project in your document root you'll see the following structure: ``` <pre class="calibre14">``` invo/ app/ cache/ config/ controllers/ forms/ library/ models/ plugins/ views/ public/ css/ js/ schemas/ ``` ``` This project follows a quite similar structure to INVO. Once you open the application in yourbrowser <http://localhost/vokuro> you'll see something like this: ![](https://box.kancloud.cn/2015-12-30_5683410194558.png) The application is divided into two parts, a frontend, where visitors can sign up the serviceand a backend where administrative users can manage registered users. Both frontend and backendare 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](https://getcomposer.org/)to load the project's dependencies. So, the first thing you have to do before execute Vkuró isinstall its dependencies via [composer](https://getcomposer.org/). Assuming you have it correctly installed, type thefollowing command in the console: ``` <pre class="calibre14">``` cd vokuro composer install ``` ``` Vkuró sends emails to confirm the sign up of registered users using Swift,the composer.json looks like: ``` <pre class="calibre14">``` { "require" : { "php" : ">=5.4.0", "ext-phalcon" : ">=2.0.0", "swiftmailer/swiftmailer" : "5.0.*", "amazonwebservices/aws-sdk-for-php" : "~1.0" } } ``` ``` Now, there is a file called app/config/loader.php where all the auto-loading stuff is set up. At the end ofthis file you can see that the composer autoloader is included enabling the application to autoloadany of the classes in the downloaded dependencies: ``` <pre class="calibre14">``` <?php // ... // Use composer autoloader to load vendor classes require_once __DIR__ . '/../../vendor/autoload.php'; ``` ``` Moreover, Vkuró, unlike the INVO, utilizes namespaces for controllers and modelswhich is the recommended practice to structure a project. This way the autoloader looks slightlydifferent than the one we saw before (app/config/loader.php): ``` <pre class="calibre14">``` <?php $loader = new \Phalcon\Loader(); $loader->registerNamespaces( array( 'Vokuro\Models' => $config->application->modelsDir, 'Vokuro\Controllers' => $config->application->controllersDir, 'Vokuro\Forms' => $config->application->formsDir, 'Vokuro' => $config->application->libraryDir ) ); $loader->register(); // ... ``` ``` Instead of using registerDirectories, we use registerNamespaces. Every namespace points to a directorydefined in the configuration file (app/config/config.php). For instance the namespace Vokuro\\Controllerspoints to app/controllers so all the classes required by the application within this namespacerequires it in its definition: ``` <pre class="calibre14">``` <?php namespace Vokuro\Controllers; class AboutController extends ControllerBase { // ... } ``` ``` ### Sign Up First, let's check how users are registered in Vkuró. When a user clicks the “Create an Account” button,the controller SessionController is invoked and the action “signup” is executed: ``` <pre class="calibre14">``` <?php namespace Vokuro\Controllers; use Vokuro\Forms\SignUpForm; class RegisterController extends ControllerBase { public function signupAction() { $form = new SignUpForm(); // ... $this->view->form = $form; } } ``` ``` This action simply pass a form instance of SignUpForm to the view, which itself is rendered toallow the user enter the login details: ``` <pre class="calibre14">``` {{ form('class': 'form-search') }} <h2>Sign Up</h2> <p>{{ form.label('name') }}</p> <p> {{ form.render('name') }} {{ form.messages('name') }} </p> <p>{{ form.label('email') }}</p> <p> {{ form.render('email') }} {{ form.messages('email') }} </p> <p>{{ form.label('password') }}</p> <p> {{ form.render('password') }} {{ form.messages('password') }} </p> <p>{{ form.label('confirmPassword') }}</p> <p> {{ form.render('confirmPassword') }} {{ form.messages('confirmPassword') }} </p> <p> {{ form.render('terms') }} {{ form.label('terms') }} {{ form.messages('terms') }} </p> <p>{{ form.render('Sign Up') }}</p> {{ form.render('csrf', ['value': security.getToken()]) }} {{ form.messages('csrf') }} <hr> </form> ``` ``` ### Conclusion As we have seen, develop a RESTful API with Phalcon is easy. Later in the documentation we'll explain in detail how touse micro applications and the [*PHQL*](#) language. | - [索引](# "总目录") - [下一页](# "教程 7:创建简单的 REST API(Tutorial 7: Creating a Simple REST API)") | - [上一页](# "Tutorial 5: Customizing INVO") |