(입문) CakePHP2로 database 연결

8076 단어 CakePHP
  • tukiyo3/php5.6-apache - Docker Hub

  • 절차


    git clone https://github.com/tukiyo/docker-php5.6-apache
    cd docker-php5.6-apache
    
    sh files/get_cakephp.sh
    chmod -R 777 app/tmp
    docker-compose up
    

    docker-compose.yml
    web:
      image: tukiyo3/php5.6-apache
      volumes:
          - ./files/apache.conf:/etc/apache2/sites-available/000-default.conf
          - ./files/php.ini:/usr/local/etc/php/php.ini
          - ./cakephp/:/var/www/html
          - ./logs/:/var/log/apache2/
      ports:
          - "8080:80"
      #restart: always
    



    아래 CakePHP2 메모



    초기 설정


  • app/Config/core.php
  • Security.salt
  • Security.cipherSeed

  • app/Config/database.php
  • app/Config/routes.php

  • 수업 추가


  • app/Model/Phone.php (db)
  • app/Controller/PhonesController.php ($this->Phone->find라든지)
  • app/View/Phones/index.ctp (view)

  • app/Model/Phone.php
    class Phone extends AppModel {
        var $useTable = 'phone';
        public $validate = array(
            'number' => array(
                'rule' => 'notEmpty'
            ),
        );
    }
    

    app/Controller/PhonesController.php
    class PhonesController extends AppController {
        public $helpers = array('Html', 'Form');
        public function index() {
            $this->set('phones', $this->Phone->find('all'));
        }
    }
    

    app/View/Phones/index.ctp
    <table>
        <tr>
            <th>Id</th>
            <th>Number</th>
            <th>Created</th>
        </tr>
    
        <?php foreach ($phones as $p): ?>
            <tr>
                <td><?php echo $p['Phone']['id']; ?></td>
                <td><?php echo $p['Phone']['number']; ?></td>
                <td><?php echo $p['Phone']['date']; ?></td>
            </tr>
        <?php endforeach; ?>
        <?php unset($g); ?>
    </table>
    

    app/Config/routes.php
    - Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
    + Router::connect('/', array('controller' => 'phones', 'action' => 'index'));
    

    편리한 기능


  • alt + shift + f : 소스 성형
  • table명을 복수형이 아니고 임의의 것을 지정한다

  • app/Model/Phone.php(예)
    class Phone extends AppModel {
        var $useTable = 'phone';
    }
    

    좋은 웹페이지 즐겨찾기