教程5:自定义 INVO

为了详细解释INVO,我们将解释如何定制INVO添加UI元素并根据控制器执行和修改标题。

To finish the detailed explanation of INVO we are going to explain how to customize INVO adding UI elements and changing the title according to the controller executed.

用户组件

所有应用程序的UI元素和视觉风格大部分是通过 Bootstrap 实现了。一些元素,比如导航栏根据应用程序的状态变化。例如,在右上角的“登录/注册”链接在一个用户登录到应用程序后变为“注销”。

All the UI elements and visual style of the application has been achieved mostly through Bootstrap. Some elements, such as the navigation bar changes according to the state of the application. For example, in the upper right corner, the link “Log in / Sign Up” changes to “Log out” if an user is logged into the application.

应用中这部分功能在”Elements” (app/library/Elements.php)中实现。

This part of the application is implemented in the component “Elements” (app/library/Elements.php).

  1. <?php
  2. use Phalcon\Mvc\User\Component;
  3. class Elements extends Component
  4. {
  5. public function getMenu()
  6. {
  7. //...
  8. }
  9. public function getTabs()
  10. {
  11. //...
  12. }
  13. }

这个类扩展了Phalcon\Mvc\User\Component。它不但强加扩展了这个组件,而且有助于更快地获得应用程序服务。现在,我们要在服务容器中注册我们的第一个用户组件:

This class extends the Phalcon\Mvc\User\Component. It is not imposed to extend a component with this class, but it helps to get access more quickly to the application services. Now, we are going to register our first user component in the services container:

  1. <?php
  2. //Register an user component
  3. $di->set('elements', function(){
  4. return new Elements();
  5. });

作为控制器,视图内插件或组件,该组件可以访问容器中的一个属性来达到访问服务的目的:

As controllers, plugins or components within a view, this component also has access to the services registered in the container and by just accessing an attribute with the same name as a previously registered service:

  1. <div class="navbar navbar-fixed-top">
  2. <div class="navbar-inner">
  3. <div class="container">
  4. <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
  5. <span class="icon-bar"></span>
  6. <span class="icon-bar"></span>
  7. <span class="icon-bar"></span>
  8. </a>
  9. <a class="brand" href="#">INVO</a>
  10. {{ elements.getMenu() }}
  11. </div>
  12. </div>
  13. </div>
  14. <div class="container">
  15. {{ content() }}
  16. <hr>
  17. <footer>
  18. <p>&copy; Company 2014</p>
  19. </footer>
  20. </div>

重要的部分是:

The important part is:

  1. {{ elements.getMenu() }}

动态改变标题

当我们浏览的时候会发现标题动态的变化表明我们所在的页面。这主要通过每个控制器中初始化实现:

When you browse between one option and another will see that the title changes dynamically indicating where we are currently working. This is achieved in each controller initializer:

  1. <?php
  2. class ProductsController extends ControllerBase
  3. {
  4. public function initialize()
  5. {
  6. //Set the document title
  7. $this->tag->setTitle('Manage your product types');
  8. parent::initialize();
  9. }
  10. //...
  11. }

注意, parent::initialize()也被调用了,这将会添加更多的数据到标题中:

Note, that the method parent::initialize() is also called, it adds more data to the title:

  1. <?php
  2. use Phalcon\Mvc\Controller;
  3. class ControllerBase extends Controller
  4. {
  5. protected function initialize()
  6. {
  7. //Prepend the application name to the title
  8. $this->tag->prependTitle('INVO | ');
  9. }
  10. //...
  11. }

最后标题被输出到主视图上(app/views/index.phtml):

Finally, the title is printed in the main view (app/views/index.phtml):

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <?php echo $this->tag->getTitle() ?>
  5. </head>
  6. <!-- ... -->
  7. </html>