创建控制器,模型和查看CAKEPHP 3

时间:2016-09-02 02:35:57

标签: cakephp model controller frameworks

我刚刚开始使用cakephp,但我有很多疑问。

首先:

使用DB连接创建Controller,模型和视图时使用:

cake bake all "name" 

它工作正常,但当我的网页的某个部分不需要数据库时,我无法做任何事情,因为它无法正常工作。

所以我删除了这些文件:

/src/Controller/MyController.php
/src/Model/Entity/Model.php
/src/Model/Table/MyTable.php
/src/Template/MyTemplate/*
/tests/TestCase/*

并在routes.php中添加以下行

$routes->connect('/', ['controller' => 'Pages', 'action' => 'display', 'inicio']);

这样可行,但是当我这样做时,我无法在页面中看到我的布局

我在布局中有Bootstrap和我的页眉和页脚的连接,但现在我不能工作,现在我没有控制器来执行我的代码。

如何在没有数据库的情况下进行控制器模型和视图?

由于

1 个答案:

答案 0 :(得分:0)

  

并在routes.php中添加以下行

     

$routes->connect('/', ['controller' => 'Pages', 'action' => 'display', 'inicio']);

     

这样可行,但是当我这样做时,我无法看到我的布局   页

创建空的inicio.ctp,不要使用$this->layout = false;

  

如何在没有数据库的情况下进行控制器模型和视图?

使用命令cake bake controller Home或控制器名称烘焙新控制器。

内部生成的控制器使用空方法,无需调用模型/ ORM方法,或设置一些变量,如

public function index()
{
 // this is empty method
}

public function aboutUs()
{
 $about_us = [
   'title' => 'My title here',
   'description' => 'some text..'
 ];
 $this->set(compact('about_us'));
}

public function contact()
{
 // Add Modelless form here
}

无模型表单: http://book.cakephp.org/3.0/en/core-libraries/form.html

注意:您可以在PagesController中添加这些方法,只需路由,例如:

$routes->connect('/contact', ['controller' => 'Pages', 'action' => 'contact']);
相关问题