2013-05-03 63 views
0

我是CakePHP的新手;
點擊每個菜單後,URL地址欄總是被添加;URL總是附加? CakePHP

我定義我的路線如下:

Router::connect('/brand/*', array('controller' => 'phones', 'action' => 'phonebybrand')); 

這是我的菜單:

<?php    
     foreach ($phonebrands as $phonebrand): 
      echo '<li><a href="brand/{$phonebrand['Phonebrand']['id']}">{$phonebrand['Phonebrand']['brandname']}</a></li>'; 
     endforeach; 
?> 

我試圖點擊我多次菜單;我發現的是始終追加的URL;
第一次:localhost/cakephp-2.3.2/brand/43
第二次:localhost/cakephp-2.3.2/brand/brand/43
第三次:localhost/cakephp-2.3.2/brand/brand/brand/43

請幫助我,爲什麼它總是附加url?

謝謝。

+2

你是如何創建菜單?你能顯示一些代碼嗎? – Nunser 2013-05-03 15:01:20

+2

也許向我們展示鏈接是如何生成的?其實我們只是用心靈感應 – Ryan 2013-05-03 15:02:10

+0

加上你的菜單代碼.. – 2013-05-03 15:07:30

回答

1

你可能不會得到正確的基礎URL。

在你看來試試這個

<?php    
    foreach ($phonebrands as $phonebrand) { 
     echo '<li><a href="'.$this->Html->url('/', true).'brand/{$phonebrand['Phonebrand']['id']}"> {$phonebrand['Phonebrand']['brandname']}</a></li>'; 
    } 
?> 

否則與Router::url('/', true);取代$this->Html->url()
This post可能會有所幫助。

+0

最後,我發現了這個問題;因爲我沒有將它指回控制器,我也將它改爲像$ this-> html-> url(); **檢查下面的代碼;它正在工作:** ' echo'

  • '。$ this-> Html-> link($ phonebrand ['Phonebrand'] ['brandname'],array('controller'=>'phones','action'= > 'phonebybrand',$ phonebrand [ 'Phonebrand'] [ 'ID']))「。
  • ';' 謝謝! – 2013-05-03 16:00:37

    +0

    它看起來好多了,但該鏈接並沒有顯示'localhost/cakephp-2.3.2/phones/phonebybrand/$ id'鏈接,而不是'localhost/cakephp-2.3.2/brands/$ id'試圖製造?我已經用兩個選項更新了答案,看看你會得到什麼。很高興我可以幫助:) – Nunser 2013-05-03 16:20:28

    +0

    是的,我明白了。 感謝您的幫助。 ^^ – 2013-05-04 06:02:36

    0

    你可能會加入 「CakePHP的-2.3.2 /品牌/ 43」,而不是 「/cakephp-2.3.2/brand/43」

    0

    ,當你更換你的菜單代碼什麼happends:

    <?php    
         foreach ($phonebrands as $phonebrand): 
          echo '<li><a href="/brand/{$phonebrand['Phonebrand']['id']}">{$phonebrand['Phonebrand']['brandname']}</a></li>'; 
         endforeach; 
    ?>