Data for website <title>, <aside> and so on


the main question about my topic is, where should I put the data for title-tag, aside-tag …

Actual stand:
I’m building a webpage without database. I’ve put in /templates/element files like nav.php and footer.php.
In /templates/Pages files like home.php and site_notice.php, so the PagesController rule the /templates/Pages directory, right?!
If I try to talk to the /templates/Pages via self made actions below the CakePHP standard function in PagesController it doesn’t work.

I read, that Controllers are normally used for the connection between database and views. The Components are additions for the Controllers, right?

So my idea is to make a own PHP file where I put some functions with

$title = 'myTitle;
$this->assign('title', $title);

for every site I’ve made, because I don’t want to mix up my /template files with PHP and HTML code.
Should I solve it this way? Is it CakePHP confirm?
Where should I put my own PHP files if they nothing have to do with any kind of database?


How do you talk to the static Pages in template/pages and which errormessage is shown?

Thank’s for your very fast reply.

There are no error messages…

The regular CakePHP PagesController build the sites in /templates/pages so I can watch them in Google Chrome Browser.

The conversation between CakePHP and the HTML-Code in /templates/pages is by now only about some hyperlinks:

<?= $this->Html->link('myLink', 'pages/home', ['class'=>'button']) ?>

Now I would try to set the title which should be for /templates/Pages/home.php: Home and for /templates/Pages/site_notice: Site notice.
In /templates/element I have aside.php. It should display on Home: Home and on Site notice: Site notice.
I’m very unsure if it’s a good idea to solve it with an selfmade controller, because (if I understand it in a right way) the given CakePHP Controller “PagesControler” handles /templates/pages already and I use no database. In CakePHP Cookbook they write, that Controllers are only for communication between views and databases…

So should I build some own PHP files with own logic?
If yes, where should I put them?
The given Cake structure is a little bit confusing for me by now (I’m a absolute beginner in CakePHP).

You are on the right way.
You just need to make some changes to your default layout located in templates/layout/default.php

Here is an example of how it could be (i only show the part between the body tag):

       <?= $this->element('nav')?>  //will load „templates/element/nav.php“  
    <?= $this->Flash->render() ?>
    <?= $this->fetch('content') ?>
     <?= $this->element('footer') ?> //will load „templates/element/nav.php“  

example for nav.php:


  <li> <?= $this->Html->link(__('Click me to View Page 1'),'/pages/page1']) ?> </li> <!-- will load "templates/pages/page1.php"-->
  <li> <?= $this->Html->link(__('Click me to View Page 2'),'/pages/page2']) ?> </li> <!-- will load "templates/pages/page2.php"-->
  <li> <?= $this->Html->link(__('Click me to View Page 3'),'/pages/page3']) ?> </li> <!-- will load "templates/pages/page3.php"-->
  <li> <?= $this->Html->link(__('Click me to View Page 4'),'/pages/page4']) ?> </li> <!-- will load "templates/pages/page4.php"-->
  <li> <?= $this->Html->link(__('Click me to View Page 5'),'/pages/page5']) ?> </li> <!-- will load "templates/pages/page5.php"-->

and then in page1.php, page2.php,… you can do

$this->assign('title', $title);
1 Like

Great :slight_smile: so I’m in the right way for sure - that’s a good feeling :sunny:
Thank you so much, dirk!

One question again…
If we think about the patterns of Clean Code, is it not better to solve all title assignments for every page in it’s own function in one extra file and include it in /templates/Pages/home.php, /site_notice.php and so on?

I´m not sure, if i get the meaning of your question.

should be at the beginning of each page in /templates/pages.
“Sitename” is to be replaced with the text you want to see in your brwoser tab.
There is nood need for one extra file.

1 Like

Yes, you get it right I see!
You’ve got answered all my questions very well. That helps me in my point of beginning enormous.

Again, thank you very much for the time you spent and your patience.

I wish you a nice day :slight_smile: