CRUD operation in CakePHP 4.0

The CakePHP team is thrilled to announce the immediate availability of 4.0.0.CakePHP 4 is coming with breaking changes. This new release is targeting the newest versions of PHP especially 7.2. CakePHP 4 supports FormHelper now generates HTML5 validation messages and DateTime inputs, Middleware for CSP headers, Form tampering prevention, and HTTPS enforcement.

CakePHP has a few system requirements:

HTTP Server. For example Apache. Having mod_rewrite is preferred, but by no means required. You can also use nginx, or Microsoft IIS if you prefer.
Minimum PHP 7.2
mbstring PHP extension
intl PHP extension
simplexml PHP extension
PDO PHP extension

Installing CakePHP 4

Before starting you should make sure that your PHP version is up to date.You should have PHP 7.2 (CLI) or higher.

Installing Composer

CakePHP uses Composer, a dependency management tool, as the officially supported method for installation.

Create a CakePHP Project

The first screen after installation

Now make database cake_cms and make one table articles.

Replace the values in the Datasources.default array in your config/app_local.php file.

Now let’s make the heart of CakePHP. They enable us to read and modify our data. They allow us to build relations between our data, validate data, and apply application rules. The file we’ll be creating will be saved to src/Model/Table/ArticlesTable.php.

We’ve attached the Timestamp behavior which will automatically populate the created and modified columns of our table. We’ll also create an Entity class for our Articles. Entities represent a single record in the database. Our entity will be saved to src/Model/Entity/Article.php.

Now we need controllers. Controllers in CakePHP handle HTTP requests and execute business logic contained in model methods, to prepare the response. Place ArticlesController.php inside the src/Controller directory.

Now create a listing page. Create templates/Articles/index.php.

Now check http://localhost/cms/articles/index.

Now create view() action in the ArticlesController.php.

Now make the view action template file. Make templates/Articles/view.php.

Let’s add the article.

To show a successful message we are using Flash Component. Add following code to ArticlesController.php.

Create add() action view in File: templates/Articles/add.php.

Now add simple slug generation.

Now add edit action.

Create edit() action template.

Update index.php view file.

Add a validation rule to ArticlesTable.php.

Add delete() action.

Update index.php.

Developer by profession . Youtube channel :