Paginator Error

Hello, I am moving to cake 5.1 and receive Paginator Error in the template inthe header tags.
My Controller function:

public function index($id = null) {
    $query = $this->News->find();

    $news = $this->paginate($query, [
        'scope' => 'news'
    ]);
    $this->set(compact('news'));
    $this->render('/Homepage/News/index');
}

Error: TypeError: Cake\View\View::get(): Argument #1 ($var) must be of type string, int given
called in PaginatorHelper.php on line 142
Template

<?php
/**
 * @var \\App\\View\\AppView $this
 * @var iterable<\\App\\Model\\Entity\\Article> $articles
 */
$yesno = $this->get('appYesNo');
$appnews = $this->get('appNews');
?>
<?= $this->Element('navigation', ['navi' => $prodItem]); ?>
<div class="news index content">
    <?= $this->Html->link(__('Neue Neuigkeit'), ['action' => 'add'], ['class' => 'button float-right']) ?>
    <h3><?= __('Neuigkeiten') ?></h3>
    <div class="table-responsive">
        <table>
            <thead>
                <tr>
                    <th width="40"><?= $this->Paginator->sort('id') ?></th>
                    <th width="300"><?= $this->Paginator->sort('title') ?></th>
                    <th><?= $this->Paginator->sort('type') ?></th>
                    <th><?= $this->Paginator->sort('folder') ?></th>
                    <th><?= $this->Paginator->sort('active') ?></th>
                    <th><?= $this->Paginator->sort('login') ?></th>
                    <th width="200" class="actions"><?= __('Actions') ?></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($news as $news): ?>
                    <tr>
  <td><?= h($news->title) ?></td>
                    <td>
                        <?php
                        echo $appnews[$news->type];
                        ?>
                    </td>
                    <td><?= h($news->folder) ?></td>
                    <td>
                        <?php
                        echo $yesno[$news->active];
                        ?>
                    </td>
                    <td>
                        <?php
                        echo $yesno[$news->login];
                        ?>
                    </td>
                    <?= $this->Element('viewArticleAction', array('recCon' => 'News', 'action' => 'singleNews', 'recId' => $news->id, 'parentId' => $news->id)); ?>   
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <div class="paginator">
        <ul class="pagination">
            <?= $this->Paginator->first('<< ' . __('first')) ?>
            <?= $this->Paginator->prev('< ' . __('previous')) ?>
            <?= $this->Paginator->numbers() ?>
            <?= $this->Paginator->next(__('next') . ' >') ?>
            <?= $this->Paginator->last(__('last') . ' >>') ?>
        </ul>
        <p><?= $this->Paginator->counter(__('Page {{page}} of {{pages}}, showing {{current}} record(s) out of {{count}} total')) ?></p>
    </div>
</div>

Whats the problem? Kid regards Klaus

@Klaus first of all - please use the “Preformatted text” Feature in this Forum so your code actually gets pasted in properly. I manually cleaned it up for now.

But your “problem” here is this line at the top:

$appnews = $this->get('appNews');

You don’t need to retrieve data inside your templates. Whenever you set a variable inside the controller like so

$this->set(compact('news'));

you automatically have access to it via `$news`

Also you can look at all your accesible variables via Debug Kit “Variables” Panel or just add a simple

pr($this->viewVars);

in your template (and make sure your Debug Mode is active) to see what your current scope of variables has to offer.

You may write foreach($news as $item):

1 Like