single/
app/
controllers/
models/
views/
public/
css/
img/
js/
multiple/
apps/
frontend/
controllers/
models/
views/
Module.php
backend/
controllers/
models/
views/
Module.php
public/
../
// Create the Dependency Injector Container
$di = new Phalcon\DI();
// Register classes, functions, components
$di->set("request", new Phalcon\Http\Request());
..
// Use anywhere else in code
$request = $di->getShared('request');
use Phalcon\Mvc\Micro;
$app = new Micro();
// بازگردانی داده به JSON
$app->get(
'/check/status',
function () {
return $this
->response
->setJsonContent(
[
'status' => 'important',
]
)
;
}
);
$app->handle();
use Phalcon\Loader;
// Autoloader را ایجاد میکند
$loader = new Loader();
// ثبت namespace جدید
$loader->registerNamespaces(
[
'Example\Base' => 'vendor/example/base/',
'Example\Adapter' => 'vendor/example/adapter/',
'Example' => 'vendor/example/',
]
);
// ثبت autoloader
$loader->register();
// Create the router
$router = new \Phalcon\Mvc\Router();
// تعریف یک مسیر
$router->add(
'/admin/users/my-profile',
[
'controller' => 'users',
'action' => 'profile',
]
);
use Invoices;
use Phalcon\Mvc\Model;
class Customers extends Model
{
public $cst_id;
public $cst_name;
public function initialize()
{
$this->hasMany(
'cst_id',
Invoices::class,
'inv_cst_id'
);
}
}
$phql = 'SELECT * '
. 'FROM Formula\Cars '
. 'ORDER BY Formula\Cars.name';
$query = $manager->createQuery($phql);
$phql = 'SELECT Formula\Cars.name '
. 'FROM Formula\Cars '
. 'ORDER BY Formula\Cars.name';
$query = $manager->createQuery($phql);
$phql = 'SELECT c.name '
. 'FROM Formula\Cars c '
. 'ORDER BY c.name';
$query = $manager->createQuery($phql);
$phql = 'SELECT c.* '
. 'FROM Cars AS c '
. 'ORDER BY c.name';
$cars = $manager->executeQuery($phql);
foreach ($cars as $car) {
echo "Name: ", $car->name, "\n";
}
use Phalcon\Mvc\Model\Transaction\Failed;
use Phalcon\Mvc\Model\Transaction\Manager;
try {
// Create a transaction manager
$manager = new Manager();
// Request a transaction
$transaction = $manager->get();
// Get the robots to be deleted
$invoices = Invoices::find(
'inv_cst_id = 123'
);
foreach ($invoices as $invoice) {
$invoice->setTransaction($transaction);
if ($invoice->delete() == false) {
// Something is wrong - rollback transaction
foreach ($invoice->getMessages() as $message) {
$transaction
->rollback($message->getMessage());
}
}
}
// Everything is OK - commit transaction
$transaction->commit();
echo "Robots were deleted successfully!";
} catch (Failed $e) {
echo "Failed, reason: ", $e->getMessage();
}
use Phalcon\Cache;
use Phalcon\Cache\AdapterFactory;
use Phalcon\Storage\Serializer\SerializerFactory;
$serializerFactory = new SerializerFactory();
$adapterFactory = new AdapterFactory($serializerFactory);
$options = [
'defaultSerializer' => 'Json',
'lifetime' => 7200
];
$adapter = $adapterFactory
->newInstance('apcu', $options);
$cache = new Cache($adapter);
<html>
<body>
<div class='top'>
<?php $this->partial('shared/ad_banner'); ?>
</div>
<div class='content'>
<h1>Robots</h1>
<p>
Check out our specials for robots:
</p>
...
</div>
<div class='footer'>
<?php $this->partial('shared/footer'); ?>
</div>
</body>
</html>
{# app/views/products/show.volt #}
{% block last_products %}
{% for prod in products %}
* Name: {{ prod.name|e }}
{% if prod.status == 'Active' %}
Price: {{ prod.price + prod.taxes/100}}
{% endif %}
{% endfor %}
{% endblock %}
// app/messages/en.php
$messages = [
'hi' => 'Hello',
'bye' => 'Good Bye',
'hi-name' => 'Hello %name%',
'song' => 'This song is %song%'
];
// app/messages/es.php
$messages = [
'hi' => 'Hola',
'bye' => 'Adiós',
'hi-name' => 'Hola %name%',
'song' => 'Esta canción es %song%'
];
use Phalcon\Mvc\Controller;
use Phalcon\Translate\Adapter\NativeArray;
// UserController.php
class UserController extends Controller
{
protected function getTranslation()
{
// Browser's best language
$language = $this
->request
->getBestLanguage();
// Check the lang translation file
$fileName = 'app/messages/'
. $language
. '.php';
if (file_exists($fileName) {
require $fileName;
} else {
// بازگشت به یک گزینه پیشفرض
require 'app/messages/en.php';
}
// بازگرداندن یک فایل ترجمه
return new NativeArray(
array(
'content' => $messages
)
);
}
public function indexAction()
{
$this->view->name = 'Mike';
$this->view->t = $this
->getTranslation();
}
}
// user.volt
<p><?php echo $t->_('hi'), ' ', $name; ?></p>
use Phalcon\Forms\Form;
use Phalcon\Forms\Element\Text;
use Phalcon\Forms\Element\Select;
$form = new Form();
$form->add(new Text('name'));
$form->add(new Text('telephone'));
$form->add(
new Select(
'telephoneType',
array(
'H' => 'Home',
'C' => 'Cellphone'
)
)
);
use Phalcon\Mvc\Controller;
class PostsController extends Controller
{
public function saveAction()
{
$this->flash->error(
'در این فرم خطاهایی رخ داده است'
);
$this->flash->success(
'yes!, everything is fine'
);
$this->flash->notice(
'این یک اطلاعیه برای کاربران است'
);
$this->flash->warning(
'این فقط یک هشدار است'
);
}
}
use Phalcon\Acl;
use Phalcon\Acl\Enum;
use Phalcon\Acl\Role;
use Phalcon\Acl\Adapter\Memory;
// ایجاد ACL
$acl = new Memory();
// عملکرد پیشفرض دسترسی DENY است
$acl->setDefaultAction(Enum::DENY);
// Register two roles, Users
// and Guests
$roles = array(
'users' => new Role('Users'),
'guests' => new Role('Guests')
);
foreach ($roles as $role) {
$acl->addRole($role);
}
use Phalcon\Db\Adapter\Pdo\Mysql;
use Phalcon\Db\Adapter\Pdo\PostgreSQL;
// این سرویس یک پایگاهداده MySQL را باز میگرداند
$container->set(
'dbMysql',
function () {
return new Mysql(
[
'host' => 'localhost',
'username' => 'root',
'password' => 'secret',
'dbname' => 'tutorial',
]
);
}
);
// این سرویس یک پایگاهداده PostgreSQL را باز میگرداند
$container->set(
'dbPostgres',
function () {
return new PostgreSQL(
[
'host' => 'localhost',
'username' => 'postgres',
'password' => '',
'dbname' => 'invo',
]
);
}
);
use Phalcon\Crypt;
$crypt = new Crypt();
$key = 'این یک کلید خصوصی (۳۲ بایتی) است.';
$text = 'The text you want to encrypt.';
$encrypted = $crypt->encrypt($text, $key);
echo $crypt->decrypt($encrypted, $key);
use Phalcon\Mvc\Dispatcher;
use Phalcon\Events\Manager;
$container->set(
'dispatcher',
function () {
// یک مدیر رویداد ایجاد میکند
$manager = new Manager();
// Listen for events produced in the
// dispatcher using the SecurityPlugin
$manager->attach(
'dispatch:beforeExecuteRoute',
new SecurityPlugin
);
// Handle exceptions and not-found
// exceptions using NotFoundPlugin
$manager->attach(
'dispatch:beforeException',
new NotFoundPlugin
);
$dispatcher = new Dispatcher();
// Assign the events manager
// to the dispatcher
$dispatcher
->setEventsManager($manager);
return $dispatcher;
}
);