Reputation: 45
i am developing a dynamic website using phalcon and i have header and footer for every page in the project so how could i include it throughout my whole project without using volt engine.i don't want to use volt engine because it is very complex to use.
/**
* Read the configuration
*/
$config = new Phalcon\Config\Adapter\Ini(__DIR__ . '/../app/config/config.ini');
$loader = new \Phalcon\Loader();
/**
* We're a registering a set of directories taken from the configuration file
*/
$loader->registerDirs(
array(
__DIR__ . $config->application->controllersDir,
__DIR__ . $config->application->pluginsDir,
__DIR__ . $config->application->libraryDir,
__DIR__ . $config->application->modelsDir,
)
)->register();
/**
* The FactoryDefault Dependency Injector automatically register the right services providing a full stack framework
*/
$di = new \Phalcon\DI\FactoryDefault();
/**
* We register the events manager
*/
$di->set('dispatcher', function() use ($di) {
$eventsManager = $di->getShared('eventsManager');
$security = new Security($di);
/**
* We listen for events in the dispatcher using the Security plugin
*/
$eventsManager->attach('dispatch', $security);
$dispatcher = new Phalcon\Mvc\Dispatcher();
$dispatcher->setEventsManager($eventsManager);
return $dispatcher;
});
/**
* 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->application->baseUri);
return $url;
});
$di->set('view', function() use ($config) {
$view = new \Phalcon\Mvc\View();
$view->setViewsDir(__DIR__ . $config->application->viewsDir);
$view->registerEngines(array(
".volt" => 'volt'
));
return $view;
});
/**
* Setting up volt
*/
$di->set('volt', function($view, $di) {
$volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di);
$volt->setOptions(array(
"compiledPath" => "../cache/volt/"
));
return $volt;
}, true);
/**
* Database connection is created based in the parameters defined in the configuration file
*/
$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
));
});
/**
* If the configuration specify the use of metadata adapter use it or use memory otherwise
*/
$di->set('modelsMetadata', function() use ($config) {
if (isset($config->models->metadata)) {
$metaDataConfig = $config->models->metadata;
$metadataAdapter = 'Phalcon\Mvc\Model\Metadata\\'.$metaDataConfig->adapter;
return new $metadataAdapter();
}
return new Phalcon\Mvc\Model\Metadata\Memory();
});
/**
* 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(){
return new Phalcon\Flash\Direct(array(
'error' => 'alert alert-error',
'success' => 'alert alert-success',
'notice' => 'alert alert-info',
));
});
/**
* Register a user component
*/
$di->set('elements', function(){
return new Elements();
});
$application = new \Phalcon\Mvc\Application();
$application->setDI($di);
echo $application->handle()->getContent();
Upvotes: 0
Views: 1711
Reputation: 626
Partials are one option. They're view logic that you extract out for reuse in many places.
Create and store your header and footer files under the /shared dir and do the following:
<div class="top"><?php $this->partial("shared/header") ?></div>
...
<div class="footer"><?php $this->partial("shared/footer") ?></div>
Edit: Do something like the below:
Change the layout to be used instead of using the name of the latest controller name
<?php
$this->view->setLayout('main');
More information can be found in the docs regarding views.
Upvotes: 2