在windows上使用symfony创建简易的CMS系统(三)

http://blog.csdn.net/kunshan_shenbin/article/details/7168249

本文主要讲述component的使用。

在cms/apps/frontend/modules/home/actions/中新建components.class.php文件,代码如下。

  1. <?php   
  2.   
  3. class homeComponents extends sfComponent {  
  4.   
  5.     function execute($request) {  
  6.           
  7.     }  
  8.       
  9.     function executeNavigator($request) {  
  10.       
  11.         $this->categories = Doctrine::getTable('Category')  
  12.         ->findAll();  
  13.     }  
  14. }  
  15.   
  16. ?>  


修改actions.class.php文件,去掉$this->categories = 。。。这一行。

在cms/apps/frontend/modules/home/actions/中新建_navigator.php文件(注意文件名是以下划开头的)

代码如下:

[html] view plaincopy
  1. <div>  
  2.     <a href="<?php echo url_for("@homepage"); ?>">首页</a>  
  3.     <?php foreach ($categories as $category) :?>  
  4.         <a href="<?php echo url_for("category/edit?id=".$category->getId()); ?>">  
  5.             <?php echo $category->getName(); ?>  
  6.         </a>  
  7.     <?php endforeach; ?>  
  8. </div>  


打开indexSuccess.php,去掉上述首页导航条的代码。

修改cms/apps/frontend/templates/layout.php文件,代码如下:

[html] view plaincopy
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">  
  3.   <head>  
  4.     <?php include_http_metas() ?>  
  5.     <?php include_metas() ?>  
  6.     <?php include_title() ?>  
  7.     <link rel="shortcut icon" href="/favicon.ico" />  
  8.     <?php include_stylesheets() ?>  
  9.     <?php include_javascripts() ?>  
  10.   </head>  
  11.   <body>  
  12.     <strong><?php include_component('home', 'navigator'); ?></strong>  
  13.         <?php echo $sf_content ?>  
  14.   </body>  
  15. </html>  

重新访问http://localhost:1300/frontend_dev.php/,点击导航条,可以发现导航已经能在所有页面的上方显示了。