I just received a copy of Yii 1.1 Application Development Cookbook from Packt, and I’m already done reading it!. It’s a great book if you are working with the Yii Framework. Stay tuned for the full review!

  • Learn to use Yii more efficiently through plentiful Yii recipes on diverse topics
  • Make the most efficient use of your controller and views and reuse them
  • Automate error tracking and understand the Yii log and stack trace
  • Full of practically useful solutions and concepts that you can use in your application, with clearly explained code and all the necessary screenshots

Short story: Rendering a view over ajax in Yii Framework might include JS and CSS files. There is no way for Yii to know that some of these files might already be present in your DOM. Having your browser re-download and add duplicates of these files can or will lead to problems. A short snipped of JS code will filter away all JS and CSS references from incoming content, while leaving inline script and style intact. No server-side changes required. Scroll down for code.

Longer story: If you attempt to render a view with processing of script in Yii Framework you’ll end up with script tags for whichever scripts the components in your view requires. For example, if you have a view which uses the CListView component, you’ll end up with jquery.listview and jquery.js. This is pretty much what you would expect when rendering a view, but it will lead to problems if you are rendering this in an ajax response and the scripts in question are present in the DOM. Yii has no way to know that you have already executed a request, which returned these scripts. There has been some talk on the Yii Framework forums on how to tackle this problem, but so far I’ve not seen any implementations or hacks that’s been satisfactory.

Server side tracking of client scripts
One way to go is to keep track of the script files that has been sent to the client on the server, and reset this state when a full page request is executed. This might seem like a good idea, but once a user starts browsing with several tabs at the same time it gets complicated. It will require hacks to prevent resetting in cases where requests fail midways etc. The state must be kept not only on a path basis, but also for submitted content (ie ajax post), which would further complicate things.

My proposal is to use jQuerys dataFilter hook (via $.ajaxSetup), which is invoked after ajax request and allows you to filter any data returned before it’s passed on to ordinary success-handlers etc.

Update: Thanks to NLAC for some code improvements.

Simply include this script after jQuery, or add this function to your existing code:

$.ajaxSetup({
	global: true,
	dataFilter: function(data,type){
		//  only 'text' and 'html' dataType should be filtered
		if (type && type != "html" && type != "text")
		{
            		return data;
		}
 
		var selector = 'script[src],link[rel="stylesheet"]';
 
      		// get loaded scripts from DOM the first time we execute.
        	if (!$._loadedScripts) {
			$._loadedScripts = {};
			$._dataHolder = $(document.createElement('div'));
 
            		var loadedScripts = $(document).find(selector);
 
			//fetching scripts from the DOM
		        for (var i = 0, len = loadedScripts.length; i < len; i++) 
			{
        		        $._loadedScripts[loadedScripts[i].src] = 1;
            		}
        	}
 
		//$._dataHolder.html(data) does not work
		$._dataHolder[0].innerHTML = data;
 
		// iterate over new scripts and remove if source is already in DOM:
		var incomingScripts = $($._dataHolder).find(selector);
		for (var i = 0, len = incomingScripts.length; i < len; i++)
		{
			if ($._loadedScripts[incomingScripts[i].src])
			{
	        	        $(incomingScripts[i]).remove();
            		}
            		else
            		{
                		$._loadedScripts[incomingScripts[i].src] = 1;
            		}
        	}
 
		return $._dataHolder[0].innerHTML;
	}
});

If you have improvements for this code, please email me at eirikhm@gmail.com

PHP 5 Social Networking cover

The contest is now over and the winners are picked! Each will receive a copy of the book in eBook format. A individual notification email will also be sent.

And the winners are: Greg, Michael and Joseph!

Spend time in terminal? Here is a preview of an upcoming book review for the book Linux Shell Scripting Cookbook from Packt!

Book coverA brief from the book description:

Linux Shell Scripting Cookbook is a collection of essential command-line recipes along with detailed descriptions tuned with practical applications. It covers most of the commands on Linux with a variety of usecases accompanied by plenty of examples. This book helps you to perform complex data manipulations involving tasks such as text processing, file management, backups and more with the combination of few commands.

You can check out the sample chapter Put on the Monitor’s Cap while waiting.

PHP 5 Social Networking cover

Contest is now over! Stay tuned for winners!

Want a chance to win the PHP 5 Social Networking book from Packt Publishing? Three copies are up for grabs in after a 14 day contest.

How to Enter

All you have to do is comment on why you’d like a copy of the book in the comments.  You can take a look at the book’s page at Packt for inspiration and additional information.

During, this contest, we’ll work with tokens. The more tokens you have, the greater your chance is of winning the a copy of the book. Leaving the answer in the comment field will provide you with one token, and tweeting about this giveaway will provide one. If you do both, we’ll give you not two, but three tokens. If you tweet about it, please link the tweet in the comment! My tweet is available here!

Deadline

The deadline for participating is March 10th, 2011. Any entries after that date will be discarded. Three winners will be picked at random, and announced shortly after the contest ends. Good luck!

Book review: PHP 5 Social Networking
ISBN: 1849512388
ISBN 13: 78-1-849512-38-1
Publisher:Packt Publishing
Author:Michael Peacock

PHP 5 Social Networking cover

Short story: If you are fresh to social networks, this book will get you up and moving. It explains how to build a fully featured social network, based on a custom underlying framework. At the end of the book you will have a functional site, and know the what’s what of social networks. It’s very easy to follow the author, and the code is very readable. New concepts and features are introduced in a way that makes it easy for the reader to keep up.


PS: Packt currently has a 40% off special offer on this book, so I recommend you pick it up via their website!


Slightly longer story: I must admit that I’m having problems seeing the value of rolling your own MVC framework when building applications like this. The main arguments made in favor of going custom is ease of maintenance, licensing and code efficiency. I really don’t buy these, as there are several good frameworks available under the BSD license, which are proven time upon time (Yii, Zend, Code Igniter, Symfony). Not only is the code maintained by teams of highly skilled developers, but also reviewed for security holes and exploits by the less good-mannered of our beloved internet. A *very* important aspect of social networking sites is security, and I feel that the book should spend more time on that subject (maybe including info on cross site scripting and related).
However, the one argument I really do see the value in is the process of learning. Writing a PHP framework is a very valuable exercise, and if you still have that on your TODO-list, this book should serve as a good guide on the way. The framework produced will allow you to keep extending and improving features after you are done with the material covered in the book.

Heads up! New book from Packt Publishing called PHP 5 Social Networking!

I just received a copy of PHP 5 Social Networking in eBook format, and plan on getting started reading it this week. The book looks really interesting, and closely matches my daily work as I’m now doing social networks on a full-time basis.

PHP 5 Social Networking cover
This book will demonstrate how to create, maintain, and extend a powerful social network site. It shows how to create your own flexible social networking framework, allowing users to sign up, customize their profile, befriend other users, share information, communicate, interact with one another, and much more.

You can check out the sample chapter Public and Private Messages right now.

Short story: Open up /lib/udev/rules.d/70-hid2hci.rules and find the following line:

KERNEL==”hiddraw*”, ATTRS{idVendor}==”046d”, ATTRS{idProduct}==”c70[345abce]|c71[34bc]“,
RUN+=”hid2hci –method=logitech-hid –devpath=%p”

Comment those lines out as follows:

# KERNEL==”hiddraw*”, ATTRS{idVendor}==”046d”, ATTRS{idProduct}==”c70[345abce]|c71[34bc]“,
# RUN+=”hid2hci –method=logitech-hid –devpath=%p”

Save, relog. Did the charm for me. If it does not work, read up on the link from above. There are other methods involving some changes to those lines.

Somewhat longer story: I installed Ubuntu 10.04 on my desktop PC this weekend. Everything went pretty smooth, that was up to the login screen. To my amazement my Logitech MX5500 keyboard refused to work. I managed to log in using the onscreen keyboard, but when i got inside Gnome I could not get it back up. After some pretty intensive copy-and-paste action from the FireFox homepage I was able to search for “onboard” and get that back up running. Google lead me to a Ubuntu Bug Report detailing this issue,

Book review: Expert PHP 5 Tools
ISBN: 1847198384
ISBN 13: 978-1-847198-38-9
Publisher:Packt Publishing
Author:Dirk Merkel

Book cover: Expert PHP 5 Tools

Book cover: Expert PHP 5 Tools

Short version: If you are working with PHP you’ll want to read this book. Covering topics like continuous integration, documentation and unit testing thoroughly it gives an insight of great value to any PHP developer.

Slightly longer version: This book is aimed at developers already working with PHP on a daily basis, or developers which are picking up PHP. It’s main focus is the environment of the PHP developer. Areas like unit testing, continuous integration, application deployment, proper documentation and version control might already be very familiar if you have been working with PHP for a while, but this book covers each of these important areas in detail and with prime examples on do and dont’s. Even if you feel like you are doing quite well I’m sure this book will give you a few “aha!” moments. If you are coming from a hobby-background these areas might not be your strongest suit, and this book will help you get a firm grasp on what a professional PHP setup is all about. The chapter on continous integration is based around phpUnderControl, which is a nice piece of software for this purpouse. However, I would have liked to see other alternatives like for example Hudson. Luckily, moving over to a different CI app is fairly easy after having set up a phpUC installation with the phing/ant configuration.

The book is available both as paperback and eBook from Packt. I’ve been reading the eBook, which worked quite well for me. PS: Convering the PDF to ePub will destroy some of the formatting.

Check out the sample chapter Documentation with phpDocumentor

Book cover: Expert PHP 5 Tools

Book cover: Expert PHP 5 Tools

While I am currently reading PHP5 eCommerce Development I’ve also got lined up a copy of Expert PHP 5 Tools from Packt.

This book will enable you to take your PHP development skills to an enterprise level by teaching you the skills and tools necessary to write maintainable and efficient code. You will learn how to perform activities such as unit testing, enforcing coding standards, automating deployment, and interactive debugging using tools created for PHP developers – all the information in one place. Your code will be more maintainable, efficient, and self-documented.

Check out the sample chapter Documentation with phpDocumentor