$this->requestAction(array('controller' => 'users', 'action' => 'get_category/10/20'));
Monday, April 22, 2013
cakephp call controller action from ctp file
Convert string to sha1 using java
<?php public static String hash(String toHash) { if (toHash == null || toHash.equals("")) return null; StringBuffer hexString = new StringBuffer(); try { java.io.StringReader sr = new java.io.StringReader(toHash); java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream(); int ch; while ((ch = sr.read()) != -1) { baos.write(ch); } //SHA-1 Hash value of data MessageDigest sha = MessageDigest.getInstance("SHA-1"); sha.reset(); sha.update(baos.toByteArray()); byte[] digest = sha.digest(); /* NB: This conversion strips the leading 0 from any * digest byte with a value less than 16. * * This weakens the security of the SHA-1 hash and requires * workarounds on the server to overcome this. * * It's all quite embarrassing. -- PB */ for (int i=0;i<digest.length;i++) { hexString.append(Integer.toHexString(0xFF & digest[i]).toUpperCase()); } } catch (java.io.IOException ioE) { System.out.println(ioE); } catch (NoSuchAlgorithmException algE) { System.out.println(algE); } return hexString.toString(); } ?>
How to add Meta Description Tags
- Go to Settings > Search Settings and Enable Meta Tags. In the Text area give a 150 character description which will describe your Blog. This text might be used by Search engines when your Blog’s home page is displayed on Search Engines. Here is the snippet that I have given “Blogger Widgets provides you the best quality blogger tutorials.It also provides you with free blogger widgets and gadgets to build a better blog.”
- Now when you make a Post, you can set the Meta Description from the Post Editor’s Post
-
If you are having a custom template, make sure that the following line of code is present in your template. To do that go to Template and Proceed to Edit HTML . Find
<b:include data='blog' name='all-head-content'/>
If it's not present, add it just before </head>
Sunday, April 21, 2013
Yii throw 404 custom exception
You need to define a view file under protected/views/site folder (any folder and that shoud be configured in config/main.php file as below) suppose named error.php, and throw CHttpException from where you want to show error page.
Define error view in config/main.php file:
'errorHandler'=>array(
// use 'site/error' action to display errors
'errorAction'=>'site/error',
)
That means a file named error.php shoud be in views/site/ folder.
Throw 404 custom exception using the following php code:
throw new CHttpException(404,'The requested page does not exist.');
And the error.php file look like:
<?php
$this->pageTitle=Yii::app()->name . ' - Error';
$this->breadcrumbs=array(
'Error'
);
?>
<h2>Error <?php echo $code; ?></h2>
<div class="error">
<?php echo CHtml::encode($message); ?>
</div>
Define error view in config/main.php file:
'errorHandler'=>array(
// use 'site/error' action to display errors
'errorAction'=>'site/error',
)
That means a file named error.php shoud be in views/site/ folder.
Throw 404 custom exception using the following php code:
throw new CHttpException(404,'The requested page does not exist.');
And the error.php file look like:
<?php
$this->pageTitle=Yii::app()->name . ' - Error';
$this->breadcrumbs=array(
'Error'
);
?>
<h2>Error <?php echo $code; ?></h2>
<div class="error">
<?php echo CHtml::encode($message); ?>
</div>
Yii upload image or file to server
Model Image:
Controller ImageController.php
View file
<?php class Image extends CActiveRecord { public $uploaded_owner_img; public static function model($className=__CLASS__) { return parent::model($className); } public function tableName() { return 'table_name'; } public function rules() { return array( array('owner_img', 'file', 'allowEmpty'=>true,'types'=>'jpg','maxSize'=>1024*1024*1, 'tooLarge'=>'Image must be less than�1MB'), array('owner_name, owner_img', 'required'), array('owner_name', 'unique'), array('owner_name', 'length', 'max'=>20, 'encoding'=>'utf-8'), array('owner_name', 'safe', 'on'=>'search'), ); } public function attributeLabels() { return array( 'owner_img' => 'Select image.', 'owner_name' => 'Name' ); } protected function beforeSave() { if (parent::beforeSave ()) { if ($this->isNewRecord) { $this->created = date('Ymd'); } return true; }else { return false; } } }
<?php class ImageController extends Controller { public function actions() { return array( // captcha action renders the CAPTCHA image displayed on the contact page 'captcha'=>array( 'class'=>'CCaptchaAction', 'backColor'=>0xFFFFFF, ), // page action renders "static" pages stored under 'protected/views/site/pages' // They can be accessed via: index.php?r=site/page&view=FileName 'page'=>array( 'class'=>'CViewAction', ) ); } public function actionCreate() { $model=new Image; //AJAX validation is needed $this->performAjaxValidation($model); if(isset($_POST['Image'])) { $model->attributes=$_POST['Image']; if (@!empty($_FILES['Image']['name']['owner_img'])){ $model->owner_img = time().".jpg"; if ($model->validate(array('owner_img'))){ $model->uploaded_owner_img = CUploadedFile::getInstance($model, 'owner_img'); $model->uploaded_owner_img->saveAs(Yii::app()->basePath.'/'.$model->owner_img); if($model->save()){ echo "Model saved"; die(0); } } } } $this->render('create',array( 'model'=>$model, )); } protected function performAjaxValidation($model) { if(isset($_POST['ajax']) && $_POST['ajax']==='map-form') { echo CActiveForm::validate($model); Yii::app()->end(); } } }
<?php $this->breadcrumbs=array( "image/create" ); $this->menu=array( ); ?> <h1>Image Upload</h1> <div class="form"> <?php $form=$this->beginWidget('CActiveForm', array( 'id'=>'map-form', 'enableAjaxValidation'=>false, 'htmlOptions' =>array('enctype'=>"multipart/form-data" ), 'enableAjaxValidation' => false, 'enableClientValidation'=>true, 'clientOptions'=>array('validateOnSubmit'=>true) )); ?> <p class="note">Mark <span class="required">*</span> are required.</p> <?php echo $form->errorSummary($model); ?> <div class="row"> <?php echo $form->labelEx($model,'owner_name'); ?> <?php echo $form->textField($model,'owner_name',array('size'=>32,'maxlength'=>20)); ?> <?php echo $form->error($model,'owner_name'); ?> </div> <div class="row"> <?php echo $form->labelEx($model,'owner_img'); ?> <?php echo $form->fileField($model,'owner_img'); ?> <?php echo $form->error($model,'owner_img'); ?> </div> <div class="row buttons"> <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?> </div> <?php $this->endWidget(); ?> </div>
YII session control
{Write}
Yii::app()->session['url']='http://pritomkumar.blogspot.com'; Yii::app()->session->add('author','Pritom Kumar');
{Read}
echo Yii::app()->session['url']; //http://pritomkumar.blogspot.com echo Yii::app()->session->get('author'); // Pritom Kumar
YII set and get flush message
In controller:
Yii::app()->user->setFlash( 'Message00', //a string for key usage 'This is msg from controller.' );
In view:
if(Yii::app()->user->hasFlash('Message00')){ echo '<div class="flash-success">'; echo Yii::app()->user->getFlash('Message00'); echo '</div>';
}
Subscribe to:
Posts (Atom)