0

My aim is to create a page/view without a database model - in essence I want to build a dashboard which will ultimately act as a portal page for accessing multiple tables of data (i.e. Countries, States and Genders) that I've created using the cake-bake-all method in Cakephp 3x.

By doing a little research I understood that with the built-in PagesController, I can't access the models. I'll have to create my own PagesController if I want to build a dashboard but I don't know what code to use. Is there any other easier approach to access several, unassociated models on one page? Any help would be greatly appreciated.

Update -

This is how I've created the Dashboard prototype, thanks to Chriss' advice!

Here is my code -

DashboardsController.php (/src/controller/)

<?php
namespace App\Controller;

use App\Controller\AppController;


class DashboardsController extends AppController
{

public function index()
{
  $this->loadModel ('States'); //Load models from States
  $states = $this->States->find('all');  // query all states
  $this->set('states', $states); // save states inside view

}
}
?>

index.ctp (src/Template/Dashboards/)

<?php //index function of dashboardscontroller ?>

<table>

<h2 class="col span_2_of_2"><?= ('State-Details') ?></h2>

<thead>

<tr>
<th scope="col"><?= $this->Paginator->sort('id') ?></th>
<th scope="col"><?= $this->Paginator->sort('state_name') ?></th>
<th scope="col"><?= $this->Paginator->sort('country_name') ?></th>
</tr>

</thead>

<tbody>
<?PHP foreach ($states as $state) : ?>

<tr>
<td>
<?= $this->Number->format($state->id) ?>
</td>

<td>
<?= h($state->state_name) ?>
</td>

<td>
<?= $state->has('country') ? 
$this->Html->link($state->country->country_name, ['controller' => 
'Countries', 'action' => 'view', 
 $state->country->id]) : '' ?>
</td>

<td class="actions">
<?= $this->Html->link(('View'), ['action' => 'view', $state->id]) ?>
<?= $this->Html->link(('Edit'), ['action' => 'edit', $state->id]) ?>
<?= $this->Form->postLink(('Delete'), ['action' => 'delete', $state->id], 
['confirm' => ('Are you sure you want to delete # {0}?', $state->id)]) ?>
</td>

</tr>

<?PHP endforeach; ?>
</tbody>
</table>
0

3 Answers 3

1

first create a Dashboard Controller inside ./src/Controller/ with the filename DashboardsController.php. Normally the Dashboard has only one index-function, unless you prepare several subsections. Here we assume that you only have one page.

<?PHP
  namespace App\Controller;

  use App\Controller\AppController;

  class DashboardsController extends AppController {

    public function index(){
      $this->loadModel ('States');
      $states = $this->States->find('all');  // query all states
      $this->set ('states', $states); // save states inside view
    }
  }  // end class DashboardsController
?>

Thats the C from MVC.

Unless you have special functionality in your tables and entities, it is not necessary to create a Table class or Entity class unless you need the PHPDoc declarations. The Cake ORM takes over for you (default class). So let's go over the M from MVC.

$this->loadModel ('States'); only load the Model inside the Controller. No less, but no more. If you have load the model inside the Controller you can Access that model with $this->States (e.g. $this->States->find('all');).

Now you must save the result inside the view (from Controller: $this->set ('states', $states); ).

The last part is the view (V) from MVC.

Create a file inside ./src/Template/Dashboards/ with the Name index.ctp (thats the template file for the index function (action) inside the Dashboards Controller).

<?PHP /* index function of Dashboards Controller */ ?>
<ul>
  <?PHP foreach ($states as $state) : ?>
    <li><?=$state->title; ?></li>
  <?PHP endforeach; ?>
</ul>

Now you can access the Dashboard with your url followed by the Controller-Name (e.g. http://{url-to-your-cake-system}/dashboards/).

Thats all. Cake use the Concept "convention over configuration". So If you cling to the conventions (file structure, filenames, class names, table names, etc.), Cake does it all for you more or less automatically.

P.S. In my opinion, there are only a few (and even less correct) approaches to using TableRegistry. You should try to avoid it from the beginning.

2
  • Thank you so much for your advice chriss! This completely solved my problem. I tried using TableRegistry class before & it was quite inconvenient but your method works perfectly! Thanks again mate! Jan 1, 2019 at 8:29
  • @w3educare Do not understand it as rudeness.Do not say thank you, but vote and/or accept the answer:https://stackoverflow.com/help/someone-answers
    – chriss
    Jan 2, 2019 at 14:20
0

Create a controller eg. DasboardController and use \Cake\Orm\TableRegistry::get(tableName)

You could use PagesController also, but its more common to deliver static pages with it

TableRegistry

0
0
  1. create DashboradController with index method or at PageController add dashboard() method.
  2. create Dashborad\index.ctp or Page\dashboard.ctp
  3. create simple Cells for various data presentations (exmp: hourly, daily, weekly ,.. ) and include in your index.ctp / dashboard.ctp
  4. route to your dashboard

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.