[Solved] Controller returning Javascript

Hi guys, after hundreds of unsuccessful attempts, I’ll have to ask here: how to make Yii return a Javascript controller file?

I’ll tell you why. I’m a Ext JS programmer (I’ve already used Ext4Yii) and in this framework the view is a Javascript file. With Ext JS the view is 100% Javascript.

So I was trying to figure out a way that I have 2 types of URL, e.g.:

  • index.php/sales.js

  • index.php/sales.json

The.js will return my Ext JS view "views/sales/index.js", and the .json will return data (I will have study another time how to return JSON).

Does anyone have a clue how to make this work? Would be the most suitable architecture for my webapp.

Thanks!

I think that this kind of feature (route using file extension) is not supported by the actual url manager.

You shuld extend CUrlManager (rewriting some functions) in order to make it to work.

About returning JSON, you can simply use CJson::encode.

I saw something about using urlSuffix (/post/100.html instead of /post/100), but I guess it is just another way to redirect to action.php, am I right?

p.s.: thanks for the tips

Yes, url suffix simply add a suffix to the url, in order to looks like an html page.

There is not the possibilty of writing rules about the suffix, that what I ment with my post, is anyway possible to change the suffix of some page (so is possible to create sales.js), but is not possible for the actual CurlManager to distinguish sales.js from sales.json

I think I found a silly solution for this.

First I added this code to components/Controller.php. One method gets the extension from the URL, and another checks if the extension is json.


/**

 * Returns URL Extension in lowercase

 */

protected function getExtension()

{

	$url = $_SERVER["REQUEST_URI"];

	$extension = explode(".",$url);

	$extension = explode("?",$extension[1]);

	$extension = $extension[0];

	

	return strtolower($extension);

}


/**

 * Checks if the requested URL is JSON

 */

protected function isJSON()

{

	return $this->getExtension() == "json";

}

I also added a .json route in config/main.php


'urlManager'=>array(

	'urlFormat'=>'path',

	'showScriptName' => false,

	'rules'=>array(

		'<controller:\w+>/<id:\d+>'=>'<controller>/view',

		'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',

		'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',

		'<controller:\w+>/<id:\d+>.json'=>'<controller>/view',

		'<controller:\w+>/<action:\w+>/<id:\d+>.json'=>'<controller>/<action>',

		'<controller:\w+>/<action:\w+>.json'=>'<controller>/<action>',

	),

),

Then, in my controllers, I can check if the requested URL is JSON or not (e.g. SaleController):


public function actionIndex()

{

	if($this->isJSON()) //return data

	{

		$models = Sale::model()->findAll();

		$this->renderText(CJSON::encode($models)); 

	}

	else //return view

	{

		$this->renderPartial('index');

	}

}

The urls are:

  • localhost/webapp/sale/index

  • localhost/webapp/sale/index.json

The first one is returning the view, and the other is returning the JSON data.

Forgive me if my code isn’t very professional, or if this functionality for checking URL extension already exists in Yii. Reviews are welcome (: