5: Add a record

Posted under » CakePHP on 11 Dec 2018

Continued from creating the article detail page article.

Start by creating an add() action in our controller which should now look like:

// src/Controller/ArticlesController.php
namespace App\Controller;
class ArticlesController extends AppController
 public function index()
  $articles = $this->Paginator->paginate($this->Articles->find());

public function view($slug = null)
  $article = $this->Articles->findBySlug($slug)->firstOrFail();

public function add()
  $article = $this->Articles->newEntity();
  if ($this->request->is('post')) {
  $article = $this->Articles->patchEntity($article, $this->request->getData());
  // Hardcoding the user_id is temporary, and will be removed later when we build authentication out.
  $article->user_id = 1;
  if ($this->Articles->save($article)) {
  $this->Flash->success(__('Your article has been saved.'));
  return $this->redirect(['action' => 'index']);
  $this->Flash->error(__('Unable to add your article.'));
  $this->set('article', $article);

You need to include the Flash component in any controller where you will use it. Often it makes sense to include it in your AppController.

Here’s what the add() action does:

Every CakePHP request includes a request object which is accessible using $this->request. The request object contains information regarding the request that was just received.

We use the Cake\Http\ServerRequest::is() method to check that the request is a HTTP POST request. Our POST data is available in $this->request->getData().

You can use the pr() or debug() functions to print it out if you want to see what it looks like.

To save our data, we first ‘marshal’ the POST data into an Article Entity. The Entity is then persisted using the ArticlesTable we created earlier.

After saving our new article we use FlashComponent’s success() method to set a message into the session. The success method is provided using PHP’s magic method features. Flash messages will be displayed on the next page after redirecting. In our layout we have <?= $this->Flash->render() ?> which displays flash messages and clears the corresponding session variable.

Finally, after saving is complete, we use Cake\Controller\Controller::redirect to send the user back to the articles list. The param ['action' => 'index'] translates to URL /articles i.e the index action of the ArticlesController.

You can refer to Cake\Routing\Router::url() function on the API to see the formats in which you can specify a URL for various CakePHP functions.

As usual we need to create the view.

Add Article

<¿php echo $this->Form->create($article); // Hard code the user for now. echo $this->Form->control('user_id', ['type' => 'hidden', 'value' => 1]); echo $this->Form->control('title'); echo $this->Form->control('body', ['rows' => '3']); echo $this->Form->button(__('Save Article')); echo $this->Form->end(); ?>

We use the FormHelper to generate the opening tag for an HTML form. Here’s the HTML that
$this->Form->create() generates:

<form method="post" action="/articles/add">

Because we called create() without a URL option, FormHelper assumes we want the form to submit back to the current action.

The $this->Form->control() method is used to create form elements of the same name.

The control() will output different form elements based on the model field specified, and use inflection to generate the label text. You can customize the label, the input or any other aspect of the form controls using options.

The $this->Form->end() call closes the form. Now let’s go back and update our src/Template/Articles/index.ctp view to include a new “Add Article” link. Before the <table>, add the following line:

<?= $this->Html->link('Add Article', ['action' => 'add']) ?>

If we were to save an Article right now, saving would fail as we are not creating a slug attribute. You can learn how to create a slug or skip this and learn to edit a record.

web security linux ubuntu GIT svn Raspberry apache mysql php drupal cake symfony javascript Ajax css