Skip to content

Instantly share code, notes, and snippets.

@phalcon
Created September 29, 2012 17:43
Show Gist options
  • Save phalcon/3804683 to your computer and use it in GitHub Desktop.
Save phalcon/3804683 to your computer and use it in GitHub Desktop.
<?php
namespace Phacrm\Portal\Controllers;
class IndexController extends \Phalcon\Mvc\Controller
{
public function indexAction()
{
$this->view->setVar('greeting', 'Hola!');
}
}
<?php
namespace Phacrm\Portal;
class Module
{
public function registerAutoloaders()
{
$loader = new \Phalcon\Loader();
$loader->registerNamespaces(array(
'Phacrm\Portal\Controllers' => '../apps/portal/controllers/',
'Phacrm\Portal\Models' => '../apps/portal/models/',
));
$loader->register();
}
/**
* Register the services here to make them general or register in the ModuleDefinition to make them module-specific
*/
public function registerServices($di)
{
//Registering a dispatcher
$di->set('dispatcher', function () {
$dispatcher = new \Phalcon\Mvc\Dispatcher();
$dispatcher->setDefaultNamespace("Phacrm\Portal\Controllers\\");
return $dispatcher;
});
//Registering the view component
$di->set('view', function () {
$view = new \Phalcon\Mvc\View();
$view->setViewsDir('../apps/portal/views/');
$view->registerEngines(
array(".tpl" => "SmartyEngine")
);
return $view;
});
$di->set('db', function () {
return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
"host" => "localhost",
"username" => "root",
"password" => "secret",
"dbname" => "invo"
));
});
}
}
<?php
/**
* Adapter to use Smarty3 as templating engine
*/
require_once 'Smarty3/Smarty.class.php';
class SmartyEngine extends \Phalcon\Mvc\View\Engine
{
protected $_smarty;
protected $_params;
public function __construct(Phalcon\Mvc\View $view, Phalcon\DI $di)
{
$this->_smarty = new Smarty();
$this->_smarty->template_dir = '.';
$this->_smarty->compile_dir = SMARTY_DIR . 'templates_c';
$this->_smarty->config_dir = SMARTY_DIR . 'configs';
$this->_smarty->cache_dir = SMARTY_DIR . 'cache';
$this->_smarty->caching = false;
$this->_smarty->debugging = true;
parent::__construct($view, $di);
}
public function render($path, $params)
{
if (!isset($params['content'])) {
$params['content'] = $this->_view->getContent();
}
foreach($params as $key => $value){
$this->_smarty->assign($key, $value);
}
$this->_view->setContent($this->_smarty->fetch($path));
}
}
<?php
/**
* Application driver class to initialize Phalcon and
* other resources.
*/
class Application extends \Phalcon\Mvc\Application
{
const MODE_PRODUCTION = 'production';
const MODE_STAGING = 'staging';
const MODE_TEST = 'test';
const MODE_DEVELOPMENT = 'development';
const MODE_MAINTAIN = 'maintain';
/**
* config section
*/
private static $config = array(
'mode' => 'development',
'defaultModule' => 'portal',
'defaultController' => 'index',
'defaultAction' => 'index',
'modules' => array(
'portal' => array(
'className' => 'Phacrm\Portal\Module',
'path' => '../apps/portal/Module.php'
),
'backend' => array(
'className' => 'Phacrm\Backend\Module',
'path' => '../apps/backend/Module.php'
)
),
'database' => array(
'adapter' => 'mysql',
'host' => 'localhost',
'username' => 'root',
'password' => '111111',
'name' => 'phacrm'
),
'urls' => array(
'libraryDir' => '/../library/',
'helperDir' => '/../helpers/',
'baseUri' => '/phacrm/'
),
'models' => array(
'metadata' => array(
'adapter' => 'Apc',
'lifetime' => 86400
)
),
'routes' => array(
'/portal/:controller/:action' => array(
'module' => 'portal',
'controller' => 1,
'action' => 2,
),
'#^/backend(|/)$#' => array(
'module' => 'backend',
'controller' => 'index',
'action' => 'index',
),
'#^/backend/([a-zA-Z0-9\_]+)[/]{0,1}$#' => array(
'module' => 'backend',
'controller' => 1,
),
'#^/backend[/]{0,1}([a-zA-Z0-9\_]+)/([a-zA-Z0-9\_]+)(/.*)*$#' => array(
'module' => 'backend',
'controller' => 1,
'action' => 2,
'params' => 3,
)
)
);
/**
* Set application mode and error reporting level.
*/
public function __construct()
{
if (!defined('PHACRM_MODE')) {
$mode = getenv('PHACRM_MODE');
$mode = $mode ? $mode : self::$config['mode'];
define('PHACRM_MODE', $mode);
}
switch (PHACRM_MODE) {
case self::MODE_MAINTAIN:
include_once 'errors/_maintaining.phtml';
exit();
break;
case self::MODE_PRODUCTION:
case self::MODE_STAGING:
default:
error_reporting(0);
break;
case self::MODE_TEST:
case self::MODE_DEVELOPMENT:
error_reporting(E_ALL);
break;
}
}
/**
* Register the services here to make them general or register in
* the ModuleDefinition to make them module-specific.
*/
protected function _registerServices()
{
$loader = new \Phalcon\Loader();
/**
* We're a registering a set of directories taken from the configuration file
*/
$loader->registerDirs(
array(
__DIR__.self::$config['urls']['libraryDir'],
__DIR__.self::$config['urls']['helperDir'],
)
)->register();
$di = new \Phalcon\DI\FactoryDefault();
// Registering a router:
$config = self::$config;
$di->set('router', function() use ($config) {
$router = new \Phalcon\Mvc\Router();
$router->setDefaultModule($config['defaultModule']);
$router->setDefaultController($config['defaultController']);
$router->setDefaultAction($config['defaultAction']);
foreach ($config['routes'] as $pattern => $paths)
{
$router->add($pattern, $paths);
}
return $router;
});
/**
* The URL component is used to generate all kind of urls in the application
*/
$di->set('url', function() use ($config){
$url = new \Phalcon\Mvc\Url();
$url->setBaseUri($config['urls']['baseUri']);
return $url;
});
$di->set('db', function() use ($config) {
return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
"host" => $config->database->host,
"username" => $config->database->username,
"password" => $config->database->password,
"dbname" => $config->database->name
));
});
/**
* Start the session the first time some component request the session service
*/
$di->set('session', function(){
$session = new Phalcon\Session\Adapter\Files();
$session->start();
return $session;
});
/**
* Register the flash service with custom CSS classes
*/
$di->set('flash', function(){
$flash = new Phalcon\Flash\Direct(array(
'error' => 'alert alert-error',
'success' => 'alert alert-success',
'notice' => 'alert alert-info',
));
return $flash;
});
$this->setDI($di);
}
public function main()
{
$this->_registerServices();
$this->registerModules(self::$config['modules']);
echo $this->handle()->getContent();
}
}
// Run application:
$app = new Application();
$app->main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment