Tips: Yii framework as server-side for mobile apps


I thought to share some tips from my expirience of using Yii Framework as a server-side for mobile apps. if you got more tips, you can post it in the comments, i’m sure it will help more developers.

1/// Ajax calls, the easy way

because you dont always have access to the mobile code it self, you need a tool to produce Ajax calls to your server, and to do it in an easy, flexible way. i found a great Chrome extention. you can download it here.

all you have to do now is enter the requested URL, choose the type of request and enter a key and a value. i uset a POST request, so the key is the name of the request and the value is the json object.

2/// Render View

well, the good news is you don’t need to render the view any more. actually, you can delete the view folder (if you don’t need it for let’s say visual debuging) and of course you can avoid using the render code:


3/// Send Json recieve Json

how do we send an object as a string? with the Json format. so you will send your data from the mobile app as Json and you should recieve it as Json. in this example i show how i structured a Controller class:

class ProfileController extends Controller
public function actionUpload()
private $response = array();

if (isset($_POST['Register'])) {
$model = new User;
$model->scenario = 'Register';

// now we are going to take a json string that was sent from the ajax call and convert it to an array
$model->attributes = CJSON::decode($_POST['Register'], true);

if ($model->validate()) {
// do something
$test = false;
if ($test) {
// when $test is true do something
} else {
// we can add an error with a key name and a description to our model
$model->addError('TestFalse', Yii::app()->params->errorsText['TestFalse']);

$this->response['errors'] = $model->errors;
echo CJSON::encode($this->response);

private function checkResponse()
if (empty($this->response['errors'])) {
$this->response['success'] = true;
} else {
$this->response['success'] = false;

the ajax callback will look something like that:

{"errors":{"UserExist":["User exist Already"]},"success":false}

4/// Security

obviously, we need to take care of some security issues. if the mobile app request the data with ajax, any one can do it. so, you have to decide which data is public and which data is private.

in order to secure the private data, you need to pass with your ajax call a token / password or any data that the server-side will recieve and check that this request comes from a genuine user. only if the server-side auth process is fine you should return the data.

those are the big things i’ve noticed. if you have some more tips, please post them in the comments.


Comments on this post

No comments.

Leave a Reply