Phalcon DevTools (4.0.1)でプロジェクト作成する
「Windows10にPhalcon3をインストールする」でPhalcon DevToolsをglobalインストールしているので、Phalconプロジェクトが作成できます。
cd C:\>phptest phalcon project sampleproject
これでsampleprojectディレクトリが作成されます。このディレクトリがプロジェクトルートです。
.phalconディレクトリができているのですが、このプロジェクトルートからコマンドを実行しないとモデル(エンティティ)など作成することができません。
DB設定(PostgreSQL)
モデルをCLIで作成するときにapp/config/config.php
ファイルでDB接続情報を適宜記述しておく必要があります。
この接続情報が正しくないと、モデルを自動生成することができません。
ここではPostgreSQLに接続をしてみます。app/config/config.php
のDB接続情報を修正します。
<?php defined('BASE_PATH') || define('BASE_PATH', getenv('BASE_PATH') ?: realpath(dirname(__FILE__) . '/../..')); defined('APP_PATH') || define('APP_PATH', BASE_PATH . '/app'); return new \Phalcon\Config([ 'database' => [ 'adapter' => 'Postgresql', 'host' => 'localhost', 'port' => 5432, 'username' => 'postgres', 'password' => 'xxxxxxxx', 'dbname' => 'postgres', ], 'application' => [ 'appDir' => APP_PATH . '/', 'controllersDir' => APP_PATH . '/controllers/', 'modelsDir' => APP_PATH . '/models/', 'migrationsDir' => APP_PATH . '/migrations/', 'viewsDir' => APP_PATH . '/views/', 'pluginsDir' => APP_PATH . '/plugins/', 'libraryDir' => APP_PATH . '/library/', 'cacheDir' => BASE_PATH . '/cache/', 'baseUri' => '/', ] ]);
モデル作成
empuserというテーブルが存在するとします。その状態でモデルを作成します。
C:\>phalcon model empuser Phalcon DevTools (4.0.1) Success: Model "Empuser" was successfully created.
これで作成に成功しました。app/models/Empuser.php
が作成されているはずです。
ERROR: could not find driver
とエラーが出る場合は、php.iniを編集する必要があります。
;extension=pdo_pgsql ↓ extension=pdo_pgsql
色々オプションがあるようですが、詳細は以下にモデルのオプション説明の記載があります。

ちなみにemp_userテーブルの場合は、EmpUser.phpファイルが作成されます。
了。
ComposerコマンドからPhalconプロジェクトを作成する
今度はComposerコマンドでPhalconプロジェクトを作成してみます。
composer init -qでcomposer.json
が作成されます。
composer require --dev phalcon/devtools
コマンドでcomposer.jsonが以下のようになります。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"require": {}, | |
"require-dev": { | |
"phalcon/devtools": "^4.0" | |
} | |
} |
vendor\bin\phalcon.bat project sampleproject
コマンドでsampleprojectを作成します。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
c:\> cd sampleproject | |
c:\> ..\vendor\bin\phalcon.bat serve | |
Phalcon DevTools (4.0.1) | |
Preparing Development Server | |
Host: 0.0.0.0 | |
Port: 8000 | |
Base: .htrouter.php | |
Document Root: public | |
Starting Server with C:\php\php.exe -S 0.0.0.0:8000 -t .htrouter.php -t public | |
[Wed Mar 18 21:34:37 2020] PHP 7.4.3 Development Server (http://0.0.0.0:8000) started |
phalcon.bat serve
でビルトインサーバを起動してくれるそうです。
これでhttp://localhost:8000/にアクセスするとウェルカムページが表示されます。
Phalcon Webtoolsを使う
http://localhost:8000/webtools.php/にアクセスするとPhalcon Webtoolsというページが開きます。
このページからコントローラやモデルの作成(DB接続情報設定している必要あり)ができます。作成したファイルの編集もこちらで行えます。
ここで、test_tblのモデルを作成しておきます。作成に失敗する場合はDB接続情報をよく確認してください。(文字コードは削除する)



URIに決まりがある
PhalconはどうもURIに決まりがあるようです。チュートリアルをわからないながら読み進めています。
router.phpにルートを追加します。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$router = $di->getRouter(); | |
// Define your routes here | |
$router->add( | |
'/aaa', | |
['controller' => 'index', 'action' => 'test'] | |
); | |
$router->handle($_SERVER['REQUEST_URI']); |
第一引数にURI、第二引数のindexがIndexControllerを指し、actionのtestがIndexControllerのtestActionメソッドを指すようです。
IndexController.phpにtestActionメソッドを追加し、以下のようにしました。
※PostgreSQLにtest_tblがある前提です。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
class IndexController extends ControllerBase | |
{ | |
public function indexAction() | |
{ | |
} | |
public function testAction() | |
{ | |
$region = TestTbl::findFirst(1); | |
echo $region->id + ' : ' + $region->name; | |
} | |
} | |
これで、http://localhost:8000/aaaにアクセスすると以下のように1行取得した結果が表示されます。


KHI入社して退社。今はCONFRAGEで正社員です。関西で140-170/80~120万から受け付けております^^
得意技はJS(ES6),Java,AWSの大体のリソースです
コメントはやさしくお願いいたします^^
座右の銘は、「狭き門より入れ」「願わくは、我に七難八苦を与えたまえ」です^^
資格:少額短期保険募集人,FP3級
コメント