É necessário habilitar no PHP as seguintes extensões:
- ext-intl
- ext-mbstring
$ composer create-project --prefer-dist cakephp/app:^3.5 app
Para criar o banco de dados, podemos executar o seguinte comando:
$ CREATE DATABASE cakephp_iniciante_34
Para o cake reconhecer o database, no arquivo app\config\app.php em Datasources informe o usuário, senha e nome do banco de dados :
...
'username' => 'root',
'password' => '',
'database' => 'cakephp_iniciante_34',
...
Para criar uma tabela podemos usar o migrations.
$ cake migrations create CreateUsers
Ao usar o comando acima é criado na pasta config é uma sub pasta Migrations com um arquivo xxxxx_CreateUse.php
<?php
use Migrations\AbstractMigration;
class CreateUsers extends AbstractMigration {
/**
* Change Method.
*
* More information on this method is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-change-method
* @return void
*/
public function change() {
$table = $this->table('users');
$table->addColumn('username', 'string');
$table->addColumn('password', 'string', [
'limit' => 200
]);
$table->addColumn('active', 'boolean', [
'default' => 0,
'null' => true
]);
$table->addColumn('created', 'timestamp', [
'default' => 'CURRENT_TIMESTAMP'
]);
$table->addColumn('modified', 'datetime', [
'default' => null,
'null' => true
]);
$table->create();
}
}
Agora para criar a tabela execute o comando abaixo:
$ cake migrations migrate
Após gerar a table podemos conferir usando o comando:
$ cake bake all
Usando o comando abaixo podemos gerar o código do CRUD completo.
$ cake bake all users
Criando uma table para paginação.
$ cake bake migration CreatePages
Ao usar o comando acima é criado na pasta config é uma sub pasta Migrations com um arquivo xxxxx_CreatePages.php
Agora vamos informar os campos da tabela.
<?php
use Migrations\AbstractMigration;
class CreatePages extends AbstractMigration {
/**
* Change Method.
*
* More information on this method is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-change-method
* @return void
*/
public function change() {
$table = $this->table('pages');
$table->addColumn('title', 'string');
$table->addColumn('url', 'string');
$table->addColumn('body', 'text');
$table->addColumn('created', 'timestamp', [
'default' => 'CURRENT_TIMESTAMP'
]);
$table->addColumn('modified', 'datetime', [
'default' => null,
'null' => true
]);
$table->create();
}
}
Agora para criar a tabela execute o comando abaixo:
$ cake migrations migrate
Agora que já temos a tabela, vamos criar o CRUD.
$ cake bake model Pages
Vamos criar umas regras para os campos url e title.
<?php
namespace App\Model\Entity;
use Cake\ORM\Entity;
use Cake\Utility\Text;
...
class Page extends Entity
{
...
protected function _setUrl($url){
$url = Text::slug($url);
if(empty($url)){
$url = Text::slug($this->_properties['title']);
}
return $url;
}
protected function _getTitle($title){
$title = strtolower($title);
return ucwords($title);
}
protected function _getTitleUrl($param) {
$title = strtolower($title);
return ucwords($title);
}
}