2012-01-26 3 views
0

내 웹 사이트에서 CakePHP를 사용하고 있으며 변수를 구문 분석하고 레이아웃에 써야합니다. 변수는 MySQL의 테이블 "articles"컬럼 "author"에 있습니다.레이아웃의 변수

내가 글을 쓰는 글자가 인데, 필자가 views/layout/default.thtml에 글을 쓰면 아무 것도 보여주지 않는다.

간단한 방법이 있나요?

+0

어디에서 변수를 배치 했습니까? 질문 내용을 레이아웃의 내용으로 업데이트 할 수 있습니까 ('default.ctp'). 또한 관련 컨트롤러 코드가 도움이 될 수 있습니다. – mensch

답변

1

오른쪽 컨트롤러 기능에서이 변수를 얻고 있습니까? 어쩌면 당신은 beforeRender 기능에서이 작업을 수행하는 (http://planetcakephp.org/aggregator/items/1398-utilizing-the-appcontrollerbeforerender-to-assign-cakephps-controller-attribut)

0

하나의 옵션을 삽입 할 필요가 CakePHP의 requestAction을 사용하여 기본 레이아웃에 태그 할 수있는 요소에 배치하십시오. 기본적으로 다음과 비슷한 것을 할 것입니다.

//Create a function in your articles controller as such 
function authorName($articleId = NULL){  
    //Return Author if an ID is passed 
    if(!empty($articleId)){ 
     $this->Article->recursive = -1; 
     $article = $this->Article->findById($articleID); 

     $return $article['Article']['author']; 
    } 

} 

// Then create an element author_name.ctp and place it in views/elements 
// that requests the AuthorID form the above function 
<div> 
<?php 
    $author = $this->requestAction(
     array(
      'controller' => 'articles', 
      'action' => 'authorName' 
     ), 
     array(
      'pass' => $article_id 
     ) 
    ); 
    echo $author; 
?> 
</div> 

// Then in you default layout or anywhere else 
// you can include the element as such 
// You have to figure out a way to get the $id. If you can place a screenshot 
// Or some code of you default layout area where you need this, we can help you further 
<?php echo $this->element('author_name', array('article_id' => $id)); ?>