Friday, April 17, 2015

How to save a form in Yii Framework

Save Form in Yii Framework:

Create a create.php in view part & add this :

<?php
$form = $this->beginWidget('CActiveForm', array(
    'id' => 'space-create-form',
    'enableAjaxValidation' => false,
    'htmlOptions' => array('enctype' => 'multipart/form-data'),
));
?>
<div class="form-group">
    <label><?php echo Yii::t('SpaceModule.views_create_create', 'Project Name'); ?></label>
    <?php print $form->textField($model, 'name', array('class' => 'form-control', 'placeholder' => Yii::t('SpaceModule.views_create_create', 'project titan'))); ?>
    <?php echo $form->error($model, 'name'); ?>
</div>
<div class="form-group">
    <label><?php echo Yii::t('SpaceModule.views_create_create', 'Business Founded'); ?></label>
    <?php print $form->textField($model, 'business', array('class' => 'form-control', 'placeholder' => Yii::t('SpaceModule.views_create_create', 'business founded'))); ?>
</div>
<?php
echo HHtml::ajaxButton(Yii::t('SpaceModule.views_create_create', 'Create'), array('//space/create/create'), array(
    'type' => 'POST',
    'beforeSend' => 'function(){ setModalLoader(); }',
    'success' => 'function(html){ $("#globalModal").html(html); }',
), array('class' => 'btn btn-primary', 'id' => 'space-create-submit-button'));
?>


In your Create Controller:

public function actionCreate()
{
if (!Yii::app()->user->canCreateSpace()) {
    throw new CHttpException(400, 'You are not allowed to create spaces!');
}
$model = new Space('edit');

// Ajax Validation
if (isset($_POST['ajax']) && $_POST['ajax'] === 'space-create-form') {
    echo CActiveForm::validate($model);
    Yii::app()->end();
}

if (isset($_POST['Space'])) {

    $_POST['Space'] = Yii::app()->input->stripClean($_POST['Space']);

    $model->attributes = $_POST['Space'];
    if ($model->validate() && $model->save()) {

        // Save in this user variable, that the workspace was new created
        Yii::app()->user->setState('ws', 'created');
        // Redirect to the new created Space
        $this->htmlRedirect($model->getUrl());
    }
}

$this->renderPartial('create', array('model' => $model), false, true);
}


In your space modal:

Firstly add your attributes in attributeLabels() function

public function attributeLabels()
{
    return array(
        'name' => Yii::t('SpaceModule.models_Space', 'Name'),
        'business' => Yii::t('SpaceModule.models_Space', 'Business Founded'),
)};


Also, add in your rules();

public function rules()
{
    $rules = array();
    if ($this->scenario == 'edit') {
        $rules = array(
            array('name,business', 'safe'),
        );
        }
        return $rules;
   }



*You must have to add in your rules(), Otherwise it will show xss errors.

Thanks

Friday, April 10, 2015

How to redirect of any url in Nginx to any another IP address of Apache2 server with same URL(proxy & reverse proxy pass)?

Redirect nginx url to apache2 ip address:

I have a website xyz.com on nginx server & any another website  xxx.xxx.xxx.xxx on apache2 server. Now i want to proxy pass xyz.com/hello to my ip address & also reverse proxy from my ip address. So, that my ip address will show on "xyz.com/hello"  this url.

For this:

Add in your nginx configuration:

location /hello {
                    proxy_pass http://xxx.xxx.xxx.xxx;
                  }


This will directly redirect  xyz.com/hello & xyz.com/hello/ to ip address xxx.xxx.xxx.xxx

Now come to apache2 configuration:

In case of wordpress set your .htaccess file

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>


Also, set in wordpress admin panel's setting

WordPress Address (URL) :  http://xxx.xxx.xxx.xxx
Site Address (URL) : http://xyz.com/hello

Or  you can do it with any another way if you are not using wordpress or any another CMS.

Now your apache2 website xxx.xxx.xxx.xxx will be work on this url http://xyz.com/hello

Thanks.