How to use caching using Zend 2 Module AssetManager? - zend-framework2

I was able to minify a CSS file with the following code block using Zend 2 Module AssetManager (link) but have a problem with caching. How do I configure it to cache the CSS file?
I see a HTTP status with 200 OK for the CSS file in Firebug. If the CSS file is cached, it should be 304 Not Modified.
Note that the Apache web server has read and write permissions for "public" directory and anything inside of the directory.
'asset_manager' => array(
'resolver_configs' => array(
'paths' => array(
__DIR__ . '/../public'
)
),
'filters' => array(
'css/a.css' => array(
array(
'filter' => 'UglifyCss'
)
)
),
'caching' => array(
'a.css' => array(
'cache' => 'FilePath',
'options' => array(
'dir' => __DIR__ . '/../public'
)
)
)
)

If the CSS file is cached, it should be 304 Not Modified.
No, it should only be a 304 if you configured browser caching in youre configuration. If you configure browser caching in the AsseticModule it only meens that the minified and combined css/js files are cached on youre system and not generated on every request.
Put something like this in youre .htaccess under public folder.
# ----------------------------------------------------------------------
# Expires headers (for better cache control)
# ----------------------------------------------------------------------
<IfModule mod_expires.c>
# Mod aktivieren
ExpiresActive on
# Standard Cache
ExpiresDefault "access plus 1 minute"
# Media
ExpiresByType image/gif "access plus 1 minute"
ExpiresByType image/jpeg "access plus 1 minute"
ExpiresByType image/jpg "access plus 1 minute"
ExpiresByType image/png "access plus 1 minute"
ExpiresByType application/javascript "access plus 1 minute"
ExpiresByType text/css "access plus 1 minute"
</IfModule>

Related

How to define an alternative database adapter config file in Apigility?

In an Apigility driven Zend Framework 2 application wenn a database adapter is created (over the Apigility Admin UI), its settings by default get saved in /config/autoload/global.php
return array(
'db' => array(
'adapters' => array(
...
'DbAdapter_FooBar' => array(),
...
),
),
...
);
and in /config/autoload/local.php
return array(
'db' => array(
'adapters' => array(
...
'DbAdapter_FooBar' => array(
'charset' => 'UTF-8',
'database' => 'asdf',
'driver' => 'PDO_Mysql',
'hostname' => 'asdf',
'username' => 'asdf',
'password' => 'asdf',
'port' => '1234',
'driver_options' => array(
1002 => 'SET NAMES \'UTF8\'',
),
),
...
),
),
...
);
In the application I'm working on the config files structure differs from the ZF2 standard, e.g. there are separate config files for the database settings: /config/autoload/mydb.global.php and /config/autoload/mydb.local.php.
(How) Can Apigility be configured in the way, that the database adapters settings get stored in custom config files? How/where to set these files?
Configuration for various components can be provided in multiple places, but all configuration is merged recursively into one large configuration file by the Zend\Stdlib\ArrayUtils::merge() method. Because configurations are merged recursively, the order that they are added to the merged configuration array is really important to avoid unexpected overwriting.
Configurations are merged in the following order:
The array returned by the module’s getConfig() method
config/autoload/*.global.php — global autoload files
config/autoload/*.local.php — local autoload files
The methods described by the Feature interface — get*Config()

Carrierwave - configuring for Amazon S3

I have a Rails 4 application and am trying to configure Carrierwave and Fog to store uploaded files on Amazon S3 but I keep getting the following error:
Expected(200) <=> Actual(301 Moved Permanently) excon.error.response :body => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Error><Code>PermanentRedirect</Code><Message>The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint.</Message><RequestId>3C27ACF693820E4E</RequestId><Bucket>bucket_name</Bucket><HostId>8hnHAWoVEgsGkSyclME99rPTq5UHuSt6ZQ/ezmCRcuK+JUGWsSeI4FvcC2A5cym7</HostId><Endpoint>s3.amazonaws.com</Endpoint></Error>" :headers => { "Content-Type" => "application/xml" "Date" => "Wed, 03 Sep 2014 06:59:16 GMT" "Server" => "AmazonS3" "x-amz-id-2" => "8hnHAWoVEgsGkSyclME99rPTq5UHuSt6ZQ/ezmCRcuK+JUGWsSeI4FvcC2A5cym7" "x-amz-request-id" => "3C27ACF693820E4E" } :local_address => "10.0.0.9" :local_port => 54480 :remote_ip => "176.32.114.26" :status => 301
config/initializers/carrierwave.rb:
CarrierWave.configure do |config|
config.fog_credentials = {
:provider => 'AWS',
:aws_access_key_id => 'XXXXXXXXXXXXXXXXXXXX',
:aws_secret_access_key => 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
:region => 'us-east-1'
}
config.fog_directory = 'bucket_name'
config.fog_public = false
config.fog_attributes = {'Cache-Control'=>'max-age=315576000'}
end
I've also tried removing the :region parameter (along with the preceding comma) but that doesn't seem to work either. I've checked the bucket region and it is listed as "US Standard" but if I look at endpoint, Amazon lists that bucket as us-east-1. Regardless, I've tried assigning both of those values to :region and neither worked.
Can somebody help me figure out what I'm doing wrong?
In case this might help somebody save a few hours of their life, the solution turned out to be that I just needed to restart the server after modifying the file.

typo3 realurl show only one segment in url

I was working on realurl's in typo3 and spend hours on working out why typo3 is showing only one segment in url.
site structure looks like this:
and URL that I got for subpage 'munchen-maistrasse' is:
http://www.bernd-gruber.at/munchen-maistrasse/
I want it to be:
http://www.bernd-gruber.at/referenzen/munchen-maistrasse/
This is my server .htaccess file:
<FilesMatch "\.(js|css)$">
<IfModule mod_expires.c>
ExpiresActive on
ExpiresDefault "access plus 7 days"
</IfModule>
FileETag MTime Size
</FilesMatch>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^(.+)\.(\d+)\.(php|js|css|png|jpg|gif|gzip)$ $1.$3 [L]
RewriteRule ^(typo3/|t3lib/|fileadmin/|typo3conf/|typo3temp/|uploads/|favicon\.ico) - [L]
RewriteRule ^typo3$ typo3/index_re.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]
</IfModule>
AddType video/x-m4v .m4v
AddType video/ogg .ogv
AddType video/webm .webm
AddType video/x-m4v .m4v
AddType application/ogg .ogg
I dont use config.baseURL in typoscript I use:
config.absRefPrefix = /
config.prefixLocalAnchors = all
on my root page. I have runned out of solutions.
You need your own configuration to reach this.
1) In the Extension Manager -> RealUrl -> Configuration you have to disable the automatic configuration and define there the path to your own realUrl configuration file.
2) You should make sure that (page).config.tx_realurl_enable = 1 is set.
3) After making the right realUrl configuration for your website you have to truncate all realUrl caching tables or just remove all ID to Path mappings.
Here some example of a standard realUrl configuration template:
<?php
$realurl_template = array(
'init' => array(
'appendMissingSlash' => 'ifNotFile,redirect',
'enableCHashCache' => 1,
'enableUrlDecodeCache' => 1,
'enableUrlEncodeCache' => 1,
'emptyUrlReturnValue' => '/'
),
'preVars' => array(
array(
'GETvar' => 'no_cache',
'valueMap' => array(
'nc' => 1,
),
'noMatch' => 'bypass',
),
),
'fileName' => array(
'index' => array(
),
),
'postVarSets' => array(
'_DEFAULT' => array (
),
),
'pagePath' => array(
'type' => 'user',
'userFunc' => 'EXT:realurl/class.tx_realurl_advanced.php:&tx_realurl_advanced->main',
'spaceCharacter' => '-',
'languageGetVar' => 'L',
'expireDays' => 3,
)
);
# Configurate domain names
$TYPO3_CONF_VARS['EXTCONF']['realurl'] = array(
'_DEFAULT' => $realurl_template,
'domain.com' => $realurl_template,
'www.domain.com' => $realurl_template,
);
$TYPO3_CONF_VARS['EXTCONF']['realurl']['domain.com']['pagePath']['rootpage_id'] = 1;
$TYPO3_CONF_VARS['EXTCONF']['realurl']['www.domain.com']['pagePath']['rootpage_id'] = 1;
# Unset template
unset($realurl_template);
?>

Enable Browser-Caching for lighttpd/thin

I have the problem that Googles “Page Speed” says that I should enable browser caching.
I am usein a lighttpd server as a proxy for different web server on my server. One of them is a rails app (Ruby 1.9.2/Rails 2.3.x) running with thin server.
I thought that I have to enable “mod_expire” in the lighttpd proxy and use the settings:
expire.url = ("/favicon.ico" => "access plus 7 days",
"/stylesheets/" => "access plus 7 days",
"/javascripts/" => "access plus 7 days",
"/images" => "access plus 7 days"
)
But it did not work. I also moved the expire.url code to:
$HTTP["host"] =~ "myRailsApp" {
expire.url = ...
proxy.balance = "fair"
proxy.server = (
"/" => (
(
"host" => "11.22.33.44",
"port" => 2000
),
(
"host" => "11.22.33.44",
"port" => 2001
)
)
}
It did not work, either.
So my question is: How can I enable the browser caching for my lighttpd/thin setting?
Unfortunately, Google did not help me.
It is also possible to use a condition, e.g.:
[
...]
$HTTP["url"] =~ "^/images/" {
expire.url = ( "" => "access plus 1 hours" )
}
[...]

Set mime_types not working for zip file in Symfony

In my Symfony application, I want to set zip file as one of the mime type during the file upload, the code to do this is below:
$this->validatorSchema ['Documents'] = new sfValidatorFile (
array ('mime_types' => array(
'application/zip',
'image/jpeg',
'image/pjpeg',
'image/png',
'image/x-png',
'image/gif',
'application/x-zip',
'application/octet-stream',
'application/pdf') ),
array ('invalid' => 'Invalid file.',
'required' => 'Select a file to upload.',
'mime_types' => 'The file must be of JPEG, PNG , GIF, pdf and zip format.' ) );
However, when I did the uploads, I found that all of pdf, png, gif etc can be uploaded. The only mime type that cannot be uploaded, are files that end with .zip.
What's going wrong here?
I asked a related, but different question here.
Note: I am using Symfony 1.2.7
Edit: I did some further testing with different browsers. Firefox 3 works because of 'application/octet-stream'', whereas IE works because of 'application/x-zip-compressed', Google Chrome doesn't work at all.
After more testing, I believe that this is a bug in Symfony.
Do an echo or error_log to see what $uploaded_file->getMime() [or whatever the right method call is] returns for your zip files.
If the string you get is one of the strings you pass to the array, there might be a bug with sfValidatorFile (I've never used it) and you might want to try using a yml validator.
this is a problem with mime type detection. the sfValidatorFile can use user supplied function to determine mime type. so you can write your own function to detect zip files from uploaded file if mime type that is determined by validator doesn't do the job right.
link to documentation, look at the end of page that explains file uploads.
There is a solution :
create your "sfValidatorFileZip.class.php" in your "/lib/Validator/". This class extend from the commun "sfValidatorFile" and it contain the "configure" methode as listed below :
class sfValidatorFileZip extends sfValidatorFile{
protected function configure($options = array(), $messages = array()){
if (!ini_get('file_uploads'))
{
throw new LogicException(sprintf('Unable to use a file validator as "file_uploads" is disabled in your php.ini file (%s)', get_cfg_var('cfg_file_path')));
}
$this->addOption('max_size');
$this->addOption('mime_types');
$this->addOption('mime_type_guessers', array(
array($this, 'guessFromFileinfo'),
array($this, 'guessFromMimeContentType'),
array($this, 'guessFromFileBinary'),
));
$this->addOption('mime_categories', array(
'web_images' => array(
'image/jpeg',
'image/pjpeg',
'image/png',
'image/x-png',
'image/gif',
),
'zip_file' => array(
'application/zip'
)
));
$this->addOption('validated_file_class', 'sfValidatedFile');
$this->addOption('path', null);
$this->addMessage('max_size', 'File is too large (maximum is %max_size% bytes).');
$this->addMessage('mime_types', 'Invalid mime type (%mime_type%).');
$this->addMessage('partial', 'The uploaded file was only partially uploaded.');
$this->addMessage('no_tmp_dir', 'Missing a temporary folder.');
$this->addMessage('cant_write', 'Failed to write file to disk.');
$this->addMessage('extension', 'File upload stopped by extension.');
}
}
in your form, the widget will be an instance from your custom "sfValidatorFileZip" an you have to precise that 'mime_types' => 'zip_file'. exemple :
$this->setValidator('filename', new sfValidatorFileZip(array(
'required' => false,
'mime_types' => 'zip_file',
'path' => sfConfig::get('sf_upload_dir') . '/files/',),
array('required' => 'filename is required.', 'mime_types' => 'only ZIP is accepted')));

Resources