Intstall Cakephp on webserver

Check the definition of your table in the database, there should be something about a slug in there. That’s what the error is complaining about.

The slut field in the table remains empty when I want to add a new Article.

This slut is needed to complete the url (normally an ID) at least that’s what I assume , don’t know how cakephp handles it yet.

So I’m trying to figure out why the slut field stays empty, maybe I forgot some code in a file.

articles/view/first-post url completely First article
articles/view/ url Not completely Second (added article)

There’s only two places that value could come from. First, the edit form itself, that would seem most common to me. It’s clearly not there. Second, some bit of code calculates it based on something else. If you don’t have any code for doing this, then neither source is providing one, and hence not surprising that it’s empty. If you do have code for this, it’s maybe wrong in some way.

Thanks

I’m trying to figure out what code I’m missing

I need the full code from the src/Model/Table/ArticlesTable.php

it is not possible for me to insert the code correctly in src/Model/Table/ArticlesTable.php

READ:

Adding Simple Slug Generation

If we were to save an Article right now, saving would fail as we are not creating a slug attribute, and the column is NOT NULL. Slug values are typically a URL-safe version of an article’s title. We can use the beforeSave() callback of the ORM to populate our slug:

<?php // in src/Model/Table/ArticlesTable.php namespace App\Model\Table; use Cake\ORM\Table; // the Text class use Cake\Utility\Text; // the EventInterface class use Cake\Event\EventInterface; // Add the following method. public function beforeSave(EventInterface $event, $entity, $options) { if ($entity->isNew() && !$entity->slug) { $sluggedTitle = Text::slug($entity->title); // trim slug to maximum length defined in schema $entity->slug = substr($sluggedTitle, 0, 191); } }

This seems extremely straight-forward. In src/Model/Table/ArticlesTable.php, you should already have namespace App\Model\Table; and use Cake\ORM\Table;.

You’ll need to add

 // the Text class
use Cake\Utility\Text;
// the EventInterface class
use Cake\Event\EventInterface;

in the area where your other use statements already are.

Then add the following method:

public function beforeSave(EventInterface $event, $entity, $options) {
    if ($entity->isNew() && !$entity->slug) {
        $sluggedTitle = Text::slug($entity->title);
        // trim slug to maximum length defined in schema (this is assuming the column is defined as 192 characters)
        $entity->slug = substr($sluggedTitle, 0, 191);
    }
}

Where are you having issues with this?

Got errors need a compleet sample off ArticlesTable.php

App\Model\Table\ArticlesTable::beforeSave(): Argument #1 ($event) must be of type App\Model\Table\EventInterface, Cake\Event\Event given, called in /var/www/vhosts/handy5.nl/httpdocs/cakephp-4-4-10/vendor/cakephp/cakephp/src/Event/EventManager.php on line 309

<?php
// src/Model/Table/ArticlesTable.php
namespace App\Model\Table;

 
use Cake\ORM\Table;
 
class ArticlesTable extends Table
{
    public function initialize(array $config): void
    {
        $this->addBehavior('Timestamp');
    }	
	public function beforeSave(EventInterface $event, $entity, $options) {
    if ($entity->isNew() && !$entity->slug) {
        $sluggedTitle = Text::slug($entity->title);
        // trim slug to maximum length defined in schema (this is assuming the column is defined as 192 characters)
        $entity->slug = substr($sluggedTitle, 0, 191);
    }
}
}

You have not added the two new use lines.

Thanks forgotten…works fine now (add)

On the same way i put the validation code
Again error :face_with_raised_eyebrow:
syntax error, unexpected token “public”

use Cake\Validation\Validator;

// Add the following method.
public function validationDefault(Validator $validator): Validator
{
    $validator
        ->notEmptyString('title')
        ->minLength('title', 10)
        ->maxLength('title', 255)

        ->notEmptyString('body')
        ->minLength('body', 10);

    return $validator;
}

Did you add that method outside of the class? Or is there mabe a closing } missing from another class method?

Its inside the class

<?php
// src/Model/Table/ArticlesTable.php
namespace App\Model\Table;
use Cake\ORM\Table;
	 // the Text class
use Cake\Utility\Text;
 // the EventInterface class
use Cake\Event\EventInterface;
// // the Validator class
use Cake\Validation\Validator;

class ArticlesTable extends Table
{
    public function initialize(array $config): void
    {
        $this->addBehavior('Timestamp');
    }	
	// Add	
	public function beforeSave(EventInterface $event, $entity, $options) {
    if ($entity->isNew() && !$entity->slug) {
        $sluggedTitle = Text::slug($entity->title);
        // trim slug to maximum length defined in schema (this is assuming the column is defined as 192 characters)
        $entity->slug = substr($sluggedTitle, 0, 191);
    }	
	// validator
	public function 
	validationDefault(Validator $validator): Validator
{
    $validator
        ->notEmptyString('title')
        ->minLength('title', 10)
        ->maxLength('title', 255)

        ->notEmptyString('body')
        ->minLength('body', 10);

    return $validator;
}
}
}

edit
{ on wrong place

$entity->slug = substr($sluggedTitle, 0, 191);
    }	}

The validationDefault method needs to be parallel to all the other class methods, not inside of them

Your code shows, that validationDefault is inside of beforeSave

What kind of editor are you using? This should be easy to catch if you use a editor which understands PHP.

I use Dw
give a lot off errors with the code op Cakephp , so i ignore them generally

With “normal” php code Dw works fine with errors.

Youreditor is Atom?

Works fine now { brace wrong place
Hope this is correct

<?php
// src/Model/Table/ArticlesTable.php
namespace App\Model\Table;
use Cake\ORM\Table;
// the Text class
use Cake\Utility\Text;
// the EventInterface class
use Cake\Event\EventInterface;
// // the Validator class
use Cake\Validation\Validator;

class ArticlesTable extends Table
{

    public function initialize(array $config):
        void
        {
            $this->addBehavior('Timestamp');
        }
        // Add
        public function beforeSave(EventInterface $event, $entity, $options)
        {
            if ($entity->isNew() && !$entity->slug)
            {
                $sluggedTitle = Text::slug($entity->title);
                // trim slug to maximum length defined in schema (this is assuming the column is defined as 192 characters)
                $entity->slug = substr($sluggedTitle, 0, 191);
            }
        }
        // validator
        public function validationDefault(Validator $validator):
            Validator
            {
                $validator->notEmptyString('title')
                    ->minLength('title', 10)
                    ->maxLength('title', 255)

                    ->notEmptyString('body')
                    ->minLength('body', 10);

                return $validator;
            }

        }
        

Everyones prefered code style can be very subjective and different depending on what you are used to or what kind of code look you want to achieve. But the basic PHP syntax needs to be present and adhered to.

If you want you can use CakePHP’s per-configured code style tool via composer cs-fix

But I don’t know how the code style tool behaves if you give it invalid PHP code :sweat_smile:

I use PHPStorm since I got it provided from my company but VSCode + a few plugins is fine as well.

Thanks for help

PhpStorm- is too expensive for me.

Will also check out VSCode+ and others.

Is my code now correct?,a lot to learn.

Well compared to a Dreamweaver CS5 license its not that much different but you do you :wink:

:smiley:
True but I now have to spend my money on my house so it stays warm

The PHP syntax seems fine now but I personally would miss some things in the initialize() method which the GitHub - cakephp/bake: The Bake Command Plugin would automatically generate for you.

But I guess you want to go step by step with what the book offers you. So yes, this is fine.

I love to play with php
But i am basic so at this moment it’s not easy to understand everything yet and I’m not the youngest anymore…

One of the reason to use cakephp is
http://webzash.org
account software , but it is with Cakephp v2 and mabe it is very difficult, then I will make something else

The bake plugin i heard… , needed composer or?

well that changes quite a bit… let me sum up a whole bunch of information you need to know (in my opinion):

All I said above relates to current CakePHP 4 and the “modern” PHP development approach.

And CakePHP 3 and 4 are sooooo much different than what CakePHP 2 used to be.

Here are some facts you need to know:

  • CakePHP 2 has reached EOL (end of life) on June 15 2021 (see here), therefore everything based upon CakePHP 2 needs to be basically re-written because there is no real “upgrade path”.
  • Regarding composer: The whole modern PHP ecosystem is module based, therefore composer is basically a necessity and required by all modern PHP frameworks like Symfony, Laravel etc.
    So if you want to learn how modern CakePHP or modern PHP frameworks work in general then you need to learn how to use composer and what its meant to do.
  • This of course means you need to be aware of how OOP (object oriented programming) in PHP works and how basic PHP classes with methods, objects etc. work.