‘壹’ 如何在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);