簡體   English   中英

CakePHP 2.2.4:使用組件/幫助器輕松幫助鏈接

[英]CakePHP 2.2.4: Help Simply Links using Component/Helper

我做了一個幫助程序,它返回給定頁面的URL。

助手文件:

  public function PageURL($link = null, $category = null, $page = null){

            if($link){
                $link = strtolower(str_replace(' ', '-', $link));
                $page_url = "http://www.domain.com/$link";
            }
            else{
                $page_url = "http://www.domain.com/$category/$page";
            }
                return $page_url;
        }

(順便說一句,是否有一個變量可以代替http://www.domain.com使用,例如full_site_url,base_url等?)

這是我將參數傳遞給助手時的視圖:

<?php echo $this->Html->link($page['Page']['title'], $this->Custom->PageuRL($page['Page']['link'], $page['Category']['directory'], $page['Page']['id'])); ?>

每次都要寫很多東西。

我創建了一個組件,該組件可獲取要訪問的頁面的URL。 它目前是在AppController上實現的,因此我可以很好地顯示當前頁面的URL,但是我想在視圖內部或幫助器內部使用它來顯示另一個頁面的URL。

public function TestPageURL($page = null) {


App::uses('ClassRegistry', 'Utility');
$pagesModel = ClassRegistry::init('Page');
$matchedpage = $pagesModel->find('first', array(
    'conditions' => array('Page.id' => $page), 'recursive' => '0'
));


        if($matchedpage['Page']['link']){
            $pagelink = strtolower(str_replace(' ', '-', $matchedpage['Page']['link']));
            $page_url = "http://www.domain.com/" . $pagelink;
        }
        else{
            $page_url = "http://www.domain.com/" . $matchedpage['Page']['Category']['directory'] . $matchedpage['Page']['id'];

        }

        return $page_url;

} // end page url

通過這種方式,我只需要將一個參數傳遞給組件。

我知道在助手中使用組件不是很好,並且我不確定在此版本的CakePHP中是否甚至允許使用它,但這會使創建鏈接變得更加簡單。 有誰知道我該如何在幫助程序中使用此組件,或者以僅傳遞page變量的相同方式使幫助程序起作用?

編輯:好的,這對我有用,並且可能會引起每個人的反對,因為它涉及在幫助程序中進行查詢,但實際上是在簡化事情。 我會看看它是否會使我的網站慢下來。

我仍然願意就如何改進它提出建議。

  public function TestPageURL($page = null) {


    $pagesModel = ClassRegistry::init('Page');
    $matchedPage = $pagesModel ->find('first', array(
        'conditions' => array('Page.id' => $page), 'recursive' => '1'
    ));

           if($matchedPage ['Page']['link']){
                $link = strtolower(str_replace(' ', '-', $matchedPage['Page']['link']));
                $page_url = "http://www.domain.com/$link";
            }
            else{
                $page_url = "http://www.domain.com/" . $matchedPage['Page']['Category']['directory'] . '/' .$matchedPage['Page']['id'];
            }


            return $page_url;

    }

我建議在控制器中獲取數據時在模型中創建URL。 順便說一句,您可以使用Router::url檢索完整的基本URL。

示例(未試用)...型號:

public function findPagesIncludingURLs()
{
    $pages = $this->find('all'); // or whatever you want to retreive

    $base = Router::url('/', true);
    foreach($pages as &$page)
    {
        $url = null;
        if($page['Page']['link'])
        {
            $url = strtolower(str_replace(' ', '-', $page['Page']['link']));
        }
        else
        {
            $url = $page['Page']['Category']['directory'] . '/' . $page['Page']['id'];
        }

        $page['Page']['url'] = $base . $url;
    }

    return $pages;
}

控制器:

$this->set('pages', $this->Page->findPagesIncludingURLs());

視圖:

echo $this->Html->link($page['Page']['title'], $page['Page']['url']);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM