CakePHP cache i18n translate - php

When I forget to translate something, somewhere Project VIEW, I change the file /app/Locale/por/LC_MESSAGES/default.po and sending it back to the server.
But mostly, this 'new translation', takes HOURS to be viewed, in short: I just send the file, cleaned the cache and browser CakePHP, press F5, and ... NOTHING HAPPENS.
For what reason?
[EDIT]
<?php echo $this->Form->input('Item.0.description', array('label' => false,
'class' => 'span12', 'div' => array('class' => 'span7'), 'rows' => 3,
'placeholder' => __('Type the description'))); ?>

To force the language to update you can clear the persistent and models directories in the /tmp/cache directory. If view caching is enabled you'll have to clean out views as well.
Caching is disabled when debug is set to 2 (which is the value for using the framework during development) and the persistent directory is populated with new cache files, overwriting the old ones each time a view is loaded. So the debug switch and subsequent browser refresh might clean the old language files for you.

I found a strange solution:
I set (app/Config/core.php)...
Configure::write('debug', 2); // It was 0
Press, F5... wait... and works.
Later, I back...
Configure::write('debug', 0);
Press F5 again, and works.
Why? I no have idea.

Even if Configure::write('debug', 2); I recommend to delete the remote folder containing the translations, refresh with browser (to state that nothing is translated anymore) then reupload the translations folder.
Works perfectly that way for me.

Related

HTML/PHP/SQL - Save a HTML page

Currently I'm working on an application and i have a problem. I want to display an html page but the probem is : there is a lot of data/query behind the page. Is it possible to save the html page with the data every morning and then display the html page saved ? I dont want to load the data every time I load the page because the loading is really long.
I'm working with ZendFramwork and Oracle.
You can use either local storage or session storage for this.
HTML web storage provides two objects for storing data on the client:
window.localStorage - stores data with no expiration date
window.sessionStorage - stores data for one session (data is lost when the browser tab is closed)
Use this link to learn more (https://www.w3schools.com/html/html5_webstorage.asp)
You can use GitHub Pages, write a script in any language to send data in GitHub web page on your decided time and all done your html page in dynamic but act as a static and loads in no time
I think you want to use frontend cache.
There are at least 3 versions of Zend Framework, but the caching si very similar.
For Zend 1 there is some theory https://framework.zend.com/manual/1.12/en/zend.cache.theory.html#zend.cache.clean
Best way is set frontend cache in routes
For that, use this in your router definition file
addRoute($router, [
'url' => "[your-path]",
'defaults' => [
'controller' => '[controller-name]',
'action' => '[action-name]',
'cache' => [TIME-OF-CACHE] // 2 hours = 7200
]
]);
Then, if you really want to delete this cache every morning, you should do it manually, by some CRON script.
For that, try to use this
Zend Framework Clearing Cache
Here is the solution:
You need a cron job that runs the script (the HTML file) every morning
Add ob_start() to beginning of your HTML file
Save the buffer into a file :)
<?php
ob_start();
// Display that HTML file here. You don't need to change anything.
// Add this to the end of your file to output everything into a file.
$out = ob_get_contents();
ob_end_clean();
file_put_contents('cached.html', $out);
?>

PHP app static values in files or database? [duplicate]

What is the best approach to storing a group of global settings for a custom PHP application? I am working on a personal project (first major one really), and need a method of storing key-value pairs for recording overall settings for the application.
Things to store as...
Website's Global Name.
Theme (just a variable, or path to theme)
etc
Should I just keep them in one table? If so what is the best way to query them from a boostrap? Besides doing a single query for each desired setting.
UPDATE:
Yes a .ini or parsing an include file would be nice, and I know how to do it that way. But I wanted to know what would be the best approach to storing them in MySQL with everything else.
UPDATE2:
The reason I ask this also is I plan for a lot of these settings to be changeable through the Administrator interface. So if you were to change the Title of the site, it would be updated right away, which I figured would be best to do through SQL, thus needing setting inside the DB.
For a single, small, simple site, I'd just put config in a PHP file. Keep it simple. PHP probably doesn't parse anything faster than it parses PHP. If you use APC, the compiled bytecode is even cached -- although the bytecode is then re-executed for every request. For a small config file, this bytecode execution should take very little time; for a very large file, it might take a bit longer.
For high-traffic sites with large configs, caching your config data in APC (e.g. as a single array) is a good idea -- at the very least, you save the overhead of actually executing the statements in your config.php file. Notably, facebook does this. When you're serving many requests per second, hitting the disk to read a config file (using parse_ini_file, an XML parser, etc.) on every request is out of the question.
For my current project, we host many sites, each with their own config. Each site had both a database and a config file; however, making sure you're always using the right config file with the right database can become a headache. Additionally, changes would require changing things in two places -- the db and the config. Forgetting one or the other always caused problems, and it happened far too frequently.
We moved the config into the database, so that you can't possibly separate a db from it's correct config, and any code changes only require updating the database. The data from the config table is also aggressively cached in APC, so we query it rarely.
So, to recap:
Small site: just use a config.php file
Very large site: cache in APC
Multiple sites: store config in database to reduce administration overhead; cache in APC to reduce database hits
Have you thought about putting them in a .php file and including it on the pages you need to use them? Give the variables a unique name so avoid naming conflicts.
Since you'll be using them repeatedly in your PHP application, this would be most ideal. This also avoids the need to make database calls if you were to store them in a database.
AppSettings.php
<?php
$_SITENAME_ = 'MyWebsite';
$_THEME_ = 'Theme/Path';
?>
UPDATE:
I assume you want these settings to be editable via a web page and don't want multiple DB Queries since these settings will change, but not too often?
One approach I personally took was to serialize the AppSettings table and store it in a XML file. Of course, every time the table is updated, the table would be reserialized and stored in the XML file. Then I created a separate class that parses the XML file and returns the specific values I needed.
We just use
$siteConfig['db_name'] = 'database1';
$siteConfig['site_name'] = 'Stackoverflow';
In a included php file. Putting the values in a array helps with name conflicts.
i understand you want to keep things in a mysql table, however, that likely means storing required configuration in multiple places. for example, i'm sure you'll want the database server and name stored in a string somewhere. that means putting those in an include or .ini file since you can't read them from a database (how can you connect to the database without knowing those things). so, you'd be keeping the db connection info in an include or .ini file and the rest of the settings in the database? that works, i suppose, but i like to keep all of the settings in one file (config.php or application.ini or whatever). it makes it easier to maintain imo.
-don
Just got done chatting with a few people on IRC about this. I looked at how Wordpress handled this after I pulled up a SQL dump of one copy. I think I'll use this layout and rename the columns a bit. But the idea is...
option_id | option_name | option_value | autoload
int | varchar | longtext | varchar
(PRIMARY) | (UNIQUE) | |
I liked Microsoft.Net's web.config ConfigurationManager.appSettings and how that worked. So I mimicked it and kinda made it better. It
Works in any environment without having to swap files (which I always forget, especially when deploying on Friday at 4:59 p.m.)
Is self documenting on what the function call is
Can handle global settings with *
<?php
namespace Library {
// the config depends on the environment, and the environment depends on the website url
class Configuration {
private static $environment;
public static function GetEnvironment(){
if(empty(Configuration::$environment)){
// returns 'dev' or 'prod'
switch($_SERVER['SERVER_NAME']){
case 'innitech.com':
Configuration::$environment = 'prod';
default:
Configuration::$environment = 'dev';
}
}
return Configuration::$environment;
}
private const settings = [
"dev" => [
'dbserver' => 'localhost',
'database' => 'mydb',
'dbuser' => 'myuser',
'dbpassword' => 'mypass',
'dbdebug' => false,
'trace' => true
],
'prod' => [
'dbserver' => 'sql1.innitech.com',
'database' => 'blahinc',
'dbuser' => 'proddb',
'dbpassword' => 'ButIWasToldiDgETaStApLer',
],
'*' => [
'adminemail' => 'admins#innitech.com',
'adminphone' => '123456789',
'dbdebug' => false,
'trace' => false
]
];
public static function Setting($name){
return self::setting[self::GetEnvironment()][$name] ??
self::setting['*'][$name];
}
}
}
?>
Usage
$conn = new mysqli(Configuration::Setting('dbserver'), Configuration::Setting('dbuser'), Configuration::Setting('dbpassword'), Configuration::Setting('database'));
What has mentioned before, is true. I like the one more than the other, but I mostly use another way of storing my configuration.
I never use a database as the place to store my settings, because that would create a lot of data transfers, which can make the application a little more insecure- in my opinion. Besides, some application hosts (like Amazon's AWS and Google's Cloud Platform) limit the read/write actions to a database.
Therefore, I mostly use this method:
Firstly, I create a file config/settings.php with the following contents:
<?php
return [
'database' => [
'host' => 'localhost',
'port' => 3006,
'user' => 'username',
'password' => // your secret password
],
'application' => [
'name' => 'Your site\'s name',
'version' => '1.0-dev'
]
]
When you want to use this in you index.php file, add the following line in it:
$config = include('./config/settings.php');
I hope this can add some information for you or others.
I generally within my index.php file set up the "required" settings so:
<?php
session_start();
ob_start();
define('BASEPATH', $_SERVER['DOCUMENT_ROOT'].'/_setUp/siteSetup/'); // CHANGE TO THE PATH OF THE SITE.
define('URIPATH', 'http://localhost/_setUp/siteSetup/'); // CHANGE TO THE URL OF THE SITE.
define ('DEBUGGER', true); // CHANGE TO FALSE TO HIDE DEBUG MESSAGES
include(BASEPATH.'system/lib/config.lib.php');
?>
and within my config file:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// public
/*
example:
<img src="<?php echo IMG ?>my_image.jpg">
http://localhost/public/images/
<img src="http://localhost/public/images/my_image.jpg">
*/
define('CSS', URIPATH.'public/css/'); // DEFINE DIR: css
define('IMG', URIPATH.'public/images/'); // DEFINE DIR: images
define('JS', URIPATH.'public/scripts/'); // DEFINE DIR: scripts
// system
define('INC', BASEPATH.'system/includes/'); // DEFINE DIR: includes
define('LIB', BASEPATH.'system/lib/'); // DEFINE DIR: lib
define('SQL', BASEPATH.'system/sql/'); // DEFINE DIR: sql
if (DEBUGGER) {
ini_set('log_errors',TRUE);
ini_set("error_log", BASEPATH.'system/'."error_log.txt");
}
else {
ini_set('log_errors',TRUE);
ini_set("error_log", BASEPATH.'system/'."error_log.txt");
}
$db_info = array(
'host' => 'localhost',
'username' => 'root',
'password' => 'root',
'database' => 'my_db'
);
/*
to use:
$db_info = unserialize(DB_INFO);
echo $db_info['host'];
echo $db_info['username'];
echo $db_info['password'];
echo $db_info['database'];
*/
define('DB_INFO', serialize($db_info));
?>
A decent approach would be to fetch commonly used settings once per page, via database. Something like keeping a autoload bool field that checks whether the setting should be loaded with the page. For other, much less commonly fetched settings, you can fetch them over the air.
If you decide to cache them all instead of fetching for every page, you might want to think of a way to notify the script to reload the settings -- or you'd have to manually tell it to do so, so you wouldn't get stuck with old settings after changing some.
I'm working with a system that does store its settings in the database.
My advice in short: Do not do it!
Storing the settings in the database means, whenever we have to move the database, e.g. from production to development, we also have to update all the settings, or the dev system might start sending e-mails (did happen--made front-page news...) or interact with production systems (also happened--saved by backups...)
So, no, never store the configuration in the database!
When you store the settings in a file, local to the environment (dev, test, prod) you can move the database around at your leisure, always assured the settings will be picked up from the file in the respective environment.
Update: Giving it some more thought I'd probably go for a combination of a table (non-lethal information without server info or integration info or anything else that will kill you if it isn't environment specific) and a .ini-file (or several).
The rule would be that a key in a .ini-file would always override anything stored in the table (to prevent above disasters, maybe even make that key "read-only" from any UI).
If you want to get extra fancy you might even add value types; boolean represented as a checkbox, dates with a date selector, even select boxes with separated option values, and of course ints that would have to be numbers even if the table might store them as strings.
Then I'd look into using some form of memory-based caching if reading the settings got slow.

facing cache issue in activecollab routers

I am developing activecollab custom module; facing an issue related to Routers.
I by mistake type wrong action name in Router's action where we need to define in router, but after getting error I updated that action name but activecollabs still reading a previous action i removed files from cache and complie folders but reading previous action.
please share if you ever face this problem in development of activecollab module..
By mistake I did this: (action=>'views')
Router::map('mymodule_view', 'mymodule/view/:request_id', array('controller' => 'mymodule', 'action' => 'views' ), array('req_id' => Router::MATCH_ID) );
but after getting error i update above code by this: (action=>'views')
Router::map('mymodule_view', 'mymodule/view/:request_id', array('controller' => 'mymodule', 'action' => 'view' ), array('req_id' => Router::MATCH_ID) );
First, make sure that your system is in development mode. Open config/config.php and confirm that APPLICATION_MODE is set to in_development:
define('APPLICATION_MODE', 'in_development');
Now that you have that covered, go to activeCollab and you'll have Developer toolbar available in the lower right corner of the application interface, next to activeCollab powered button (it has a red bug icon). Use this tool to clear cache, rebuild images etc.
PS: You can also clear all files from /cache folder, just in case.

Cache only part of a page in PHP

Is it possible to cache only a specific part of a page in PHP, or the output of a specific section of code in the PHP script? It seems when I try to cache a particular page, it caches the whole page which is not want I want, some of the content in my page should be updated with every page load while others (such as a dropdown list with data from a database) only needs to be updated every hour or so.
If you are talking about caching by the browser (and any proxies it might interact with), then no. Caching only takes place on complete HTTP resources (i.e. on a per URI basis).
Within your own application, you can cache data so you don't need to (for example) hit the database on every request. Memcached is a popular way to do this.
Zend_Cache
I would probably use Zend Frameworks Zend_Cache library for this.
You can just use this component without needing to use the entire framework.
Step over to Zend Framework Download Page and grab the latest.
After you have downloaded the core files, you will need to include Zend_Cache in your project.
Zend_Cache docs.
Have you decided how you want to cache your data? Are you using a file system? Or are you memcache? Once you know which you are going to use, you need to use a specific Zend_Cache backend.
Zend_Cache Backends / Zend_Cache Frontends
You need to use a backend (how you are caching in storage what it is you want to cache) and
You need to use a frontend (how do you actually want to cache.. like using a buffer, or caching function results etc)
Backend documentation: Zend_Cache Backends
Frontend documentation: Zend_Cache Frontends
So you would do something like this...
<?php
// configure caching backend strategy
$backend = new Zend_Cache_Backend_Memcached(
array(
'servers' => array( array(
'host' => '127.0.0.1',
'port' => '11211'
) ),
'compression' => true
) );
// configure caching frontend strategy
$frontend = new Zend_Cache_Frontend_Output(
array(
'caching' => true,
'cache_id_prefix' => 'myApp',
'write_control' => true,
'automatic_serialization' => true,
'ignore_user_abort' => true
) );
// build a caching object
$cache = Zend_Cache::factory( $frontend, $backend );
This would create a cache which makes use of the Zend_Cache_Frontend_Output caching mechanisms.
To use Zend_Cache_Frontend_Output which is want you want, it would be simple. Instead of the core you would use output. The options which you pass are identical. Then to use it you would:
Zend_Cache_Frontend_Output - Usage
// if it is a cache miss, output buffering is triggered
if (!($cache->start('mypage'))) {
// output everything as usual
echo 'Hello world! ';
echo 'This is cached ('.time().') ';
$cache->end(); // output buffering ends
}
echo 'This is never cached ('.time().').';
Useful Blog: http://perevodik.net/en/posts/14/
Sorry this question took longer to write than expected and lots of answers have been written I see!
You could roll your own caching with ob_start(), ob_end_flush() and similar functions. Gather the desired output, dump it into some file or database, and read later if conditions are the same. I usually build md5 sum of the state and restore it later.
It depends on both what caching and view technologies are you using. Generally speaking yes, you can do something like this:
// if it is a cache miss, output buffering is triggered
if (!($cache->start('mypage'))) {
// output everything as usual
echo 'Hello world! ';
echo 'This is cached ('.time().') ';
$cache->end(); // output buffering ends
}
echo 'This is never cached ('.time().').';
taken from Zend_Cache documentation.
Otherwise in your example you can always make a function which returns the dropdown list and implement the cache mechanism inside that function. In this way your page is not even aware of caching.

Sugar CRM theme wont change

I am trying to override the default Sugar5 theme and I believe it worked. But its not picking up on my css. My theme directory looks like this
themes/mytheme
themes/mytheme/themedef.php
themes/mytheme/css/
themes/mytheme/css/style.css
themes/mytheme/themedef.php
<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
/*********************************************************************************
[ legal boiler plate removed ]
********************************************************************************/
$themedef = array(
'parentTheme' => "Sugar5",
'name' => "mytheme",
'description' => "Enhanced Brands",
'version' => array(
'regex_matches' => array('6\.*.*'),
),
);
But its not picking up on my css folder
So I went into config.php and changed the default_theme to mytheme
Still nothing. any ideas whats next ??
First off, you'll want to do a 'Quick Rebuild and Repair' in the Admin -> Repair section to pick up your new theme and any changes to it. Also, clearing out your browser cache will help as well.
Also, make sure the permission on your css file and css/ folder both allow read access to the webserver user.
some suggestions:
Isn't the parentTheme called "Classic"? (And not "Sugar5")
Rebuild SugarCRM via the admin->Repair to rebuild files
Make sure you clean any browser cache (since css usually are cached)
Enable developer mode in Sugar to disable any caching
Use Firebug or similar to ensure the css is loaded properly
Hope it helps.
#Kåre W. Storgaard /themes/Sugar5 is the correct theme to be changing code wise, its called "Classic" in the Interface.
However you really shouldn't be doing it in the /themes folder as this may be over written in future updates.
Instead copy the classic theme to /custom/themes/{your new theme name}, then switch the theme over to it in the user control panel.
Another thing to watch is some css in the default theme is declared twice!

Categories