Giter Club home page Giter Club logo

cakephp-schoolofnet's Introduction

CakePHP

1. Instalando o CakePHP

1.1 PHP

É necessário habilitar no PHP as seguintes extensões:

  • ext-intl
  • ext-mbstring

1.2 Instalando o CakePHP com o Composer (global)

$ composer create-project --prefer-dist cakephp/app:^3.5 app

2. Database

2.1 Criar o Banco de dados

Para criar o banco de dados, podemos executar o seguinte comando:

$ CREATE DATABASE cakephp_iniciante_34

2.2 Configurando o database

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',
    ...

2.3 Criando a tabela users

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

3. Criando um CRUD completo para Users

Usando o comando abaixo podemos gerar o código do CRUD completo.

$ cake bake all users

3.2 Criando a table Pages

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

3.2 Em Model (Entity)

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);
    }
}

Controlles e Rotas

Criando View

Form Helper

Layout

Trabalhando com layout

Adicionando um Registro

cakephp-schoolofnet's People

Contributors

sialka avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.