Posted by Andy Nagai on Jan 25, 2012
If you select a menu item that is registered or special access and you are not logged in, Joomla will redirect to a default login page.
This is the default login redirect route:
index.php?option=com_user&view=login&return=
What if you want a different login path or go to a custom login page based on some condition. You can accomplish this by modifying the default redirect code in JSite::authorize(). JSite is located in /includes/application.php.
Posted by Andy Nagai on Nov 02, 2011
Seems that all these rapid releases Mozilla has made has finally paid off. There are reported substantially improved memory usage and no reports of massive memory leaks.
Mozilla has been taking great efforts in reducing the memory usage problems. They have a project named MemShrink just to focus on this.
Here is a good article about Firefox 7 memory and javascript performance.
Posted by Andy Nagai on Aug 14, 2011
Memoization is an optimization pattern, which involves caching results that a function returns. Any subsequent calls to function to calculate same computation will instead retrieve from the result cache. This avoids wasting valuable computer resources by not repeating the same calculation.
This first method caches single parameter method calls.
class Util {
function expCalc($param) {
static $cache;
if(!$cache[$param]) {
$result = 'expensive calculation';
$cache[$param] = $result;
}
return $cache[$param];
}
}
This method will add the result to cache as an array hash value for each different parameter value passed to it. The cache variable is static so it maintains its value for each method call.
You can call the method statically or as an instance method.
$val = Util::expCalc(1);
This second method shows how to handle multiple parameters in the method call.
class Util {
function expCalc($param1,$param2,$param3) {
static $cache;
$key = json_encode(func_get_args());
if(!$cache[$key]) {
$result = 'expensive calculation';
$cache[$key] = $result;
}
return $cache[$key];
}
}
The only difference in this method and previous is the way the cache array key is generated. This will get all the arguments passed to method as an array and turn it into a JSON formatted string as the array key. Arguments can be any combination of strings, integers, arrays and objects. The serialize() function can also be used to make the key but json_encode() makes a more compact key. You will need at least PHP 5.2+ to use the JSON functions.
Posted by Andy Nagai on Aug 04, 2011
When inserting a value from PHP script into javascript code you should be careful to properly format the text so it will not break your javascript. These are two ways of properly formatting the text to be inserted into a javascript variable.
You can first url encode the value in php then decode it in javascript. Try something like this from your php script.
<script type="text/javascript">
var list = decodeURIComponent("<?php echo rawurlencode($mySelectList) ?>");
</script>
If you have PHP 5.2 + you can use json_encode to properly escape the text. It will also put in the outer quotes for the value for you.
<script type="text/javascript">
var list = <?php echo json_encode($mySelectList) ?>;
</script>
Posted by Andy Nagai on Jun 19, 2011
In the future release of Chrome you will be able to pre-render the page before it is displayed.
Prerendering a page is when you tell the browser ahead of time what page the user is most likely to visit next and the browser will pre-load and pre-render the page in the background. This would be useful in articles that consist of multiple pages where user is very likely to click to the second page of the article. The second page will just pop open instantly because its already completely rendered.
This is accomplished by adding a special link tag with rel=prerender and a url that tells the browser what page to prerender. Currently it is a experimental feature in Chrome 13.
Here is more information from Google labs.
Posted by Andy Nagai on May 27, 2011
I have been a faithful user of Firefox since version 2.0. It was my favorite browser for browsing and developing on. I only used it for developing because of the firebug extension. Over the years it has gotten steadily worse as far as memory usage in each successive version.
Firefox 4 is the worst of them all in term of memory leaks. I normally have 4 to 6 tabs open continuously. After a length of time memory usage varies from 500mb to over 1gig. Memory usage will just keep going up even if you just leave your computer with the tabs open to static pages. When it gets to 1gig of memory usage my computer just slows to a crawl. I am just sick and tired of this slowness.
There is a post of Mozilla support forum here about the FF 4 memory leak problem someone experienced.
Here is a thread on Mozilla’s Facebook page about the problem.
Here is the bug on bugzilla.
Switching to Chrome
I have found a much better alternative to firefox as far as performance and memory usage. Have been using Google’s Chrome version 11 browser for past few weeks and I find it does not have a memory leak problem. I have the same number of tabs open as before and the memory usage hovers around 40mb per tab on my Mac running Snow Leopard. When a tab is closed the memory is released. Seems to run very efficiently.
Chrome also has a built in developer tool that has most of the features of firebug and some features firebug does not have. It has a audit feature that will analyze your page and give you tips on how to improve performance. Looks like they incorporated their Page Speed tool into it. Overall the browser just seems more responsive and efficient. I will just be using Firefox for testing and Chrome for all of my other browser needs.
Posted by Andy Nagai on May 25, 2011
There are times when you want to display a message in the message bar that goes across the page when not doing a redirect in your controller. Normally status messages are set when redirecting using the setRedirect() method.
You can set the message bar to display within the view layout itself by directly accessing the message queue with JApplication::enqueueMessage();
This will display a blue or green message status bar across the page as long as you have the message module tag in your template index.php file.
$app = &JFactory::getApplication();
$app->enqueueMessage("File was submitted successfully!");
This will display a red error status bar with your error message:
$app = &JFactory::getApplication();
$app->enqueueMessage("Error encountered. File not Submitted!","error");
Posted by Andy Nagai on May 23, 2011
It is a good idea on most light to medium traffic sites to have keep-alive active on the server. This will allow a single client request to use the same connection thread on all file requests for the page. The disadvantage is that on high traffic sites this can keep many concurrent connections open using alot of server resources and potentially blocking users from accessing your site. In this article linked below it discusses the benefits and drawbacks of keep-alive.
One solution the author proposes is to set the KeepAliveTimeout to 2 seconds which allows enough time for all the files to be requested from the client. This should provide benefits of single connection per page request and not blocking requests for high concurrent sites.
You can view the well detailed article with additional performance tips in the comments here.
Posted by Andy Nagai on May 21, 2011
It is possible to create connections to multiple databases using the JDatabase::getInstance() method. The following link points to a tutorial that describes how to create a helper file that makes it easy to create multiple JDatabase instances that point to different databases. You can create the database instances in the following manner using the custom helper class.
$db = JFactory::getDBO();
$db2 = MyDataHelper::getDBO2();
$db3 = MyDataHelper::getDBO3();
You can still get the default database object normally using JFactory.
The full tutorial on creating this helper is found here.
Posted by Andy Nagai on May 13, 2011
When your doing simple single table inserts and updates you can use the JDatabase methods to do so. The advantages of using the JDatabase methods is it saves you time from hand coding your sql and will escape and correctly quote your inputs for you.
The following will insert a new record into a table:
$db = JFactory::getDBO();
//Create data object
$row = new JObject();
$row->title = 'The Title';
$row->author = 'Bob Smith';
//Insert new record into jos_book table.
$ret = $db->insertObject('jos_book', $row);
//Get the new record id
$new_id = (int)$db->insertid();
This will update an existing record:
$db = JFactory::getDBO();
//Create data object
$row = new JObject();
//Record to update
$row->rec_id = 200;
$row->title = 'The Title';
$row->author = 'Bob Smith';
//Update the record. Third parameter is table id field that will be used to update.
$ret = $db->updateObject('jos_book', $row,'rec_id');
You can also use JTable to insert and update records, but requires more initial setup since you have to create a new JTable class for each table you want to modify. Using JTable is preferred if table has many fields to update from a form submit. JTable will automatically bind the form fields to corresponding table fields using the bind() method.
Here is more info on using JTable.