Fully ajax website with Yii – Part 1

This article will be suitable for who made fully ajax site like  Gmail with best PHP Framework – Yii.
Main requirements are:

  1. All pages should be loaded via ajax.
  2. All forms should be submitted via ajax.
  3. Pages should be reachable by search engines.
  4. History support.
  5. Window title support
  6. Minimal additional code.

Here  is my solution.
I get for example simple web application which you can made with help of yiic utility.

cd /home/www/mywebsite
wget http://yii.googlecode.com/files/yii-1.1.7.r3135.tar.gz
gunzip yii-1.1.7.r3135.tar.gz
tar -xvf yii-1.1.7.r3135.tar
mv yii-1.1.7.r3135/framework/ ./
php framework/yiic.php webapp ./

That is it your application has been created!

Add this code to the protected/views/layout/main.php file before  </head> tag.


getClientScript();
  		$cs->registerCoreScript('jquery');
  		$cs->registerCoreScript('bbq');
?>
  	
  	registerScript('applychanges', $script, CClientScript::POS_HEAD);
?>	

  	registerScript('loading-indicator', $script, CClientScript::POS_READY);
?>



registerScript('ajaxlinks-and-forms', $script, CClientScript::POS_READY);
?>

Next code should be added after <body> tag

We’ve jus added javascript code which will handle all client side job
Then we extend Base controller class “Controller”.

protected/componens/Controller.php; Add new methods below:

public function ajaxRender($file, $data=array()) {
		
		$data['title'] = CHtml::encode($this->pageTitle);			
		header('Content-type: text/x-json');
		echo CJSON::encode($data);
		Yii::app()->end();

	}
		
	public function render($file, $params = array(), $data=array()) {
		
		if(Yii::app()->request->isAjaxRequest){

			if(Yii::app()->user->hasFlash('updatedata')) {
				$flashdata = Yii::app()->user->getFlash('updatedata');		
				$data = $data + $flashdata;	
			}
			
			
			$data['#content'] = parent::renderPartial($file, $params, true);
			$data['title'] = CHtml::encode($this->pageTitle);
			
			header('Content-type: text/x-json');
			echo CJSON::encode($data);
			Yii::app()->end();
			
		} else {			
			echo parent::render($file, $params, true);	
		}	
	
	}
	
	public function redirect($url) {
		
		if(is_array($url) && isset($url[0]))
			$url = $url[0];
	
		if(Yii::app()->request->isAjaxRequest){
			
			$data = array();
			
			if(Yii::app()->user->hasFlash('updatedata')) {
				$flashdata = Yii::app()->user->getFlash('updatedata');		
				$data = $data + $flashdata;	
			}
			
			$data['#content']= '';
			
			
			
			header('Content-type: text/x-json');
			echo CJSON::encode($data);
			Yii::app()->end();
				
		} else {
			parent::redirect($url);
		}
	}
	
	public function refresh() {
		if(Yii::app()->request->isAjaxRequest){

			$data = array();
			
			if(Yii::app()->user->hasFlash('updatedata')) {
				$flashdata = Yii::app()->user->getFlash('updatedata');		
				$data = $data + $flashdata;	
			}
			
			
			$data['#content']= '';
			
			header('Content-type: text/x-json');
			echo CJSON::encode($data);
			Yii::app()->end();
			
		} else {
			parent::refresh();
		}
	}

Then you have to override some Request methods to support CWebUser redirects:

Put content below to new file with name protected/components/EHttpRequest.php

request->isAjaxRequest){

			$data = array();
			
			if(Yii::app()->user->hasFlash('updatedata')) {
				$flashdata = Yii::app()->user->getFlash('updatedata');		
				$data = $data + $flashdata;	
			}
			
			$data['#content']= '';
			
			header('Content-type: text/x-json');
			echo CJSON::encode($data);
			
			Yii::app()->end();	
		} else {
			parent::redirect($url);
		}
	}
	
	public function refresh() {
		if(Yii::app()->request->isAjaxRequest){

			$data = array();
			
			if(Yii::app()->user->hasFlash('updatedata')) {
				$flashdata = Yii::app()->user->getFlash('updatedata');		
				$data = $data + $flashdata;	
			}
			
			
			$data['#content']= '';
			
			header('Content-type: text/x-json');
			echo CJSON::encode($data);
			
			
			Yii::app()->end();	
		} else {
			parent::refresh();
		}
	}
}

Set new class name for request component in your main config protected/config/main.php

It should looks like this

  .... 
  'components'=>array(
		'user'=>array(
			// enable cookie-based authentication
			'allowAutoLogin'=>true,
		),
		
		'request'=>array(
                     'class'=>'EHttpRequest',
               ),

   ....

That’s all. Now you have full ajax simple web application.
Offcourse it has some limitations and issues, further I’ll extend this article.

P.S. I have forgotten to say about styles.
Just add these lines to your css file.

#loading {
	position:fixed;
	padding:3px;
	background:#80B646;
	color:#fff;
	display:none;
	z-index:999;
	right:0;
	top:0;
}
#loading.activity {
	display:block;
}

26 thoughts on “Fully ajax website with Yii – Part 1”

  1. Hi!
    Your article is very nice, I intend to make a fully ajax website by yii, I follow your instruction and i got an error, i can not figure where the problem is, can you help me?
    I use yii 1.1.7

    This is the error message:

    Declaration of EHttpRequest::redirect() should be compatible with that of CHttpRequest::redirect()

    E:\wwwroot\ngaymua\protected\components\EHttpRequest.php(3)

    01 request->isAjaxRequest){
    10
    11 $data = array();
    12
    13 if(Yii::app()->user->hasFlash(‘updatedata’)) {
    14 $flashdata = Yii::app()->user->getFlash(‘updatedata’);
    15 $data = $data + $flashdata;

    Thanks for your help.

  2. Thanks for your update.

    Your new update also need to be applied to function redirect and refresh in Controller.php

    Now it’s work.

    Thanks so much.

  3. I’m new to Yii and Ajax. Correct me if I am wrong but this tutorial has got so many gaps in the codes that it does not work at all. Has Tpoxa actually tested this? Refresh and Redirect classes are incompatible. I had to add the following to make it work:

    – public function redirect($url,$terminate=true,$statusCode=302)
    – public function refresh($terminate=true,$anchor=”)

    JQuery registration is incomplete. No code in the body tag section. the $script variable is not assigned any value.

    appreciate it if you test before you write a tutorial

    thanks

  4. >
    you have this part broken in your code, which is about javascript, i would really like to see it )

  5. Hello, nice post, but i can’t see all the content from “protected/components/EHttpRequest.php” in this page.

    And i can’t see what to put above te part of html.

    Can you send me this information, please?

    Thank you, very much.

  6. hi… i have error
    PHP Error

    Declaration of Controller::redirect() should be compatible with that of CController::redirect()

    C:\xampp\htdocs\testyii\protected\components\Controller.php(7)

    01 <?php
    02 /**
    03 * Controller is the customized base controller class.
    04 * All controller classes for this application should extend from this base class.
    05 */
    06 class Controller extends CController
    07 {
    08 /**
    09 * @var string the default layout for the controller view. Defaults to '//layouts/column1',
    10 * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
    11 */
    12 public $layout='//layouts/column1';
    13 /**
    14 * @var array context menu items. This property will be assigned to {@link CMenu::items}.
    15 */
    16 public $menu=array();
    17 /**
    18 * @var array the breadcrumbs of the current page. The value of this property will
    19 * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}

    Stack Trace
    #0
    +
    C:\xampp\htdocs\yii\framework\YiiBase.php(421): include("C:\xampp\htdocs\testyii\protected\components\Controller.php")
    #1
    unknown(0): YiiBase::autoload("Controller")

    how i can solve this… ??

  7. ok soleve by dodgek
    – public function redirect($url,$terminate=true,$statusCode=302)
    – public function refresh($terminate=true,$anchor=»)

  8. Hello Everyone ,
    I have Successfully made my Website Ajax.
    I Did it by Following the two tutorials Given By Tpoxa.
    Cheers to Him 😀

    Now my problem is That , i am not able to change the url Structure

    Its something like this :
    projectname/#url=/projectname/news/index

    Now my desired Url is something like this :
    projectname/#!/projectname/news/index

    In Short , i just want to repalce «#url=» with «#!»

    Please Help

    Thanks in Advace 🙂

  9. I’m looking for the same solution as Lavket. Been trying to figure this out, I’ve managed to get the hashbang in there properly by using 1.3pre version of bbq and adding $.param.fragment.ajaxCrawlable( true );

    But, I still can’t remove the “url=” part for the life of me.

  10. cod delivery no rx Minocin
    generic Minocin buy online usa
    Minocin drug no prescription usa
    non prescription cheap Minocin usa
    cheap price generic Minocin

    REALLY CHEAPEST PRICES!!!

    cheapest Minocin usa order ,fedex Minocin without priscription ,Minocin usa no prescription needed ,Minocin no prescription needed mastercard ,order Minocin cod next day non presciption Minocin ,order cheap Minocin online ,order cheap Minocin in united states ,Minocin side effects ,buy Minocin europe order Minocin online ,buy Minocin no prescription ,purchase Minocin in canada online ,

  11. generic Minocin usa buy online
    cheapest Minocin usa online
    Minocin rx online usa
    buy Minocin online discount Minocin

    VIEW OUR ADDRESS BELOW

    buy Minocin from a usa pharmacy without a prescription ,how can i buy Minocin usa without prescription ,Minocin without a prescription online usa ,order cheap Minocin in canada ,order Minocin online fedex ,Minocin usa without prescription with free shipping ,buy online Minocin ,order Minocin pills generic usa ,cheapest Minocin in united states online order ,buy Minocin medicine ,order Minocin suspension ,

    REALLY CHEAPEST PRICES!!!

  12. where to buy Minocin usa without prescription
    where to buy Minocin in canada without prescription
    purchase Minocin online no doctors consult
    buy Minocin online uk purchase Minocin

    VIEW OUR ADDRESS BELOW

    sale Minocin online without prescription ,cheapest Minocin online ,buy discount Minocin online usa ,Minocin no prescription usa ,purchase Minocin online no presciption usa ,cheap Minocin usa ,fedex overnight Minocin usa ,cheap Minocin usa ,purchase Minocin in canada online ,saturday delivery cod Minocin ,cheapest Minocin in canada purchase ,

    REALLY CHEAPEST PRICES!!!

  13. generic Minocin usa without prescription
    purchase cheap generic Minocin usa
    saturday delivery Minocin overnight no prescription
    buy Minocin overnight usa

    VIEW OUR ADDRESS BELOW

    order Minocin online – order Minocin united states ,buy Minocin online pharmacy sales ,Minocin without a prescription canadian ,purchase cheap generic Minocin usa ,buy online Minocin prescriptions ,buy Minocin online usa no prescription ,saturday delivery on Minocin ,buying Minocin without a script ,buy cheap generic Minocin in united states ,buy Minocin with no prescription ,cheap Minocin no prescription next day delivery ,

    REALLY CHEAPEST PRICES!!!

  14. discount Minocin online fedex overnight
    buy Minocin from a usa pharmacy without a prescription
    where to buy Minocin usa without prescription
    how to buy Minocin on line usa

    VIEW OUR ADDRESS BELOW

    cheap Minocin overnight saturday delivery ,saturday delivery on Minocin ,i want to order Minocin without a perscription usa ,Minocin with next day delivery ,overnight Minocin cod shipping ,cheap order Minocin ,order Minocin overnight ,buy generic Minocin online in canada ,buy Minocin order Minocin online united states ,cheap Minocin online ,buy generic Minocin online usa ,

    REALLY CHEAPEST PRICES!!!

  15. Minocin order usa cheapest
    fedex overnight Minocin usa
    Minocin order cheapest usa
    Minocin purchase in canada cheapest

    VIEW OUR ADDRESS BELOW

    order cheap Minocin usa online ,buy cheap Minocin usa without prescription ,how can i buy Minocin usa without prescription ,Minocin non prescription usa ,buy cheap Minocin usa without prescription ,Minocin sale online no prescription ,buy Minocin from a usa pharmacy without a prescription ,buy cheap generic Minocin usa ,Minocin in united states buy online cheap ,Minocin overnight delivery fed ex ,cheapest Minocin usa buy online ,

    REALLY CHEAPEST PRICES!!!

  16. online prescription Minocin
    buy cheap Minocin usa
    generic Minocin buy online usa
    how to buy Minocin on line usa

    VIEW OUR ADDRESS BELOW

    cheapest Minocin usa buy online ,purchase cheap generic Minocin usa ,buy Minocin united states ,free overnight pharmacy Minocin ,purchase cheap Minocin in canada ,buy Minocin with no prescription usa ,buy cheap online pharmacy Minocin ,buy Minocin overnight cheap usa ,cheap Minocin overnight saturday delivery ,Minocin on line discount usa ,buy no prescription Minocin ,

    REALLY CHEAPEST PRICES!!!

  17. buy Minocin no prescription mastercard visa
    cheapest Minocin usa online order
    buy drug Minocin
    buy generic Minocin online in united states

    VIEW OUR ADDRESS BELOW

    nextday Minocin cash on deliver cod ,cheap Minocin overnight saturday delivery ,generic Minocin usa buy online ,cheap pharmacy to buy Minocin ,order Minocin suspension ,cheap Minocin usa online overnight delivery ,buy cheap usa Minocin online ,non prescription cheap Minocin usa ,cheap buying Minocin online ,buy online Minocin prescriptions ,order Minocin without prescription usa pharmacy ,

    REALLY CHEAPEST PRICES!!!

  18. generic Minocin in united states buy online
    buy Minocin usa purchase Minocin without prescription
    buy Minocin with no prescription
    buy Minocin online no prescription needed

    VIEW OUR ADDRESS BELOW

    Minocin overnight delivery no prescription ,cheapest generic Minocin in united states online ,order Minocin cod next day non presciption Minocin ,buy Minocin safety online usa ,buy Minocin fast shipping ,cheap order Minocin ,buy cheap Minocin in united states ,cheapest Minocin in canada online ,order Minocin usa cheap ,generic Minocin in united states without prescription ,buy Minocin online united states purchase Minocin ,

    REALLY CHEAPEST PRICES!!!

  19. Minocin cheap overnight fedex
    c o d Minocin saturday
    Minocin non prescription next day delivery
    Minocin in canada buy online cheap

    VIEW OUR ADDRESS BELOW

    cheap Minocin prescriptions ,Minocin usa online no prescription ,order Minocin next day delivery ,buy Minocin online cod ,buy Minocin europe order Minocin online ,Minocin without prescription cod ,buy cheap Minocin generic online ,how to buy Minocin online without prescription ,order Minocin with no prescription usa ,cheap order Minocin ,buy Minocin uk ,

    REALLY CHEAPEST PRICES!!!

  20. where can i buy Minocin cash on delivery
    cod delivery Minocin usa
    buy Minocin medication
    buying Minocin online

    VIEW OUR ADDRESS BELOW

    order Minocin with no prescription usa ,where to buy Minocin usa online ,Minocin without prescription cash on delivery ,where to buy Minocin usa cheap ,where to buy Minocin usa without prescription ,buy Minocin safety online ,cheap Minocin cod delivery ,buy Minocin medication ,Minocin cheap overnight fedex ,cheap online order Minocin ,buy Minocin safety online usa ,

    REALLY CHEAPEST PRICES!!!

  21. buy cheap generic Minocin in united states
    buy generic Minocin online
    Minocin uses
    purchase Minocin

    VIEW OUR ADDRESS BELOW

    buy cheap Minocin usa ,Minocin cash on delivery without prescription ,buy generic Minocin ,buy Minocin safety online ,buy Minocin safety online ,buy generic Minocin online in united states ,buy Minocin no prescription ,buy Minocin overnight usa ,generic Minocin no prescription ,generic Minocin in united states without prescription ,order cheap Minocin in united states ,

    REALLY CHEAPEST PRICES!!!

  22. how much Minocin
    buy discount Minocin generic online
    purchase online Minocin cheap
    Minocin no prescription required usa

    VIEW OUR ADDRESS BELOW

    cheap Minocin overnight ,cheap Minocin no prescription next day delivery ,purchase Minocin without prescription ,cheapest Minocin ,cheap Minocin online overnight delivery ,buy Minocin cheap ,buy Minocin usa purchase Minocin without prescription ,order Minocin online at cheapest prices usa ,Minocin sale usa ,buy cheap Minocin usa without prescription ,fedex Minocin without priscription ,

    REALLY CHEAPEST PRICES!!!

Leave a Reply

Your email address will not be published. Required fields are marked *