『壹』 如何在php上安裝phalcon
這是正耐猜 phpinfo();
System Windows NT ADMIN-PC 6.1 build 7601 (Windows 7 Ultimate Edition Service Pack 1) i586
Build Date Jun 5 2013 20:58:05
Compiler MSVC9 (Visual C++ 2008)
Architecture x86
Configure Command cscript /nologo configure.js "--enable-snapshot-build" "--disable-isapi" "舉型--enable-debug-pack" "畝友--without-mssql" "--without-pdo-mssql" "--witho
『貳』 怎麼在 Phalcon 框架里管理使用cookie
Phalcon\Http\Response\Cookies(不是Phalcon\Http\Cookie)是一個為響應設置cookie的容器,類似於PHP中的setcookie()函數。
這個容器中設置的每個cookie都是一個Phalcon\Http\Cookie類實例,容器的send()方法實際是添加響應頭Set-Cookie指令。
在Phalcon中,最終會調用:
Phalcon\Http\Response->sendHeaders()
Phalcon\Http\Response->sendCookies()
這里的Phalcon\Http\Response->sendCookies()內部實際調用Phalcon\Http\Response\Cookies的send()方法。
如下例子:
//在控制器方法中添加如下代碼:
$this->cookies->useEncryption(false);
$this->cookies->set("test-cookie","ffffffffffffffff");
$this->cookies->send();
return;
//響應頭
Connection Keep-Alive
Content-Length 10
Content-Type text/html
Date Mon, 08 Sep 2014 04:20:17 GMT
Keep-Alive timeout=5, max=100
Server Apache/2.2.27 (Unix) PHP/5.5.15
Set-Cookie test-cookie=ffffffffffffffff; path=/; httponly test-cookie=ffffffffffffffff; path=/; httponly
代碼中設置了一個cookie,然後調用容器的send()方法,在客戶端查看響應頭,發現Set-Cookie指令中test-cookie設置了兩次,這個說明Phalcon在響應時也調用了一次send()方法,這個就可以證明Phalcon在響應時調用的sendCookies()方法內部就是調用cookie容器的send()方法。
Phalcon\Http\Response有三個和cookie有關的方法:
Phalcon\Http\Response::getCookies();
Phalcon\Http\Response::setCookies($cookies);
Phalcon\Http\Response::sendCookies();
分別獲取和設置Response的Cookies容器(\Phalcon\Http\Response\CookiesInterface 實例),sendCookies()是調用容器的send()方法發喊穗送cookie。
還有一個問題是,如何獲取來自請求的鄭野卜cookie?你可以發現,在Phalcon\Http\Request中並脊派不存在類似getCookies()方法,難道要使用$_COOKIE全局數組?Phalcon文檔對於這個並沒有提及。使用如下例子:
//使用的請求頭信息
Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding gzip, deflate
Accept-Language zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3
Cache-Control max-age=0
Connection keep-alive
Cookie test-cookie=only+for+test+cookie.
Host 192.168.1.168
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0
//控制器代碼
$this->cookies->set("hello-cookie","hello cccccc kie.");
if($this->cookies->has("test-cookie")){
echo $this->cookies->get("test-cookie")->getValue();
}
print_r($this->cookies);
$this->view->disable();
//輸出
only for test cookie.
Phalcon\Http\Response\Cookies Object
(
[_dependencyInjector:protected] => Phalcon\DI\FactoryDefault Object
[_registered:protected] =>
[_useEncryption:protected] =>
[_cookies:protected] => Array
(
[hello-cookie] => Phalcon\Http\Cookie Object
(
[_readed:protected] => 1
[_restored:protected] =>
[_useEncryption:protected] =>
[_dependencyInjector:protected] => Phalcon\DI\FactoryDefault Object
[_filter:protected] =>
[_name:protected] => hello-cookie
[_value:protected] => hello cccccc kie.
[_expire:protected] => 0
[_path:protected] => /
[_domain:protected] =>
[_secure:protected] =>
[_httpOnly:protected] => 1
)
)
)
請求中發送了cookie(test-cookie=only+for+test+cookie.)
$this->cookies->get("test-cookie")獲取到了這個來自請求的cookie,但是從Phalcon\Http\Response\Cookies輸出來看,這個cookie並不在它的_cookies數組中,觀察到代碼中設置了一個叫hello-cookie的cookie,而它在_cookies數組中,所以應該是只有設置了才進入_cookies數組,$this->cookies->get("test-cookie")先判斷在不在_cookies數組中,否則再判斷是不是來自請求的cookie。
注意,$this->cookies->get("test-cookie")返回的是一個來自請求的Phalcon\Http\Cookie的類實例,所以可以調用相關方法。
Phalcon對管理來自請求的cookie比較隱晦,它只能通過Phalcon\Http\Response\Cookies容器相關方法調用,從名字來看,不應該是它的一部分。
HTTP 請求環境(Request Environment)
每個HTTP請求(通常來源於瀏覽器)會包含請求的附加信息,比如頭部數據,文件,變數等。一個基於Web的應用需要解析這些信息以提供正確的響應返回到請求者。Phalcon\Http\Request封裝了這些請求的信息,允許你以一個面向對象的方式訪問它。
<?php
// Getting a request instance
$request = new \Phalcon\Http\Request();
// Check whether the request was made with method POST
if ($request->isPost() == true) {
// Check whether the request was made with Ajax
if ($request->isAjax() == true) {
echo "Request was made using POST and AJAX";
}
}
獲取值(Getting Values)
PHP更加請求的類型自動地填充$_GET和$_POST超全局數組。這些數組保存了來自提交表單的值或者通過URL傳遞的參數值。在這些數組中的變數沒有被清理兵器包含非法字元,甚至是可能導致SQL注入或跨站腳本(XSS)攻擊的惡意代碼。
Phalcon\Http\Request allows you to access the values stored in the $_REQUEST, $_GET and $_POST arrays and sanitize or filter them with the 『filter』 service, (by default Phalcon\Filter). The following examples offer the same behavior:
Phalcon\Http\Request允許你訪問這些存儲在$_REQUEST, $_GET 和 $_POST數組中的值並且通過使用'filter'服務清理或過濾它們,(默認是Phalcon\Filter)。
以下例子提供了相同的行為:
<?php
// Manually applying the filter
$filter = new Phalcon\Filter();
$email = $filter->sanitize($_POST["user_email"], "email");
// Manually applying the filter to the value
$filter = new Phalcon\Filter();
$email = $filter->sanitize($request->getPost("user_email"), "email");
// Automatically applying the filter
$email = $request->getPost("user_email", "email");
// Setting a default value if the param is null
$email = $request->getPost("user_email", "email", "[email protected]");
// Setting a default value if the param is null without filtering
$email = $request->getPost("user_email", null, "[email protected]");
(這寫個用法基本要牢記了,要麼直接使用filter清理,要麼間接使用)
控制器中訪問請求(Accessing the Request from Controllers)
最通用的方法請求環境的地方是在控制器的動作中。為了從訪問控制器訪問Phalcon\Http\Request對象你將需要使用$this->request控制器的公共屬性:(注意,DI資源通過魔術方法映射到公共屬性,並非真的是它的公共屬性)
<?php
use Phalcon\Mvc\Controller;
class PostsController extends Controller {
public function indexAction() {
}
public function saveAction() {
// Check if request has made with POST
if ($this->request->isPost() == true) {
// Access POST data
$customerName = $this->request->getPost("name");
$customerBorn = $this->request->getPost("born");
}
}
}
文件上傳(Uploading Files)
另一個常見的任務是文件上傳。
Phalcon\Http\Request提供了一個面向對象的方法去完成這個任務:
<?php
use Phalcon\Mvc\Controller;
class PostsController extends Controller {
public function uploadAction() {
// Check if the user has uploaded files
if ($this->request->hasFiles() == true) {
// Print the real file names and sizes
foreach ($this->request->getUploadedFiles() as $file) {
//Print file details
echo $file->getName(), " ", $file->getSize(), "\n";
//Move the file into the application
$file->moveTo('files/' . $file->getName());
}
}
}
}
通過Phalcon\Http\Request::getUploadedFiles()返回的每個對象是一個Phalcon\Http\Request\File類的實例。
使用$_FILES超全局數組提供類似行為。Phalcon\Http\Request\File僅封裝了請求的跟每個上傳文件相關的信息。
使用頭信息(Working with Headers)
跟上面提到的一樣,請求頭包含了允許我們去發送合適的響應到用戶的有用的信息。以下例子展示這些信息的用法:
<?php
// get the Http-X-Requested-With header
$requestedWith = $response->getHeader("HTTP_X_REQUESTED_WITH");
if ($requestedWith == "XMLHttpRequest") {
echo "The request was made with Ajax";
}
// Same as above
if ($request->isAjax()) {
echo "The request was made with Ajax";
}
// Check the request layer
if ($request->isSecureRequest() == true) {
echo "The request was made using a secure layer";
}
// Get the servers's ip address. ie. 192.168.0.100
$ipAddress = $request->getServerAddress();
// Get the client's ip address ie. 201.245.53.51
$ipAddress = $request->getClientAddress();
// Get the User Agent (HTTP_USER_AGENT)
$userAgent = $request->getUserAgent();
// Get the best acceptable content by the browser. ie text/xml
$contentType = $request->getAcceptableContent();
// Get the best charset accepted by the browser. ie. utf-8
$charset = $request->getBestCharset();
// Get the best language accepted configured in the browser. ie. en-us
$language = $request->getBestLanguage();
『叄』 phalcon怎麼使用sql語句
直接寫原生掘岩的mysql_connect() mysql_select_db() mysql_query() 再液散舉循環結果集取鬧碧數據
『肆』 phalcon框架中怎麼實現脫離模型進行sql查詢
1:模型類創建後可答悔激以用:$this->setSource("tablename");設定表名
2:用原生sql 我前臘就假設你會原生咯清襪
『伍』 mac brew 怎麼安裝msql 5.5
Brew 是 Mac 下面的包管理工具跡罩昌,通過 Github 託管適合 Mac 的編譯配置以及 Patch,可以方便的安裝開發工具。 Mac 自帶ruby 所以安裝起來很方便,同時它也會自動把git也給你裝上。官方網站: brew/Homebrew/homebrew/go/install)" 使用以下方法可以查看brew是否安裝成功,以及目前的版本: liondeMacBook-Pro:~ lion$ brew -v Homebrew 0/project/machomebrew/姿扒Bottles/curl-7/freetds/stable/freetds-0/project/machomebrew/Bottles/gmp-6.0.0a.mavericks.bottle.tar.gz ######################################################################## 100.0% ==> Pouring gmp-6.0.0a.mavericks.bottle.tar.gz /usr/local/Cellar/gmp/6.0.0a: 15 files, 3.2M ==> Installing php55 ==> Downloading /get/php-5.5.15.tar.bz2/from/this/mirror Already downloaded: /Library/Caches/Homebrew/php55-5.5.15 ==> ./configure --prefix=/usr/local/Cellar/php55/5.5.15 --localstatedir=/usr/local/var --sysconfdir=/usr/local/etc/php/5.5 -- ==> make ==> make install ==> /usr/local/Cellar/php55/5.5.15/bin/pear config-set php_ini /usr/local/etc/php/5.5/php.ini ==> Caveats The php.ini file can be found in: /usr/local/etc/php/5.5/php.ini ✩✩✩✩ PEAR ✩✩✩✩ If PEAR complains about permissions, 'fix' the default PEAR permissions and config: chmod -R ug+w /usr/local/Cellar/php55/5.5.15/lib/php pear config-set php_ini /usr/local/etc/php/5.5/php.ini ✩✩✩✩ Extensions ✩✩✩✩ If you are having issues with custom extension compiling, ensure that you are using the brew version, by placing /usr/local/bin before /usr/sbin in your PATH: PATH="悶枝/usr/local/bin:$PATH" PHP55 Extensions will always be compiled against this PHP. Please install them using --without-homebrew-php to enable compiling against system PHP. ✩✩✩✩ PHP CLI ✩✩✩✩ If you wish to swap the PHP you use on the command line, you should add the following to ~/.bashrc, ~/.zshrc, ~/.profile or your shell's equivalent configuration file: export PATH="$(brew --prefix homebrew/php/php55)/bin:$PATH" ✩✩✩✩ FPM ✩✩✩✩ To launch php-fpm on startup: * If this is your first install: mkdir -p ~/Library/LaunchAgents cp /usr/local/Cellar/php55/5.5.15/homebrew.mxcl.php55.plist ~/Library/LaunchAgents/ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php55.plist * If this is an upgrade and you already have the homebrew.mxcl.php55.plist loaded: launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.php55.plist cp /usr/local/Cellar/php55/5.5.15/homebrew.mxcl.php55.plist ~/Library/LaunchAgents/ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php55.plist The control script is located at /usr/local/Cellar/php55/5.5.15/sbin/php55-fpm Mountain Lion comes with php-fpm pre-installed, to ensure you are using the brew version you need to make sure /usr/local/sbin is before /usr/sbin in your PATH: PATH="/usr/local/sbin:$PATH" You may also need to edit the plist to use the correct "UserName". Please note that the plist was called 'homebrew-php.josegonzalez.php55.plist' in old versions of this formula. To have launchd start php55 at login: ln -sfv /usr/local/opt/php55/*.plist ~/Library/LaunchAgents Then to load php55 now: launchctl load ~/Library/LaunchAgents/homebrew.mxcl.php55.plist ==> Summary /usr/local/Cellar/php55/5.5.15: 477 files, 43M, built in 3.9 minutes 過程中如果出現configure: error: Cannot find OpenSSL's <evp.h>這樣的錯誤,請執行以下命令,將xcode進行更新: xcode-select --install 在PHP5安裝完成的提示中寫到,如何進行PHP5和mac自帶的php以及php-fpm替換,執行以下命令後,我們可以看到當前的php版本已經是最新的了: liondeMacBook-Pro:~ lion$ echo 'export PATH=/usr/local/bin:$PATH' >> ~/.bash_profile liondeMacBook-Pro:~ lion$ echo 'export PATH=/usr/sbin:$PATH' >> ~/.bash_profile liondeMacBook-Pro:~ lion$ echo 'export PATH=/usr/local/sbin:$PATH' >> ~/.bash_profile liondeMacBook-Pro:~ lion$ source ~/.bash_profile liondeMacBook-Pro:~ lion$ php -v PHP 5.5.15 (cli) (built: Aug 14 2014 15:37:16) (DEBUG) Copyright (c) 1997-2014 The PHP Group Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies liondeMacBook-Pro:~ lion$ 使用brew search php55-命令,可以查看還有哪些擴展可以安裝,然後執行brew install php55-XXX就可以了。 liondeMacBook-Pro:~ lion$ brew search php55- php55-amqp php55-igbinary php55-msgpack php55-runkit php55-varnish php55-apcu php55-imagick php55-mysqlnd_ms php55-scrypt php55-vld php55-augmentedtypes php55-inclued php55-oauth php55-snappy php55-wbxml php55-binpack php55-intl php55-opcache php55-solr php55-xcache php55-blitz php55-ioncubeloader php55-parsekit php55-sphinx php55-xdebug php55-boxwood php55-jsmin php55-pcntl php55-spl-types php55-xhgui php55-chdb php55-judy php55-pdflib php55-ssh2 php55-xhp php55-couchbase php55-leveldb php55-pdo-dblib php55-stats php55-xhprof php55-crypto php55-libevent php55-pdo-pgsql php55-stemmer php55-xmldiff php55-dbase php55-libvirt php55-phalcon php55-sundown php55-yac php55-dbus php55-lz4 php55-proctitle php55-svm php55-yaf php55-dmtx php55-lzf php55-propro php55-swoole php55-yaml php55-ev php55-mailparse php55-protobuf php55-thrift php55-yar php55-gearman php55-mcrypt php55-pspell php55-tidy php55-yaz php55-geoip php55-memcache php55-pthreads php55-timezonedb php55-yp php55-gmagick php55-memcached php55-raphf php55-tokyotyrant php55-yrmcds php55-graphdat php55-midgard2 php55-redis php55-twig php55-zmq php55-htscanner php55-mongo php55-redland php55-uploadprogress php55-zookeeper php55-http php55-mosquitto php55-riak php55-uuid 我安裝的是以下php5.5幾個擴展: liondeMacBook-Pro:~ lion$ brew install php55-imagick php55-igbinary php55-ev php55-gmagick php55-geoip php55-memcache php55-memcached php55-thrift php55-xdebug php55-sphinx composer 接下來我們測試php-fpm的配置是否正確,以及是否正確啟動: #測試php-fpm配置是否正確 liondeMacBook-Pro:bin lion$ php-fpm -t [14-Aug-2014 16:14:30] NOTICE: configuration file /usr/local/etc/php/5.5/php-fpm.conf test is successful liondeMacBook-Pro:bin lion$ php-fpm -c /usr/local/etc/php/5.5/php.ini -y /usr/local/etc/php/5.5/php-fpm.conf -t [14-Aug-2014 16:14:48] NOTICE: configuration file /usr/local/etc/php/5.5/php-fpm.conf test is successful #啟動php-fpm liondeMacBook-Pro:bin lion$ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php55.plist #查看9000埠是否在運行 liondeMacBook-Pro:bin lion$ lsof -i:9000 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME php-fpm 51241 lion 8u IPv4 0xce9d0f6417637937 0t0 TCP localhost:cslistener (LISTEN) php-fpm 51243 lion 0u IPv4 0xce9d0f6417637937 0t0 TCP localhost:cslistener (LISTEN) php-fpm 51244 lion 0u IPv4 0xce9d0f6417637937 0t0 TCP localhost:cslistener (LISTEN) php-fpm 51245 lion 0u IPv4 0xce9d0f6417637937 0t0 TCP localhost:cslistener (LISTEN) #關閉掉php-fpm進程,再查看9000埠,沒有監聽了 liondeMacBook-Pro:bin lion$ ps auxgrep php-fpmgrep -v 'grep'awk '{print $2}'xargs kill -9 liondeMacBook-Pro:bin lion$ lsof -i:9000 liondeMacBook-Pro:bin lion$ 設置PHP-FPM開機運行(其實在安裝完成的提示中,也有說明): ln -sfv /usr/local/opt/php55/*.plist ~/Library/LaunchAgents launchctl load ~/Library/LaunchAgents/homebrew.mxcl.php55.plist
『陸』 phalcon怎麼插入數據
//config/services.php中注冊如下服務//緩存元數據絕彎$di->set('modelsMetadata',function(){$metaData=new\Phalcon\Mvc\Model\Metadata\Files(['metaDataDir'=>__DIR__.'/../apps/cache/metadata/']);return$metaData;});//設置資料庫鏈接記錄查詢$di->set('db',function()use($di){$eventsManager=$di->get("eventsManager");$logger=new\Phalcon\Logger\Adapter\File(__DIR__."/../apps/logs/debugs.log");$eventsManager->attach('握虛db'段宏燃,function($event,$connection)use($logger){if($event->getType()=='beforeQuery'){$logger->log($connection->getSQLStatement(),\Phalcon\Logger::INFO);}if($event->getType()=='beforeSave'){$logger->log($connection->getSQLStatement(),\Phalcon\Logger::INFO);}});$connection=new\Phalcon\Db\Adapter\Pdo\Mysql(["host"=>"127.0.0.1","username"=>"root","password"=>"root","dbname"=>"ai_manage","charset"=>"utf8"]);$connection->setEventsManager($eventsManager);return$connection;},false);