简体   繁体   English

在没有用于构建仪表板的模型的情况下,如何在cakephp 3中创建页面/视图?

[英]How to create a page/view in cakephp 3 without a model for building a dashboard?

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 (ie Countries, States and Genders) that I've created using the cake-bake-all method in Cakephp 3x. 我的目的是创建没有数据库模型的页面/视图-本质上讲,我想构建一个仪表板,该仪表板最终将用作访问我使用的多个数据表(即国家,州和性别)的门户页面。 Cakephp 3x中的cake-bake-all方法。

By doing a little research I understood that with the built-in PagesController, I can't access the models. 通过做一些研究,我了解到内置的PagesController无法访问模型。 I'll have to create my own PagesController if I want to build a dashboard but I don't know what code to use. 如果要构建仪表板,我将必须创建自己的PagesController,但我不知道要使用什么代码。 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! 感谢克里斯(Chriss)的建议,这就是我创建仪表板原型的方式!

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>

Create a controller eg. 创建一个控制器,例如 DasboardController and use \\Cake\\Orm\\TableRegistry::get(tableName) DasboardController并使用\\Cake\\Orm\\TableRegistry::get(tableName)

You could use PagesController also, but its more common to deliver static pages with it 您也可以使用PagesController ,但通过它提供静态页面更为常见

TableRegistry TableRegistry

first create a Dashboard Controller inside ./src/Controller/ with the filename DashboardsController.php . 首先在./src/Controller/内部创建一个文件名DashboardsController.php的Dashboard Controller。 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. 多数民众赞成从MVCC。

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. 除非在表和实体中具有特殊功能,否则除非需要PHPDoc声明,否则无需创建Table类或Entity类。 The Cake ORM takes over for you (default class). Cake ORM为您接管(默认类)。 So let's go over the M from MVC. 因此,让我们回顾一下MVC中的M。

$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 (eg $this->States->find('all'); ). 如果您已将模型加载到Controller中,则可以使用$this->States (例如$this->States->find('all'); )访问该模型。

Now you must save the result inside the view (from Controller: $this->set ('states', $states); ). 现在,您必须将结果保存在视图中(从Controller: $this->set ('states', $states); )。

The last part is the view (V) from MVC. 最后一部分是MVC的视图(V)。

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). ./src/Template/Dashboards/创建一个名称为index.ctp的文件(即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 (eg http://{url-to-your-cake-system}/dashboards/ ). 现在,您可以使用URL和控制器名称(例如, http://{url-to-your-cake-system}/dashboards/ )访问http://{url-to-your-cake-system}/dashboards/

Thats all. 就这样。 Cake use the Concept "convention over configuration". Cake使用“约定超越配置”的概念。 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. 因此,如果您遵循约定(文件结构,文件名,类名,表名等),Cake或多或少会自动为您完成所有操作。

PS In my opinion, there are only a few (and even less correct) approaches to using TableRegistry. PS:在我看来,只有几种(甚至更不正确)的方法来使用TableRegistry。 You should try to avoid it from the beginning. 您应该从一开始就避免它。

  1. create DashboradController with index method or at PageController add dashboard() method. 使用索引方法创建DashboradController或在PageController处添加dashboard()方法。
  2. create Dashborad\\index.ctp or Page\\dashboard.ctp 创建Dashborad \\ index.ctp或Page \\ dashboard.ctp
  3. create simple Cells for various data presentations (exmp: hourly, daily, weekly ,.. ) and include in your index.ctp / dashboard.ctp 为各种数据演示文稿创建简单的单元格 (例如:每小时,每天,每周,..),并将其包含在index.ctp / dashboard.ctp中
  4. route to your dashboard 路由到您的仪表板

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM