Skip to content

Instantly share code, notes, and snippets.

@b3457m0d3
Created March 22, 2015 01:29
Show Gist options
  • Save b3457m0d3/f8ebdc1714d450b11018 to your computer and use it in GitHub Desktop.
Save b3457m0d3/f8ebdc1714d450b11018 to your computer and use it in GitHub Desktop.
// clientScript
Yii::app()->clientScript->registerCoreScript(); // used for libraries i.e jquery, backbone
//using resources from the main application:
$cssFile=Yii::app()->baseUrl."/css/mystyle.css";
$jsFile=Yii::app()->baseUrl."/js/myscript.css";
$cs=Yii::app()->clientScript;
//$cs->registerCssFile( URL, MEDIA);
$cs->registerCssFile($cssFile);
//$cs->registerScriptFile(URL, POSITION);
/** POSITION OPTIONS
* CClientScript::POS_HEAD : the script is inserted in the head section right before the title element.
* CClientScript::POS_BEGIN : the script is inserted at the beginning of the body section.
* CClientScript::POS_END : the script is inserted at the end of the body section.
* CClientScript::POS_LOAD : the script is inserted in the window.onload() function.
* CClientScript::POS_READY : the script is inserted in the jQuery's ready function.
*/
$cs->registerScriptFile($jsFile);
//add raw css styling to the page
Yii::app()->clientScript->registerCss('style_css',<<<CSS
a{
color:red;
}
CSS
);
//add raw code to be executed when the page loads
Yii::app()->clientScript->registerScript("welcome_js"," alert('welcome');",1);
// params:
//- name
//- code
//- position: 0 -END OF HEAD / 1 -BEGIN OF BODY / 2 -END OF BODY
// user
Yii::app()->user->isAdmin(); // check if user is an admin
Yii::app()->user->getId(); // get the users guid
// db
Yii::app()->db->schema->getTable(TABLE_NAME) // returns null if TABLE_NAME doesn't exist
//standard querying
$user = Yii::app()->db->createCommand()
->select('username, password')
->from('tbl_user')
->where('id=:id', array(':id'=>1))
->queryRow();
//raw sql query
$sqlStatement="select username, password from tbl_user where id=:id limit 1";
$command=$connection->createCommand($sqlStatement);
$command->bindParam(':id',$userid);
$command->execute(); // a non-query SQL statement execution
$user=$command->query();
// language
Yii::app()->language || Yii::app()->getLanguage()// contains the users current default language
Yii::app()->setLanguage() // allows you to override the default language setting
Yii::app()->timeZone || Yii::app()->getTimeZone() // contains the users time zone
Yii::app()->setTimeZone() // set the time zone to wherever you want
Yii::app()->createAbsoluteUrl(“/”) // the full url, from protocol to extension
Yii::app()->createUrl(“/”) // returns the full address of the given route
// request
Yii::app()->request->setBaseUrl('yiiframework'); // set the base url of the application
echo Yii::app()->request->getBaseUrl(); // echos `yiiframework`
Yii::app()->request->isAjaxRequest
Yii::app()->request->isPostRequest
Yii::app()->request->url //returns the current url
Yii::app()->request->urlReferrer //returns the url of the previous page
Yii::app()->request->userAgent // get the browser and other technical info
// redirect anywhere in the application
Yii::app()->request->redirect(Yii::app()->createAbsoluteUrl("user/view"));
// redirect anywhere in the application with parameters
Yii::app()->request->redirect(Yii::app()->createAbsoluteUrl("user/view",array("id"=>$id)));
// Ajax
//CHtml
//button
CHtml::button(
'Button Text', // label on the button
array(
'submit' => array('controller/action') // url to submit to
));
//ajaxButton
Chtml::ajaxButton (
'Button Label', - //Label on the button
' array('URL')', - // this the url of the proccess file... or call the action function of the current controller.
' type' => 'post' - // method type Post or GET
'data' =>array(), - // Data you want to send to controller.. must be in array
'success' => 'function(data) { ---Do somthing here-- }',
);
// controllers
Yii::app()->controller->id // the name of the current controller
//Inside Controller -> $this->getId();
Yii::app()->controller->action->id // the name of the current controller action
//Redirect within controller
$this->redirect(array("admin"));
$this->redirect(array("admin",array("id"=>$id));
//Redirect between controllers
$this->redirect(array("site/login"));
$this->redirect(array("error/display",array("error"=>$error));
// files
$file_path = Yii::getPathOfAlias('webroot').$model->attachment_uri;
echo CFileHelper::getMimeType($file_path); // outputs the MIME type
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment