Ссылка на урок: https://www.youtube.com/watch?v=pCB5ShwtHdw&t=472s
Укажем нашу БД:
config/app.php
Строки 'Datasources' => [
'default'
'default'
src/Controller
создаем файл PostsController.php
Пишем:
<?php
namespace
App\Controller;
use
App\Controller\AppController;
class
PostsController extends AppController {
public
function index() {
}
}
В src/Template
создаем папку Posts
и в ней файл index.ctp
В
config/routs.php меняем
$routes->connect('/',
['controller' => 'Posts', 'action' => 'index', 'home']);
Идем на
страницу: http://bootswatch.com/paper/#navbar
И скачиваем там код
навигационном панели.
Далее в
src/Tamplate/Layout/default.ctp
вставляем эту навигационную
панель.
Также подключим свои
стили:
<?php echo
$this->Html->css('bootstrap.css') ?>
<?php echo $this->Html->script('jquery-3.2.1') ?>
<?php echo $this->Html->script('bootstrap.css') ?>
<?php echo $this->Html->script('jquery-3.2.1') ?>
<?php echo $this->Html->script('bootstrap.css') ?>
Далее в
src/Template/Posts/index.ctp
вставим код таблицы с
сайта: http://bootswatch.com/paper/#tables
<div class="row"></div>
<table
class="table table-striped table-hover ">
<thead>
<tr>
<th>Title</th>
<th>Descripion</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr>
<td>Column
content</td>
<td>Column
content</td>
<td>Column
content</td>
</tr>
</tbody>
</table>
</div>
Идем в
PostsController.php и
пишем там:
public function index() {
$this->set('posts',
$this->Posts->find('all'));
}
Далее в
src/Template/Posts/index.ctp
вставим код:
<div class="row"></div>
<h2>View all Posts</h2>
<?php echo $this->Html->link('ADD
POST', ['action' => 'add'], ['class' => 'btn btn-primary']); ?>
<table
class="table table-striped table-hover ">
<thead>
<tr>
<th>Title</th>
<th>Descripion</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
if(!empty($posts)) : ?>
<?php
foreach($posts as $post) : ?>
<tr>
<td><?php
echo $post->title; ?></td>
<td><?php
echo $post->description; ?></td>
<td></td>
</tr>
<?php
endforeach; ?>
<?php
else: ?>
<td>No
record found</td>
<?php
endif; ?>
</tbody>
</table>
</div>
Мы добавили кнопку для
добавления статьи.
Идем в
PostsController.php и
пишем там:
public function
add() {
}
В
src/Template/Posts создадим
файл add.ctp
<fieldset>
<legend>Add
Post</legend>
<div
class="form-group">
<div
class="col-lg-10">
<?php
echo $this->Form->input('title', ['class' => 'form-control',
'Placeholder' => 'Title']); ?>
</div>
</div>
<div
class="form-group">
<div
class="col-lg-10">
<?php
echo $this->Form->input('description', ['class' =>
'form-control', 'Placeholder' => 'Description']); ?>
</div>
</div>
<div
class="form-group">
<div
class="col-lg-10 col-lg-offset-2">
<?php
echo $this->Form->button(__('Add New Post'), ['class' =>
'btn btn-primary']); ?>
<?php
echo $this->Html->link('Back', ['action' => 'index'],
['class' => 'btn btn-default']); ?>
</div>
</div>
</fieldset>
Идем в
PostsController.php и
пишем там:
public function add() {
$post =
$this->Posts->newEntity();
if($this->request->is('post')) {
//
валидация
//
https://book.cakephp.org/3.0/en/orm/saving-data.html#validation-and-patchentity
$post
= $this->Posts->patchEntity($post,
$this->request->getData());
if($this->Posts->save($post))
{
$this->Flash->success('Post added
successfully', ['key' => 'message']);
return
$this->redirect(['action' =>
'index']);
}
$this->Flash->error(__('Unable to add your
post'));
}
$this->set('post',
$post);
}
В
add.ctp открываем
и закрываем форму:
<?php echo
$this->Form->create($post); ?>
<fieldset>
<legend>Add
Post</legend>
<div
class="form-group">
<div
class="col-lg-10">
<?php
echo $this->Form->input('title', ['class' => 'form-control',
'Placeholder' => 'Title']); ?>
</div>
</div>
<div
class="form-group">
<div
class="col-lg-10">
<?php
echo $this->Form->input('description', ['class' =>
'form-control', 'Placeholder' => 'Description']); ?>
</div>
</div>
<div
class="form-group">
<div
class="col-lg-10 col-lg-offset-2">
<?php
echo $this->Form->button(__('Add New Post'), ['class' =>
'btn btn-primary']); ?>
<?php
echo $this->Html->link('Back', ['action' => 'index'],
['class' => 'btn btn-default']); ?>
</div>
</div>
</fieldset>
<?php echo
$this->Form->end(); ?>
В
index.ctp добавляем
флешку:
<?php echo
$this->Flash->render('message'); ?>
Делаем
просмотр и удаление статьи:
<div
class="row"></div>
<?php
echo $this->Flash->render('message'); ?>
<h2>View
all Posts</h2>
<?php
echo $this->Html->link('ADD POST', ['action' => 'add'],
['class' => 'btn btn-primary']); ?>
<table
class="table table-striped table-hover ">
<thead>
<tr>
<th>Title</th>
<th>Descripion</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
if(!empty($posts)) : ?>
<?php
foreach($posts as $post) : ?>
<tr>
<td><?php
echo $post->title; ?></td>
<td><?php
echo $post->description; ?></td>
<td>
<?php
echo $this->html->link('View', ['action' => 'view',
$post->id], ['class' => 'btn btn-primary'])?>
<?php
echo $this->html->link('Edit', ['action' => 'edit',
$post->id], ['class' => 'btn btn-success'])?>
<?php
echo
$this->Form->postlink(
'Delete',
['action'
=> 'delete', $post->id],
['class'
=> 'btn btn-danger'],
['confirm'
=> 'Are you sure to delete?']
)
?>
</td>
</tr>
<?php
endforeach; ?>
<?php
else: ?>
<td>No
record found</td>
<?php
endif; ?>
</tbody>
</table>
</div>
Идем
в PostsController.php и
пишем там:
public function
view($id = null) {
$posts
= $this->Posts->get($id); // вибираем по id
$this->set('post',
$posts);
}
Создаем
файл view.ctp
<p><?php
echo $post->title; ?></p>
<p><?php
echo $post->description; ?></p>
<?php
echo $this->html->link('Back', ['action' => 'index'],
['class' => 'btn btn-primary']); ?>
Идем
в PostsController.php и
пишем там:
public
function edit($id = null) {
$post
= $this->Posts->get($id);
if
($this->request->is(['post', 'put'])) {
$this->Posts->patchEntity($post,
$this->request->getData());
if
($this->Posts->save($post)) {
$this->Flash->success(__('Ваша
статья была обновлена.'));
return
$this->redirect(['action' => 'index']);
}
$this->Flash->error(__('Ошибка
обновления вашей статьи.'));
}
$this->set('post',
$post);
}
Создаем
файл edit.ctp
<?php echo
$this->Form->create($post); ?>
<fieldset>
<legend>Add
Post</legend>
<div
class="form-group">
<div
class="col-lg-10">
<?php
echo $this->Form->input('title', ['class' => 'form-control',
'Placeholder' => 'Title']); ?>
</div>
</div>
<div
class="form-group">
<div
class="col-lg-10">
<?php
echo $this->Form->input('description', ['class' =>
'form-control', 'Placeholder' => 'Description']); ?>
</div>
</div>
<div
class="form-group">
<div
class="col-lg-10 col-lg-offset-2">
<?php
echo $this->Form->button(__('Add New Post'), ['class' =>
'btn btn-primary']); ?>
<?php
echo $this->Html->link('Back', ['action' => 'index'],
['class' => 'btn btn-default']); ?>
</div>
</div>
</fieldset>
<?php echo
$this->Form->end(); ?>
Идем в
PostsController.php и пишем там:
public
function delete($id = null) {
$this->request->allowMethod(['post',
'delete']);
$post
= $this->Posts->get($id);
if($this->Posts->delete($post))
{
$this->Flash->success('Post
deleted successfully', ['key' => 'message']);
return
$this->redirect(['action' => 'index']);
}
}
Комментариев нет:
Отправить комментарий