0

I am using Cakephp2.0 i created a custom function

    <?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;
         }
    }

       ?> 

and it give

Fatal error: Cannot redeclare GenerateNavHTML()

But there is no redeclaration of function.

if i write

<?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;
     }
}

   ?> 

and it is working fine

i want to use cakephp syntax

Thanks

2 Answers 2

2

In MVC, this code should be part of a Helper, not just a standalone 'function'.

Creating your own Helper

This may sound hard, but it really isn't. It has many advantages as well; by moving your code to a Helper, it's easier to re-use and maintain.

For example;

Create a 'Navigation' helper (of course, give it a logical name);

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')
            ));
    }    }

Once you created that file, you can use it in any view or element;

echo $this->Navigation->generateNavHTML($navarray);

You don't even have to add it to the 'Helpers' array of your controller, because CakePHP 2.3 uses 'autoloading' to do that for you

If you need other functionality (related to 'Navigation'), you can just add a 'method' to the Helper, I added a 'logoutLink()' method just to illustrate this

For more information, read this chapter Creating Helpers

Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

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

And declare the function before all for secure

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.