ZF2 - Asset Manager, slowing down application - zend-framework2

I started writing this as I was struggling to get ZF2 Asset Manager to work properly however in mid swing I managed to sort out the problem and felt the question still needs to be asked in order to help others.
If you are looking for an Asset Manager for ZF2, try out: https://github.com/RWOverdijk/AssetManager
The problems I faced was essentially due to a lack of documentation and the solution was really simple.
For asset manager to work, you essentially need to generate a cache of your files that your app can locate. Once the files have been located and are reachable, your app will speed up tremendously. The first problem I faced was the module.config.php file. The documentation is not clear in how to set this up correctly.
My original file looked like this:
'asset_manager' => [
'resolver_configs' => [
'paths' => [
__DIR__ . '/../assets',
]
],
],
Which essentially tells my application to look in the module/assets directory for any assets. This worked out the box but was VEERY slow...
I read up about caching and my modification of my file was as follows:
'asset_manager' => [
'caching' => [
'default' => [
'cache' => 'FilePath', // Apc, FilePath, FileSystem etc.
],
],
'resolver_configs' => [
'paths' => [
__DIR__ . '/../assets',
]
],
],
This did nothing... so after searching code in gitHub to see how others had implemented Asset Manager I discovered you can set the options of where to put the cache... so my new config became this:
'asset_manager' => [
'caching' => [
'default' => [
'cache' => 'FilePath', // Apc, FilePath, FileSystem etc.
'options' => [
'dir' => getcwd() . 'public',
]
],
],
'resolver_configs' => [
'paths' => [
__DIR__ . '/../assets',
]
],
],
Still my app was not working although the assets where being generated in a folder called: trunkpublic in the root of my app. After much frustration I realised that I need to update:
'dir' => getcwd() . 'public', to 'dir' => getcwd() . '/public',
Adding a slash put the folder in the public directory, which in retrospect is obvious since the app needs access to the cached files.
My final configuration is this:
'asset_manager' => [
'caching' => [
'default' => [
'cache' => 'FilePath', // Apc, FilePath, FileSystem etc.
'options' => [
'dir' => getcwd() . '/public',
//'dir' => getcwd() . '/data/cache/assets',
]
],
],
'resolver_configs' => [
'paths' => [
__DIR__ . '/../assets',
]
],
],
You will notice I have this snippet commented out:
//'dir' => getcwd() . '/data/cache/assets',
The above is super fast and is used with the Filepath option and is great for development mode, it essentially generates a hash of the file in the /data.cache/assets directory however for a production environment I prefer to have actual files saved to the assets cache found in: public/assets as this allows the app to grab the files directly without having to invoke php.
NOTE:
There was another issue I faced and this is due to misleading documentation
If you look at this line:
'cache' => 'Filesystem', // Apc, FilePath, FileSystem etc.
You may be tempted to add your files using CamelCase... however some may reauire CamelCase and others may require Normalcase... check the code for this...

works for me!!
'asset_manager' => array(
'caching' => array (
'default' => array(
'cache' => 'FilePath', // Apc, FilePath, FileSystem etc.
'options' => array(
'dir' => './public',
),
),
),
'resolver_configs' => array(
'paths' => array(
'MyModule' => __DIR__ . '/../public',
),
),
),
now i try fix flter JSMin and JSSqueeze
tank you!

Related

Typo3 Fatal error: Error\Exception: PHP Warning: gzuncompress()

I have updated my Typo3 ddev installation and i get this error:
Fatal error: Uncaught TYPO3\CMS\Core\Error\Exception: PHP Warning: gzuncompress(): data error in /var/www/html/public/typo3/sysext/core/Classes/Cache/Backend/Typo3DatabaseBackend.php line 158 in /var/www/html/public/typo3/sysext/core/Classes/Error/ErrorHandler.php:130 Stack trace: #0 [internal function]: TYPO3\CMS\Core\Error\ErrorHandler->handleError(2, 'gzuncompress():...', '/var/www/html/p...', 158, Array) #1 /var/www/html/public/typo3/sysext/core/Classes/Cache/Backend/Typo3DatabaseBackend.php(158): gzuncompress('a:1:{i:0;a:25:{...') #2 /var/www/html/public/typo3/sysext/core/Classes/Cache/Frontend/VariableFrontend.php(81): TYPO3\CMS\Core\Cache\Backend\Typo3DatabaseBackend->get('1__0_-99') #3 /var/www/html/public/typo3/sysext/core/Classes/Utility/RootlineUtility.php(211): TYPO3\CMS\Core\Cache\Frontend\VariableFrontend->get('1__0_-99') #4 /var/www/html/public/typo3/sysext/extbase/Classes/Configuration/BackendConfigurationManager.php(68): TYPO3\CMS\Core\Utility\RootlineUtility->get() #5 /var/www/html/public/typo3/sysext/ex in /var/www/html/public/typo3/sysext/core/Classes/Error/ErrorHandler.php on line 130
I don't understand what i'm missing.
I have did composer update, didn't solve it.
Even installtool say "no site configuration" "page are inaccessible"
Typo3 10.4
PHP 7
This may happen if you have utf8mb4 configured in TYPO3 configuration for the database but it is an old database with utf8 encoding.
Solution:
Dump the database
Drop the database
Create the database
Import the dump
You may want to check the dump that it does not force old utf8 encoding.
I've had a similar problem and truncating all cache tables on the database solved it for me.
I have find the solution, i was had a bad cache configuration in the localconf file, so i deleted all backend cache configuration:
[
'cacheConfigurations' => [
'hash' => [
'backend' => 'TYPO3\\CMS\\Core\\Cache\\Backend\\Typo3DatabaseBackend',
],
'imagesizes' => [
'backend' => 'TYPO3\\CMS\\Core\\Cache\\Backend\\Typo3DatabaseBackend',
'options' => [
'compression' => 1,
],
],
'pages' => [
'backend' => 'TYPO3\\CMS\\Core\\Cache\\Backend\\Typo3DatabaseBackend',
'options' => [
'compression' => 1,
],
],
'pagesection' => [
'backend' => 'TYPO3\\CMS\\Core\\Cache\\Backend\\Typo3DatabaseBackend',
'options' => [
'compression' => 1,
],
],
'rootline' => [
'backend' => 'TYPO3\\CMS\\Core\\Cache\\Backend\\Typo3DatabaseBackend',
'options' => [
'compression' => 1,
],
],
],
],
So i guess that the caching method that i was using it wasn't working... maybe from my server side, i hope i find a solution for caching.

How to create custom urls with dashes in Yii2?

I am working with advanced project application and trying to add URL rules in Yii2 to handle custom URLs with dashes.
What I want to do is to change the URL from
http://www.example.com/post/details?url=example-post-title
To
http://www.example.com/example-post-title
I have below configuration which works fine when the URL parameter does not have dash (exampleposttitle).
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
// ...
'<url:\w+>' => 'post/details',
],
],
You need to fix your regexp, since \w+ does not allow dashes:
'<url:[\w-]+>' => 'post/details',

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()

How to config Zend Frameword 2 output cache

I'm using output cache in zend framework 2:
$outputCache = Zend\Cache\PatternFactory::factory(
'output',
array(
'storage' => 'apc'
)
);
$outputCache->start('mySimpleViewScript');
include '/path/to/view/script.phtml';
$outputCache->end();
Please help me config time cache(ttl) and cache_dir.
Thanks.
If you pass storage by string ('apc' in your example), specific storage implementation is pulled from Zend\Cache\Storage\AdapterPluginManager as invokable with default options (ttl is 0 by default).
If you need custom options, just create your cache storage directly, it is covered by cache storage adapter documentation:
$cache = Zend\Cache\StorageFactory::factory([
'adapter' => [
'name' => 'apc',
'options' => [ 'ttl' => 3600 ],
],
]);
$outputCache = Zend\Cache\PatternFactory::factory('output', [
'storage' => $cache,
]);
cache_dir is not valid option for Apc adapter.

Unable to override route via DI

I'm attempting to override the routes provided by ZfcUser using akrabat's method, however it doesn't seem to have any effect. I'm starting by rebasing the routes from /user to /users but will want to add other routes later on (should be simple if I can get this working).
My DI configuration:
return [
'di' => [
'instance' => [
'Zend\Mvc\Router\RouteStack' => [
'parameters' => [
'routes' => [
'zfcuser' => [
'options' => [
'route' => '/users'
]
]
]
]
]
]
]
];
Am I making an obvious mistake or has the configuration structure for this changed since this blogpost?
You'd overwrite configuration from within your own modules.
'router' => array(
'routes' => array(
'zfcuser' => array(
//...
)
)
)
Though if i'm correct the full DI Path should still work... It's important, too, that your module loads AFTER the zfcUser-Module. So within your application.config.php be sure that your modules Namespace is listed after zfcUser. Since the arrays simply get overwritten depending on load time ^^

Resources