americandog1993の日記

プログラマー歴半年のメモ

CakePHP HerokuでHelloWorld

とりあえずHelloWorldまで。

環境

ローカル vagrant CentOS7
公開 Heroku

ローカル環境構築

VirtualBoxのCentOS7にCakePHPを導入する - Qiita
CakePHP環境構築。
HerokuToolbeltインストールにはrubyが必要 - americandog1993の日記
HerokuToolbeltも入れておく。

HelloController.php

手元の『CakePHP3入門』を見ながら書いた。

$ touch src/Controller/HelloController.php
// src/Controller/HelloController.php

<?php
namespace App\Controller

class HelloController extends AppController {
    public $name = 'Hello';
    public $autoRender = false;
    public function index(){
        echo "Hello World";
    }
}

routes.php

ルーティング設定。

// config/routes.php

Router::scope('/', function (RouteBuilder $routes) {
    /**
     * Here, we are connecting '/' (base path) to a controller called 'Pages',
     * its action called 'display', and we pass a param to select the view file
     * to use (in this case, src/Template/Pages/home.ctp)...
     */
//  $routes->connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']);
    $routes->connect('/', ['controller' => 'Hello', 'action' => 'index']);

Herokuデプロイ設定

Herokuデプロイの設定は以下を参考に。
CakePHP3をHerokuで動かす設定 - Qiita

$ heroku config:add CAKE_ENV="heroku"

config/bootstrap.php

// Load an environment local configuration file.
// You can use a file like app_local.php to provide local overrides to your
// shared configuration.
//Configure::load('app_local', 'default');
if (isset($_ENV['CAKE_ENV'])) {
    Configure::load('app_' . $_ENV['CAKE_ENV'], 'default');
}

Procfile

Webサーバとルートディレクトリの設定。

$ touch Procfile
$ vi Procfile
web: vendor/bin/heroku-php-apache2 webroot

デプロイ

$ git init
$ heroku create アプリ名
$ git add .
$ git commit -m "first commit"
$ git push heroku master