在CakePHP中编写内容类型,通常涉及到以下几个步骤:
以下是一个简单的示例,展示了如何在CakePHP中编写内容类型:
ContentType.php
:namespace App\Model;
use Cake\ORM\Entity;
use Cake\ORM\Table;
class ContentTypeTable extends Table
{
public function initialize(array $config): void
{
$this->setTable('content_types');
$this->setDisplayField('name');
$this->setPrimaryKey('id');
$this->addBehavior('Timestamp');
}
}
CREATE TABLE content_types (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
description TEXT,
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
modified TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
ContentTypesController.php
:namespace App\Controller;
use App\Controller\AppController;
use App\Model\ContentType;
class ContentTypesController extends AppController
{
public function index()
{
$contentTypes = $this->ContentTypes->find('all');
$this->set(compact('contentTypes'));
}
public function view($id)
{
$contentType = $this->ContentTypes->get($id);
$this->set(compact('contentType'));
}
public function add()
{
$contentType = $this->ContentTypes->newEntity();
if ($this->request->is('post')) {
$contentType = $this->ContentTypes->patchEntity($contentType, $this->request->getData());
if ($this->ContentTypes->save($contentType)) {
$this->Flash->success(__('The content type has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The content type could not be saved. Please, try again.'));
}
$this->set(compact('contentType'));
}
public function edit($id)
{
$contentType = $this->ContentTypes->get($id);
if ($this->request->is(['patch', 'post', 'put'])) {
$contentType = $this->ContentTypes->patchEntity($contentType, $this->request->getData());
if ($this->ContentTypes->save($contentType)) {
$this->Flash->success(__('The content type has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The content type could not be saved. Please, try again.'));
}
$this->set(compact('contentType'));
}
public function delete($id)
{
$this->request->allowMethod(['post', 'delete']);
$contentType = $this->ContentTypes->get($id);
if ($this->ContentTypes->delete($contentType)) {
$this->Flash->success(__('The content type has been deleted.'));
} else {
$this->Flash->error(__('The content type could not be deleted. Please, try again.'));
}
return $this->redirect(['action' => 'index']);
}
}
index.ctp
:<h2>Content Types</h2><table>
<tr>
<th>Name</th>
<th>Description</th>
<th>Actions</th>
</tr>
<?php foreach ($contentTypes as $contentType): ?>
<tr>
<td><?= h($contentType->name) ?></td>
<td><?= h($contentType->description) ?></td>
<td>
<a href="<?= $this->Url->build(['controller' => 'ContentTypes', 'action' => 'view', $contentType->id]) ?>">View</a>
<a href="<?= $this->Url->build(['controller' => 'ContentTypes', 'action' => 'edit', $contentType->id]) ?>">Edit</a>
<a href="<?= $this->Url->build(['controller' => 'ContentTypes', 'action' => 'delete', $contentType->id]) ?>">Delete</a>
</td>
</tr>
<?php endforeach; ?>
</table>
<a href="<?= $this->Url->build(['controller' => 'ContentTypes', 'action' => 'add']) ?>">Add Content Type</a>
config/routes.php
中添加以下内容:$routes->connect('/content-types', ['controller' => 'ContentTypes', 'action' => 'index']);
$routes->connect('/content-types/:action/*', ['controller' => 'ContentTypes']);
现在,你已经成功地在CakePHP中编写了一个内容类型的应用程序。你可以通过访问/content-types
来查看和管理内容类型。
领取专属 10元无门槛券
手把手带您无忧上云