Volt.php files not auto-updating - php

While using the volt template the files ending with .volt would auto generate a volt.php file and if any changes were made to the volt file, the volt.php file would not be updated unless deleted manually for it to generate a new volt.php file. Is there a way for it to auto-update when changes were made?
Thanks in advance.

There wasn't. I don't think this has changed. You can however set compileAlways flag while in development, or set it depending on your app environment variable, if you use one. Read this for more details.
$di->set('view', function(){
$view = new \Phalcon\Mvc\View();
$view->setViewsDir('../app/views/');
$view->registerEngines(array(
'.volt' => function($view, $di) {
$volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di);
$volt->setOptions(array(
'compiledPath' => '../app/compiled/',
'stat' => true,
'compileAlways' => true
));
return $volt;
}
));
return $view;
});

Related

How to add extension Add global in twig

When I try add the Extension Add global in twig
$view = new \Slim\Views\Twig($settings['template_path'], [
'debug' => $settings['debug'],
'cache' => $settings['cache_path']
]);
// Add extensions
$view->addExtension(new \Slim\Views\TwigExtension(
$c['router'],
$c['request']->getUri()
));
$view->addExtension(new \Core\TwigFunction());
$view->addExtension(new Twig_Extension_Debug());
$view = new Twig_Environment();;
$view->addGlobal('session', $_SESSION);
I receive:
Call to undefined method Twig_Environment::offsetSet()
In the line
$view = new Twig_Environment();
You are overriding the $view variable. Delete ing that line should solve the problem as slim/twig-view sets up the Twig environment for you.

How do I enable debuggin of Twig when used with Slim?

Per http://twig.sensiolabs.org/doc/api.html#using-extensions:
When creating a new Twig_Environment instance, you can pass an array
of options as the constructor second argument:
$twig = new Twig_Environment($loader, array('debug' => true));
How would this be implemented when used with Slim? My attempt is below, however, {{ dump(foo) }} results in an Unknown "dump" function in "forms.html" at line 35. error. I have tried adding the script on the Twig website, however, I don't know what to use for $loader and where to insert $twig.
$container['view'] = function ($c) {
$view = new \Slim\Views\Twig('../application/templates', [
//'cache' => 'path/to/cache' // See auto_reload option
'debug' => true,
'strict_variables'=> true
]);
$view->addExtension(new \Slim\Views\TwigExtension(
$c['router'],
$c['request']->getUri()
));
return $view;
};

Background image CSS path using slim php folder structure

I'm trying to do a simple background image on a page in my SlimPHP web app.
In my CSS file (which is loaded fine) I have:
.image1 {
background-color: transparent;
background: url("image1.jpg");
}
and my structure is:
-- project
-- app
-- routes
-- templates
-- www
-- css
-- stylesheet.css
-- image1.jpg
-- js
-- i
-- image1.jpg
In the network tab I get 403 Forbidden because slim is trying to access it as a page.
I have tried a few variations such as background-image, "../i/image1.jpg", but always get the same issues.
I can't find any other questions anywhere like this so there must be something simple I am missing!
I know SlimPHP fairly well but do not have much experience in CSS so any help would be appreciated!
Thanks
Additional:
Example of page request:
$app = new \Slim\Slim(array(
'templates.path' => '../templates',
));
$app->container->singleton('log', function () {
$log = new \Monolog\Logger('slim-skeleton');
$log->pushHandler(new \Monolog\Handler\StreamHandler('../logs/app.log', \Monolog\Logger::DEBUG));
return $log;
});
$app->view(new \Slim\Views\Twig());
$app->view->parserOptions = array(
'charset' => 'utf-8',
'cache' => realpath('../templates/cache'),
'auto_reload' => true,
'strict_variables' => false,
'autoescape' => true
);
$app->view->parserExtensions = array(new \Slim\Views\TwigExtension());
// Twig
$loader = new Twig_Loader_Filesystem(dirname(dirname(__FILE__)) . '/templates');
$twig = new Twig_Environment($loader);
/**
* Default home page
*/
$app->get('/', function() use ($app) {
$app->redirect('/home');
});
/**
* Index page
*/
$app->get('/home', function() use ($app, $twig) {
$params['page_title'] = 'home';
$template = $twig->loadTemplate('routes/index.html.twig');
$template->display($params);
});

Zend Framework 2 - ZFCUser - Translate login form

I'm using ZFCUser and need to develop a German project.
Unfortunately the login form is in English and I couldn't find a way to translate the form fields or especially the error messages to English.
Is there maybe a global way for the module to either overwrite the messages or switch the language?
Thanks!
Edit:
This is my translator call in my bootstrap:
$translator = new Translator();
$translator->addTranslationFile(
'phpArray',
'vendor/zendframework/zendframework/resources/languages/de/Zend_Validate.php',
'default',
'de_DE'
);
AbstractValidator::setDefaultTranslator($translator);
Edit II:
My custom translation file:
<?php
return array(
// ZFCUser
"Authentication failed. Please try again." => "test"
);
My factory:
<?php
class CustomTranslatorFactory implements FactoryInterface
{
public function createService(ServiceLocatorInterface $sl)
{
$translator = new Translator();
$translator->addTranslationFile(
'phpArray',
'vendor/.../TranslationTable.php',
'default',
'de_DE'
);
return $translator;
}
}
Module.php from my User module:
public function getServiceConfig()
{
return array(
'factories' => array(
'translator' => 'Path\To\Translator\CustomTranslatorFactory',
),
);
}
Edit III:
My Translator looks like this. Even when using a PHP file instead of the array just nothing happens. No error, no translation. Any ideas what I'm doing wrong?
$custom_translations = array(
"Authentication failed. Please try again." => "test",
);
$translator = new Translator();
$translator->addTranslationFile(
'phpArray',
'vendor/zendframework/zendframework/resources/languages/de/Zend_Validate.php',
'default',
'de_DE'
);
$translator->addTranslationFile(
'phpArray',
$custom_translations
);
return $translator;
Just for Your knowledge - since yesterday there's a modular de_DE translation available (pl_PL and ja_JP are available too). It covers only ZfcUser messages, so not all validators, but form labels etc. https://github.com/websafe/zf-mod-zfc-user-i18n-de-de Easy installable and available via Packagist too.
I'm creating an answer for that because it's becoming too complex for the comment section.
The validator library does translation of messages on its own. As you've assigned the translator to it, your validation messages are fine.
However, form label translation belongs to another piece of library. Accordingly, they also need a translator assigned to them. As stated above in the comments, you can either do that manually (by invoking $viewHelper->setTranslator($translator)) or let the ViewHelperManager do that for you.
You can easily refactor your code to support the second case.
Create a factory class for your translator.
Register that factory to SM. Use the "translator" key.
If you need it, retrieve your $translation var through SM in future.
Example (uses skipped):
/** One of your modules, should be a base module that's always loaded */
class Module
{
// ...
public function getServiceConfig()
{
return array(
'factories' => array(
'translator' => 'Your\Translator\Factory', // could also be a closure (anonymous function)
)
);
}
}
-
class YourTranslatorFactory implements FactoryInterface
{
public function createService(ServiceLocatorInterface $sl)
{
$translator = new Translator();
$translator->addTranslationFile(
'phpArray',
'vendor/zendframework/zendframework/resources/languages/de/Zend_Validate.php',
'default',
'de_DE'
);
return $translator;
}
}
If you need to access it, simpyl retrieve it from SM as you're used to ($translator = $sm->get('translator');).
Note: Validators implement the TranslatorAwareInterface. This means that, if you've registered the translator key to SM, they also should be injected into the validators automatically. Thus, you can even skip the static method call.
Note also: This is just an example of how you can do it without changing much. You could also reach this goal just by configuration.

Zend Framework 2 - Translate Standard Form Validation and Error messages

I'm writing a complete German application and therefore need to set basically everything to German.
My question: What is the best and easiest way to set for example the form validation to German?
I found this page but couldn't figure out how to get this code working:
Zend_Validate_Abstract::setDefaultTranslator($translate);
Could anyone give me some advice how to use this?
Edit:
Thanks to #Gordon I put the following into my Application/Module.php:
use Zend\I18n\Translator\Translator;
use Zend\Validator\AbstractValidator;
class Module
{
public function onBootstrap(MvcEvent $e)
{
...
$translator = new Translator();
$translator->addTranslationFile(
'phpArray',
'resources/languages/de.php',
'default',
'de_DE'
);
AbstractValidator::setDefaultTranslator($translator);
...
}
Edit 2:
Alright, this is odd. When I set de_DE I get the message that the de.php file couldn't be opened - which is true because "de" is a folder containing two other PHP files.
Could not open file resources/languages/de.php for reading
Altering the path to the folder or to any existing file within it doesnt help...
When I change the "de_DE" to "de" or "de_de" then nothing happens. No error and English validation errors. Any clues?
for me it works with
public function onBootstrap(MvcEvent $e)
{
$translator=$e->getApplication()->getServiceManager()->get('translator');
$translator->addTranslationFile(
'phpArray',
'./vendor/zendframework/zendframework/resources/languages/it/Zend_Validate.php'
);
AbstractValidator::setDefaultTranslator($translator);
// \Zend\Debug\Debug::dump($application);
}
'./vendor/zendframework/zendframework/resources/languages/langfolderyouwant/Zend_Validate.php'
Finally I found with help of #Gordon the answer!
I put the following into my Application/Module.php:
use Zend\I18n\Translator\Translator;
use Zend\Validator\AbstractValidator;
class Module
{
public function onBootstrap(MvcEvent $e)
{
...
$translator = new Translator();
$translator->addTranslationFile(
'phpArray',
'vendor/zendframework/zendframework/resources/languages/Zend_Validate.php',
'default',
'de_DE'
);
AbstractValidator::setDefaultTranslator($translator);
...
}
Then you need to enable php5-intl. Go to php.ini and enable extension=php_intl.dll.
Finally I needed to add the full path (starting with vendor) in the funciton provided by Gordon and the docs.
The later versions of Zend FW must have translators with specific interfaces.
use Zend\I18n\Translator\Translator;
use Zend\Validator\AbstractValidator;
class Module
{
public function onBootstrap(MvcEvent $e)
{
//...
$translator = new Translator();
$translator->addTranslationFile(
'phpArray',
'vendor/zendframework/zendframework/resources/languages/Zend_Validate.php',
'default',
'de_DE'
);
AbstractValidator::setDefaultTranslator($translator);
//...
}
}
would become:
use Zend\I18n\Translator\Translator;
use Zend\Validator\AbstractValidator;
class Module
{
public function onBootstrap(MvcEvent $e)
{
//...
$translator = new Translator();
$translator->addTranslationFile(
'phpArray',
'vendor/zendframework/zendframework/resources/languages/Zend_Validate.php',
'default',
'de_DE'
);
AbstractValidator::setDefaultTranslator(
new \Zend\Mvc\I18n\Translator($translator)
);
//...
}
}
Note the new \Zend\Mvc\I18n\Translator($translator)
If you don't want to pile code into onBootstrap and you only need just one language, you can use the configuration file:
'translator' => array (
'locale' => 'ru',
'translation_files' => [
[
'type' => 'phparray',
'filename' => 'path/to/ru/directory/Zend_Validate.php'
]
],
),
Put it into you module.config.php
For newest zf2 Version (2.5.0) you have to change the path to Zend_Validate.php to ./vendor/zendframework/zend-i18n-resources/languages/de/Zend_Validate.php.
$translator->addTranslationFile(
'phpArray',
'./vendor/zendframework/zend-i18n-resources/languages/de/Zend_Validate.php',
'default',
'de_DE'
);

Categories