爱死你了 发表于 2017-3-25 09:29:49

php phalcon项目实战

  Phalcon是一套实现MVC架构的高性能PHP应用程序框架。初始版本发布于2012年11月,开放源代码并基于BSD授权条款。与其他大部分的PHP框架不同,Phalcon是以扩充的方式以C语言所编写,因此Phalcon的执行速度高过其他PHP框架,并且消耗更少的资源,根据官方的测试,Phalcon是目前世界上速度最快的PHP框架之一。
  最近使用phalcon和之前的模板进行搭建了简单的项目,下面一起分享下该框架是怎样搭建项目:
  该项目中是一个多模板项目:phalcon中session,db,第三方类库扩展,验证码,分页等都涉及到,请参阅代码。
  1,首先我们需要详细了解阅读phalcon提供的api文档,目前使用的phalcon版本是1.3
  2,我们搭建的该项目(该项目是多模块)中public/index.php是非常重要的引导文件(入口),由于phalcon是c开发的一个框架,我们是需要注册各种组件,调用其框架的所需接口,常见的如session,db,router,url等等
  如下,这是我在项目中使用的index.php:
  <?php
  use Phalcon\Mvc\Application;
  error_reporting(E_ALL);
  ini_set('date.timezone','Asia/Shanghai');
  try {
  $loader = new \Phalcon\Loader();
  $loader->registerNamespaces(array(
  "Phalcon_wifi\Common\Models" => "../apps/Common/models",
  "Phalcon_wifi\Common\Controllers" => "../apps/Common/controllers",
  "Phalcon_wifi\Common\Config" => "../apps/Common/config",
  "Phalcon_wifi\Common\Ext" => "../apps/Common/ext",
  ));
  $loader->register();
  $di = new \Phalcon\DI\FactoryDefault();
  $di["router"] = function () {
  $router = new \Phalcon\Mvc\Router();
  $router->setDefaultModule("Admin");
  $router->setDefaultNamespace("Phalcon_wifi\Admin\Controllers");
  $router->add('/:controller/:action/:params', array(
  'module' => 'Admin',
  'controller' => 1,
  'action' => 2,
  'params' => 3
  ))->setName("common");
  return $router;
  };
  $di["url"] = function () use ($di) {
  $url = new \Phalcon\Mvc\Url();
  $url->setBaseUri($di->get("config")->get("common")["baseuri"]);
  return $url;
  };
  $di["session"] = function () {
  $session = new \Phalcon\Session\Adapter\Files();
  $session->start();
  return $session;
  };
  $di["db"] = function () use($di) {
  $config = $di->get("config")->get("common")["db"];
  $connection = new \Phalcon\Db\Adapter\Pdo\Mysql(array(
  "host" => $config["host"],
  "username" => $config["username"],
  "password" => $config["password"],
  "dbname" => $config["dbname"],
  "charset" => $config["charset"]
  ));
  $eventsManager = new \Phalcon\Events\Manager();
  $dblog = $di->get("config")->get("common")["dblog"];
  $logger = new \Phalcon\Logger\Adapter\File(__DIR__ . $dblog);
  $eventsManager->attach('db:beforeQuery', function($event, $connection) use ($logger) {
  $sqlVariables = $connection->getSQLVariables();
  if (count($sqlVariables)) {
  $logger->log($connection->getSQLStatement() . ' ' . join(', ', $sqlVariables), \Phalcon\Logger::INFO);
  } else {
  $logger->log($connection->getSQLStatement(), \Phalcon\Logger::INFO);
  }
  });
  $connection->setEventsManager($eventsManager);
  return $connection;
  };
  $di["dbBackupTool"] = function () use($di) {
  $config = $di->get("config")->get("common")["db"];
  return new \Phalcon_wifi\Common\Ext\DBManager($config["username"], $config["password"], $config["host"], $config["dbname"]);
  };
  $di["validateCodeCreator"] = function () use($di) {
  return new \Phalcon_wifi\Common\Ext\ValidateCode($di->get('session'));
  };
  $di["config"] = function () {
  $config = new \Phalcon_wifi\Common\Config\Config;
  return $config->config;
  };
  $di->set('modelsManager', function() {
  return new Phalcon\Mvc\Model\Manager();
  });
  $application = new Application($di);
  $application->registerModules(array(
  "Admin" => array(
  "className" => "Phalcon_wifi\Admin\Module",
  "path" => __DIR__ . "/../apps/Admin/Module.php"
  ),
  "Portal" => array(
  "className" => "Phalcon_wifi\Portal\Module",
  "path" => __DIR__ . "/../apps/Portal/Module.php"
  )
  ));
  echo $application->handle()->getContent();
  } catch (Exception $e) {
  echo $e->getMessage();
  }
  上述代码中config,是单独放在一个文件apps/common/config/config.php如下:
  <?php 
  namespace Phalcon_wifi\Common\Config;
  class Config {
  public $config;
  public function __construct() {
  //module对应整型数值
  $configs["common"]["modules"] = array("Admin" => 1, "Brand" => 2, "Agent" => 3, "Merchant" => 4);
  //缓存文件夹
  $configs["common"]["application"] =  array('cacheDir' => __DIR__ . '/../cache/');
  //不需要进行登陆验证的url()
  $configs["common"]["not_check_uri"] =  array("Admin/Members/login", "Admin/Index/getValidateCode", "Admin/Index/test", "Admin/Members/confirmLogin");
  //常用的静态数据
  $configs["common"]["config_setting"] = [
  "group" => [
  1=>"图片配置",
  2=>"路由器配置",
  3=>"短信网关配置",
  4=>"邮件配置",
  5=>"系统配置"
  ],
  "type" => [
  1=>"字符",
  2=>"文本",
  3=>"数组",
  4=>"枚举",
  5=>"密码",
  6=>"长文本",
  7=>"加长文本",
  8=>"数字"
  ]
  ];
  //系统版本
  $configs["common"]["version"] =  "0.0.1";
  //数据库配置
  $configs["common"]["db"] =  [
  "host" => "XXXXXX",
  "username" => "XXXX",
  "password" => "xxxxxxx",
  "dbname" => "xxxxxxxx",
  "charset" => "utf8"
  ];
  //数据库备份文件名
  $configs["common"]["dblog"] = "/../apps/Common/logs/db_" . date("Y_m_d_H") . ".log";
  //app baseuri
  $configs["common"]["baseuri"] = "/project_wifi/phalcon_wifi/";
  $this->config = new \Phalcon\Config($configs);
  }
  }
  具体的项目代码,数据库文件,api文档进行打包,有兴趣的进行下载进行参考,有好的想法,见解,请给出宝贵的意见,运行该项目请修改apps/common/config/config.php中的baseUri,每个人的项目可能不一样。
  部分运行效果见附件
页: [1]
查看完整版本: php phalcon项目实战