Yii 1.1: googlesearch

A Searchfield that uses google api to allow the user to search your entire site using google.

Thix extention is created thanks to this: http://tutorialzine.com/2010/09/google-powered-site-search-ajax-jquery/

With this widget you quickly add a searchfield to your site.

  • User searches on either your site or on the entire web.
  • Web,image,news,video search types
  • Uses Google as search engine
  • Hits are shown via ajax, "more" button to load more results
  • Fully Customizable css
  • Easy to install


Tested with Yii 1.10 in firefox, should work on all platforms though.


  1. Unpack the files to protected/extentions/search.

Your dir-structure should now be:

  • protected/extentions/search
  • protected/extentions/search/assets
  • protected/extentions/search/assets/img
  • protected/extentions/search/assets/css
  • protected/extentions/search/assets/js
  • protected/extentions/search/views
  1. EDIT protected/extentions/search/assets/js/google_search.js Change line 3:
siteURL     : 'YOUR_SITE_URL',  // Change this to your site url
  1. Optional: Style the widget in protected/extentions/search/assets/css/google_search.css You can change everything if you want to.

  2. Place this code in a view:

<?php $this->widget('application.extensions.search.GoogleSearch'); ?>

DONT FORGET You have to clean your app/assets folder after making changes to the js or css files.


Total 7 comments

#15160 report it
logos010 at 2013/10/14 03:27am
Not enough information

Hi, I think this ext could be awesome. But the author did not give any detail for the extension, I just do like readme.txt and nothing happen! Moreover, the author did not answer in the extention' forum! Why?

#8256 report it
mrtain at 2012/05/22 01:45am

You are limited to 8 pages of 8 results (64 in total), however if your website is well optimised, your user's shouldn't need any more than that.

A little addition for your JS file, should you need it:

var cursor = r.responseData.cursor;
                var resultsNum = +cursor.estimatedResultCount;
                // Checking if there are more pages with results, 
                // and deciding whether to show the More button:

                if( +cursor.estimatedResultCount > (settings.page+1)*settings.perPage){
                    $('<div>',{id:'more', text:'Next'}).appendTo(resultsDiv).click(function(){

                    resultsCount.html($('<p>',{id:'numResults', text:resultsNum+' Results'}));

stick a div with id="resultsDiv" on your results page, this will show the total result count for your search (remember the shown results are limited to 64), the other two IF statements basically add a very primitive form of pagination (by primitive, I mean 'Next' and 'Previous' buttons.)

If you want to load these results on another page, pinch the 'publishAssets() function from the main php file in the extension folder, stick it in your own searchController, render a results page with these assets registered and stick this in the .js file:

$(document).ready(function() {
                return false;

This will load the function as soon as the page has loaded, to actually get the results, I suggest wrapping your search field inside a form and storing the terms once posted as:

Yii::app()->user->setState('searchTerms', $_POST['search']);

Just remember to reference your search field correcty in the .js file:

settings.term = settings.term || $('#search').val();

I also suggest putting the un-escaped URL in the results too, this is a small change at the bottom of the .js file:

arr = [
                    '<div class="webResult">',
                    '<h2><a href="',r.unescapedUrl,'" target="_blank">',r.title,'</a></h2>',
                    '<a href="',r.unescapedUrl,'" target="_blank">',**r.unescapedUrl**,'</a>',

sorry if this is a lengthy comment, just thought you guys could benefit being shown around the code a bit :)

#8254 report it
Hesam K at 2012/05/22 01:27am

thank you for updating this page :) I knew it was just missed and you forgot to mention it, not anything else !

about the API, the problem is that we are not sure about correct and reliable functionality, for example there may be a limit for amount of search requests per day. actually i've used this search in one of my projects, but this project is not deployed yet, so i cant tell if it works well or not, I'll let everyone know when I find out.

#8247 report it
Sampa at 2012/05/21 05:00pm

I am so sorry, I wrote it in the readme.txt included in the download, I thought I wrote the same here. Updated the text here now:)

I only found that tutorial, like it, made a yii widget wrapper around it,changed it some to spare people some time to do the same themselves:p

If you check my other contributions you would see I'm always giving the original author full credit:p

About the api, this script still works, so why is it a problem?:)

#8245 report it
Hesam K at 2012/05/21 02:05pm
something is missed

nice integrating with Yii, but it really would be better if you've mentioned the real source from which you take the code! ;) and as it is mentioned on the original post's comments, this Google API is deprecated, if anyone knows how to use new API with this platform, let us know !

#8242 report it
mrtain at 2012/05/21 04:24am

Awesome extension, thanks a lot mate!

#8231 report it
bonnie at 2012/05/20 09:03am

I will give it a try this is what I have been looking for.

Leave a comment

Please to leave your comment.

Create extension