Vote Up 0 Vote Down

How to set Meta Keywords and Description in Cakephp Controller

I am new to Cakephp. Where we set the meta tags in cakephp. I referred the cakephp manual. They insist to set the meta keywords and content in the views.

But I need some dynamic content loaded in the meta keywords and description. So I feel good if we can set the meta content in the controllers so that we have full control over the dynamic keywords. Any ideas ?
flag

Answers


Vote Up 0 Vote Down
You can of course define your meta tags content in the controllers. It is then more flexible and we can dynamically fill the keywords and description along with the title.

Try the following steps :

Step 1:
Edit the app/controllers/example_controller.php file and take any function. (Folder names slightly differ in new versions of Cakephp)

class TagsController extends AppController {
.....
function exampleFunction(){
$this->set('title_for_layout','My Title');
$this->set('description_for_layout','My page description');
$this->set('keywords_for_layout','Keyword1,Keyword2,Keyword3'); //here you can load keywords dynamically
.....
}

....

}


Step 2:

Edit the app/views/layouts/default.ctp (Or any other view file that is your header view page)

 <html>
<head>
<? if(isset($description_for_layout)){ ?>
<meta name = "description" content="<?=$description_for_layout;?>" />
<? } ?>

<? if(isset($keywords_for_layout)){ ?>
<meta name = "keywords" content="<?=$keywords_for_layout;?>" />
<? } ?>

<title>
<?=$title_for_layout;?>
</title>
...


These two steps will help us to define meta keywords, title and description in controllers rather than statically in views.
flag | link |

Your Answer

Login before answering

Login with facebook