Cakephp2.3 html->;自定义函数中的url

Cakephp2.3 html->;自定义函数中的url,cakephp,cakephp-2.0,cakephp-2.1,Cakephp,Cakephp 2.0,Cakephp 2.1,我正在使用Cakephp2.0我创建了一个自定义函数 <?php echo GenerateNavHTML($navarray); ?> <?php function GenerateNavHTML($nav) { if(!empty($nav)) { if(isset($nav[0]['parent'])) { $parentid=$nav[0]['pa

我正在使用Cakephp2.0我创建了一个自定义函数

    <?php echo GenerateNavHTML($navarray);  ?>

<?php 

    function GenerateNavHTML($nav)
    { 

        if(!empty($nav)) {

              if(isset($nav[0]['parent'])) {
                     $parentid=$nav[0]['parent'];
              }else{
                  $parentid=1;
              }
             if($parentid==0) {
                 $html = '<ul style="display: block;" class="nav">';
             }else {
                 $html='<ul>';
             }
               foreach($nav as $page) {
                    $html.='<li>';    
                    $html .= '"'.$this->Html->url('Logout',array('controller'=>'users','action'=>'logout')).'"';


                    $html .= '</li>';
                }
                $html.='</ul>';
                return $html;
         }
    }

       ?> 

它给

致命错误:无法重新声明GenerateNavHTML()

但没有对功能的重新说明

如果我写

<?php 

function GenerateNavHTML($nav)
{ 

    if(!empty($nav)) {

          if(isset($nav[0]['parent'])) {
                 $parentid=$nav[0]['parent'];
          }else{
              $parentid=1;
          }
         if($parentid==0) {
             $html = '<ul style="display: block;" class="nav">';
         }else {
             $html='<ul>';
         }
           foreach($nav as $page) {
                $html.='<li>';    
                $html .= '<a href=""></a>';


                $html .= '</li>';
            }
            $html.='</ul>';
            return $html;
     }
}

   ?> 

它工作得很好

我想使用cakephp语法

谢谢

试试这个:

<?php echo $this->GenerateNavHTML($navarray);  ?>


在MVC中,此代码应该是帮助程序的一部分,而不仅仅是一个独立的“函数”

创建自己的助手 这听起来很难,但事实并非如此。它也有许多优点;通过将代码移动到助手,可以更容易地重用和维护

比如,

创建一个“导航”助手(当然,给它一个逻辑名称)

app/View/Helper/NavigationHelper.php

class NavigationHelper extends AppHelper
{
    /**
     * Other helpers used by *this* helper
     * 
     * @var array
     */
    public $helpers = array(
        'Html',
    );
    

    /**
     * NOTE: In general, convention is to have
     *       functions/methods start with a lowercase
     *       only *Classes* should start with a capital
     * 
     * @param array $nav
     * 
     * @return string
     */
    public function generateNavHTML($nav)
    {
        $html = '';

        if (!empty($nav)) {

            if (isset($nav[0]['parent'])) {
                $parentid = $nav[0]['parent'];
            } else {
                $parentid = 1;
            }
            if ($parentid == 0) {
                $html = '<ul style="display: block;" class="nav">';
            } else {
                $html = '<ul>';
            }
            foreach ($nav as $page) {
                $html .= '<li>';
                $html .= '"' . $this->Html->url('Logout', array('controller' => 'users', 'action' => 'logout')) . '"';
                $html .= '</li>';
            }
            $html .= '</ul>';
        }

        // NOTE: moved this 'outside' of the 'if()'
        //       your function should always return something
        return $html;
    }

    /**
     * You can add other methods as well
     * For example, a 'Convenience' method to create a link to the Homepage
     *
     * Simply use it like this:
     * <code>
     * echo $this->Navigation->logoutLink();
     * </code>
     */
    public function logoutLink()
    {
        return $this->Html->link(__('Log out'), array(
                'controller' => 'users',
                'action' => 'logout',
                'confirm' => __('Are you sure you want to log out')
            ));
    }    }
创建该文件后,可以在任何视图或元素中使用它

echo $this->Navigation->generateNavHTML($navarray);
您甚至不必将其添加到控制器的“Helpers”数组中,因为CakePHP 2.3使用“自动加载”来为您完成这项工作

如果您需要其他功能(与“导航”相关),您只需向助手添加一个“方法”,我添加了一个“logoutLink()”方法就是为了说明这一点

有关更多信息,请阅读本章