3: view-template

Posted under » CakePHP on 11 Dec 2018

Continued from create controller article.

See also

CakePHP’s template files are stored in "src/Template" inside a folder named after the controller they correspond to. So we’ll have to create a folder named ‘Articles’ in this case.

<!-- File: src/Template/Articles/index.ctp -->
<h1>Articles
<table>
<tr>
<th>Title
<th>Created
</tr>
<!-- Here is where we iterate through our $articles query object, printing out article info -->
<¿php foreach ($articles as $article): ?>
<tr>
<td>
<?= $this->Html->link($article->title, ['action' => 'view', $article->slug]) ?>
</td>
<td>
<?= $article->created->format(DATE_RFC850) ?>
</td>
</tr>
<¿php endforeach; ?>
</table>

'$this->Html' is an instance of the CakePHP HtmlHelper. CakePHP comes with a set of view helpers that make tasks like creating links, forms, and pagination buttons easy.

the link() method will generate an HTML link with the given link text (the first parameter) and URL (the second parameter). When specifying URLs in CakePHP, it is recommended that you use arrays or named routes. These syntaxes allow you to leverage the reverse routing features CakePHP offers.

You should now be able to point your browser to http://kueh.com/articles/index or /articles. You should see your list view, correctly formatted with the title and table listing of the articles.

However if you were to click one of the ‘view’ links in our Articles list page, you’d see an error page saying that action hasn’t been implemented. So have to add this view function to the controller which we did earlier. What we have is a page where it list all of our articles, but what about the articles itself? So let us create this article page ».

web security linux ubuntu GIT svn Raspberry apache mysql php drupal cake symfony javascript Ajax css