🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] 如果您发现了错误,请务必在您的问题中添加相关的可重现性信息,以便我们重现错误并更快地修复错误。如果您在Github上公开申请,请提交存储库地址以及问题说明。您还可以使用 [Gist](https://gist.github.com/) 发布您想要与我们分享的任何代码。 ## 创建一个小脚本 一个小的单文件脚本通常是重现问题的最佳方法: ```php <?php $di = new Phalcon\DI\FactoryDefault(); //Register your custom services $di['session'] = function() { $session = new \Phalcon\Session\Adapter\Files(); $session->start(); return $session; }; $di['cookies'] = function() { $cookies = new Phalcon\Http\Response\Cookies(); $cookies->useEncryption(false); return $cookies; }; class SomeClass extends \Phalcon\DI\Injectable { public function someMethod() { $cookies = $this->getDI()->getCookies(); $cookies->set("mycookie", "test", time() + 3600, "/"); } } $c = new MyClass; $c->setDI($di); $c->someMethod(); $di['cookies']->send(); var_dump($_SESSION); var_dump($_COOKIE); ``` 根据您的应用程序,您可以使用这些框架来创建自己的脚本并重现该错误: ### 数据库 请记住向脚本添加如何注册数据库服务: ```php <?php $di = new Phalcon\DI\FactoryDefault(); $di->setShared('db', function () { return new \Phalcon\Db\Adapter\PDO\Mysql(array( 'host' => '127.0.0.1', 'username' => 'root', 'password' => '', 'dbname' => 'test', 'charset' => 'utf8', )); }); $result = $di['db']->query('SELECT * FROM customers'); ``` ### 单/多模块应用 请记住向脚本添加如何创建 `Phalcon\Mvc\Application` 实例: ```php <?php $di = new \Phalcon\DI\FactoryDefault(); //other services $app = new \Phalcon\Mvc\Application(); $app->setDi($di); //register modules if any echo $app->handle->getContent() ``` 包括模型和控制器作为测试的一部分: ```php <?php $di = new \Phalcon\DI\FactoryDefault(); //other services $app = new \Phalcon\Mvc\Application(); $app->setDi($di); class IndexController extends Phalcon\Mvc\Controller { public function indexAction() { /* your content here */ } } class Users extends Phalcon\Mvc\Model { } echo $app->handle->getContent() ``` ### 微应用 按照此结构创建脚本: ```php <?php $di = new \Phalcon\DI\FactoryDefault(); $app = new \Phalcon\Mvc\Micro($di); //define your routes here $app->handle(); ``` ### Dispatcher ### ORM 您可以提供自己的数据库模式,甚至可以更好地使用任何phalcon测试[数据库](https://github.com/phalcon/cphalcon/tree/master/unit-tests/schemas)。按照此结构创建脚本: ```php <?php use Phalcon\DI; use Phalcon\Events\Manager as EventsManager; use Phalcon\Db\Adapter\Pdo\Mysql as Connection; use Phalcon\Mvc\Model\Manager as ModelsManager; use Phalcon\Mvc\Model\Metadata\Memory as ModelsMetaData; $eventsManager = new EventsManager(); $di = new DI(); $connection = new Connection(array( "host" => "localhost", "username" => "root", "password" => "", "dbname" => "test" )); $connection->setEventsManager($eventsManager); $eventsManager->attach('db', function ($event, $connection) { switch ($event->getType()) { case 'beforeQuery': echo $connection->getSqlStatement(), "<br>\n"; break; } } ); $di['db'] = $connection; $di['modelsManager'] = new ModelsManager(); $di['modelsMetadata'] = new ModelsMetadata(); if (!$connection->tableExists('user', 'test')) { $connection->execute('CREATE TABLE user (id integer primary key auto_increment, email varchar(120) not null)'); } class User extends \Phalcon\Mvc\Model { public $id; public $email; public static function myCustomUserCreator() { $newUser = new User(); $newUser->email = 'test'; if ($newUser->save() == false) { return false; } return $newUser->id; } } echo User::myCustomUserCreator(); ```