Mac搭建PHP Phalcon框架


個人博客: alex-my.xyz

1 說明

php與nginx安裝請參考“Mac搭建lnmp環境”
mysql請先安裝
部分路徑說明:

# 安裝php相關庫的路徑
~/Lib/php/
# 新項目路徑
~/WWW/
# nginx配置位置
/usr/local/etc/nginx
# php配置位置
/usr/local/etc/php/5.6

2 安裝phalcon

進入目錄~/Lib/php,並執行以下命令

git clone git://github.com/phalcon/cphalcon.git
cd cphalcon/build
sudo ./install

安裝之後在/usr/local/etc/php/5.6/php.ini 中添加:

extension=phalcon.so

重啓php-fpm

killall php-fpm
php-fpm -D

如果已經有搭建好的php服務器,可以運行以下代碼查確定是否安裝成功,如果沒有,則無需糾結該步驟。

// test.php
<?php
    $info = get_loaded_extensions();
    foreach ($info as $info0)
    {
        echo $info0.'<br/>';
    }
?>

在展示的頁面中出現phalcon則表示安裝成功。

3 安裝phalcon develop tool

進入目錄~/Lib/php,並執行以下命令

git clone git://github.com/phalcon/phalcon-devtools.git
cd phalcon-devtools/
sudo /bin/sh ./phalcon.sh

此時查看~/.bash_profile中會發現已添加了以下內容:

export PTOOLSPATH=/Users/alex/Lib/php/phalcon-devtools/
export PATH=$PATH:/Users/alex/Lib/php/phalcon-devtools

運行source ~/.bash_profile使其生效。

輸入phalcon –version查看信息:

➜  /Users/alex phalcon --version

Phalcon DevTools (3.0.3)

Environment::
  OS: Darwin alex.local 15.4.0 Darwin Kernel Version 15.4.0: Fri Feb 26 22:08:05 PST 2016; root:xnu-3248.40.184~3/RELEASE_X86_64 x86_64
  PHP Version: 5.6.24
  PHP SAPI: cli
  PHP Bin: /usr/local/Cellar/php56/5.6.24/bin/php
  PHP Extension Dir: /usr/local/Cellar/php56/5.6.24/lib/php/extensions/debug-non-zts-20131226
  PHP Bin Dir: /usr/local/Cellar/php56/5.6.24/bin
  Loaded PHP config: /usr/local/etc/php/5.6/php.ini
Versions::
  Phalcon DevTools Version: 3.0.3
  Phalcon Version: 3.0.2
  AdminLTE Version: 2.3.6

[Fri Dec 23 17:08:22 2016]  Script:  '/Users/alex/Lib/php/phalcon-devtools/phalcon.php'
/Users/alex/Lib/php/cphalcon/build/php5/64bits/phalcon.zep.c(23654) :  Freeing 0x1106784F0 (32 bytes), script=/Users/alex/Lib/php/phalcon-devtools/phalcon.php
=== Total 1 memory leaks detected ===

如果沒有效果,包括後面的步驟phalcon命令不存在等問題,可以在終端執行source ~/.bash_profile,重啓終端解決。

4 創建新項目manage

進入目錄~/WWW/中,輸入以下命令創建一個名爲manage的新項目。

phalcon create-project manage

終端輸出:

Phalcon DevTools (3.0.3)

    Success: Controller "index" was successfully created.  
/Users/alex/WWW/manage/app/controllers/IndexController.php

    Success: Project "manage" was successfully created.

[Fri Dec 23 14:59:30 2016]  Script:  '/Users/alex/Lib/php/phalcon-devtools/phalcon.php'
/Users/alex/Lib/php/cphalcon/build/php5/64bits/phalcon.zep.c(23654) :  Freeing 0x10757A680 (32 bytes), script=/Users/alex/Lib/php/phalcon-devtools/phalcon.php
=== Total 1 memory leaks detected ===

5 創建manage.conf

在nginx下添加配置文件,用以運行manage。
進入/usr/local/etc/nginx/sites-enabled目錄,創建manage.conf文件,內容:

server {
    listen      80;
    server_name manage.com;
    # 注意指向的是manage/public
    set         $root_path '/Users/alex/WWW/manage/public/';
    root        $root_path;

    index index.php index.html index.htm;

    try_files $uri $uri/ @rewrite;

    location @rewrite {
        rewrite ^/(.*)$ /index.php?_url=/$1;
    }

    location ~ \.php {
        # try_files    $uri =404;

        fastcgi_index  /index.php;
        fastcgi_pass   127.0.0.1:9000;

        include fastcgi_params;
        fastcgi_split_path_info       ^(.+\.php)(/.+)$;
        fastcgi_param PATH_INFO       $fastcgi_path_info;
        fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
        root $root_path;
    }

    location ~ /\.ht {
        deny all;
    }
}

在/etc/hosts中添加manage.com記錄

sudo vim /etc/hosts
# 添加
127.0.0.1   manage.com

重載nginx

nginx -s reload

在瀏覽器中輸入 manage.com

Congratulations!
You're now flying with Phalcon. Great things are about to happen!

This page is located at views/index/index.volt

6 phpstorm配置phalcon develop tool

用phpstorm打開manage項目。
打開app/controllers/ControllerBase.php,發現以下代碼顯示紅色錯誤(Mvc):

use Phalcon\Mvc\Controller;

現在將Phalcon Libray添加進來,消除錯誤,添加語法提示。

External Libraries 右鍵選擇 configure php include paths
interpreter 記得選擇 /usr/local/bin/php(沒有就自己建一個), /usr/bin/php是系統自帶的php5.5(或者重建軟鏈接)
在include path 點擊 + 號
選擇 /Users/alex/Lib/php/phalcon-devtools/ide/stubs/Phalcon

現在紅色錯誤消除了。也增加了語法提示。

7 phpstorm配置命令行工具

phpstorm – Prefreners – Tools – Command Line Tool Support 選擇 +號
內容按照以下填寫:

Choose tool: Custom tool
Visibility: global

Tool path: /Users/alex/Lib/php/phalcon-devtools/ide/phpstorm/phalcon.sh
Alias: phalcon-tool
Description: Phalcon Developer Tools

8 phpstorm測試命令行工具

phpstorm – tool – run command
在ide底部出現輸入框和顯示框.Command Line Tools Console,輸入以下命令

phalcon-tool create-controller --name test

命令中的phalcon-tool就是第7步 Alias: phalcon-tool
顯示框顯示:

> /Users/alex/Lib/php/phalcon-devtools/ide/phpstorm/phalcon.sh create-controller --name test

Phalcon DevTools (3.0.3)

[Fri Dec 23 16:14:13 2016]  Script:  '/Users/alex/Lib/php/phalcon-devtools/phalcon.php'
/Users/alex/Lib/php/cphalcon/build/php5/64bits/phalcon.zep.c(23654) :  Freeing 0x10FE2ED60 (32 bytes), script=/Users/alex/Lib/php/phalcon-devtools/phalcon.php
=== Total 1 memory leaks detected ===

  Success: Controller "test" was successfully created.  

/Users/alex/WWW/manage/app/controllers/TestController.php

Process finished with exit code 0 at 16:14:13.
Execution time: 472 ms.

在app/controllers中出現一個新文件TestController.php

9 修改數據庫配置

在本地創建數據庫manage

create database manage;

修改 app/config/config.php中的database

'database' => [
    'adapter'     => 'Mysql',
    'host'        => 'localhost',
    'username'    => 'root',
    'password'    => '123456',
    'dbname'      => 'manage',
    'charset'     => 'utf8',
],

10 數據庫中的表生成模型

在數據庫manage創建表user;

DROP TABLE IF EXISTS `user`;

CREATE TABLE `user` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `username` varchar(200) NOT NULL DEFAULT '' COMMENT '登錄名',
  `password` varchar(32) NOT NULL COMMENT '登錄密碼',
  `salt` char(6) NOT NULL COMMENT '校驗碼',
  PRIMARY KEY (`id`),
  UNIQUE KEY `m_username` (`username`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT COMMENT='成員表';

在命令行輸入:

phalcon-tool create-model --name user

顯示框顯示:

> /Users/alex/Lib/php/phalcon-devtools/ide/phpstorm/phalcon.sh create-model --name user

Phalcon DevTools (3.0.3)

[Fri Dec 23 16:21:46 2016]  Script:  '/Users/alex/Lib/php/phalcon-devtools/phalcon.php'

/Users/alex/Lib/php/cphalcon/build/php5/64bits/phalcon.zep.c(48476) :  Freeing 0x1051B07B8 (32 bytes), script=/Users/alex/Lib/php/phalcon-devtools/phalcon.php
  Success: Model "User" was successfully created.  
[Fri Dec 23 16:21:46 2016]  Script:  '/Users/alex/Lib/php/phalcon-devtools/phalcon.php'

/Users/alex/Lib/php/cphalcon/build/php5/64bits/phalcon.zep.c(23654) :  Freeing 0x10480AD60 (32 bytes), script=/Users/alex/Lib/php/phalcon-devtools/phalcon.php
=== Total 2 memory leaks detected ===

在app/models中,會自動出現一個User.php。如果沒出現,需要稍等一小段時間。

11 參考

發佈了118 篇原創文章 · 獲贊 68 · 訪問量 57萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章