PHP - Yii Framework tutorials, Extension, Component Devlopment

Monday, 23 December 2019

How to install Yii in different ways


How many ways to install Yii2


Using Composer is one of the best way to install the Yii because composer way allowing developers to install new extensions and upgrade the framework to the latest version.

Another way is to download the archive file.


Lets start with composer approach.


Installing via Composer

Open command prompt (cmd) for windows or terminal for ubuntu and type

composer --version

D:\Xampp\htdocs\yii2>composer --version
Composer version 1.8.6 2019-06-11 15:03:05

If you found above output then make sure you are using latest version so trigger below command.

D:\Xampp\htdocs\yii2>composer self-update

If you do not found composer installed then firstly install the composer.
For windows user, as always most of the time, use Composer-Setup,exe
For Linux and Mac OS X, you will run following commands.

curl -s http://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer

Once composer has been installed then now we are good to go with the Yii installation.

D:\Xampp\htdocs\yii2>composer global require "fxp/composer-asset-plugin:1.0.0"

D:\Xampp\htdocs\yii2>composer create-project --prefer-dist --stability=dev yiisoft/yii2-app-basic .

Please note, i have added .(dot) at the last position which means installing yii into the current directory(D:\Xampp\htdocs\yii2) only.


Note that the development version of Yii should not be used for production as it may break your running code.

Installing Yii from an archive file 


Installing Yii from an archive file involves three steps:

1. Download the archive file from yiiframework.com.
2. Unpack the downloaded file to a Web-accessible folder (D:\Xampp\htdocs\)
3. Open the config/web.php file by entering a secret key for the cookieValidationKey configuration item.

   'cookieValidationKey' => 'enter your secret key here',

How to verify Installation


http://localhost/yii2/web/index.php
OR
http://127.0.0.1/yii2/web/index.php

Be aware the I have assumed that Yii installed in a directory named basic under D:\Xampp\htdocs\ directory or /var/www/html/ for linux.
You may need to adjust it to your installation environment.

Yii requirements 

Open URL http://localhost/yii2/requirements.php

Thursday, 12 February 2015

Yii simple ajax form validation

Here's how Yii implement AJAX validation in CActiveForm :


1)
Code for Form declaration generally found in _form.php

<? php $form = $this->beginWidget('CActiveForm', array(
'id'=>'sandy-form',
'enableAjaxValidation'=>true //switch on Ajax Validation on the client side
)); ?>
<? php echo $form->errorSummary($model); ?>

<div class="form-group">
<? php echo $form->labelEx($model,'title'); ?>
<? php echo $form->textField($model,'title',array('size'=>60,'maxlength'=>255)); ?>
<? php echo $form->error($model,'title'); ?>
</div>
<? php $this->endWidget(); ?>

2) put your validation code in model file found in models folder.

Model File

public function rules()
{
return array(
array('title', 'required'),
array('title', 'length', 'max'=>255),

  );
}


3) Also put code corresponding controller file.
 
public function actionCreate()
{
if(Yii::app()->getRequest()->getIsAjaxRequest()) {
echo CActiveForm::validate( array( $model));
Yii::app()->end();
}
}
public function actionUpdate($id)
{
$model=$this->loadModel($id);
if(Yii::app()->getRequest()->getIsAjaxRequest()) {
echo CActiveForm::validate( array( $model));
Yii::app()->end();
}
}

I hope you understand well.

Notes: Validation fires every time you tab out of a (changed) html form field. 
From the javascript/jquery point of view it works with blur function.


Wednesday, 11 February 2015

Yii 1.X Framework Migration Tips

Yii Framework Migrations - based on tool(1.1.14) 

Straight Serial

1) Modify config.php under protected/config. Also change in main.php same directory.

2) Come to Command Prompt. (Type "cmd" in run menu)

3) Go to the project directory . e.g. :D\wamp\www\phpproject\protected

Type "php.exe yiic migrate create san"

where, "san" is the migrate php file

It will ask you to confirm to create migration or not.

Press Y to confirm.

New migration created successfully. The file would save in protected/migrate directory. 

Executing the migration.
 Go to the project directory . e.g. :D\wamp\www\phpproject\protected

Type "php.exe yiic migrate".

Migration file will restore your db in localhost server. 

Yii remove index.php from URL and SEO URL tips


This one is using for removing index.php and also remove site portion from URL.

I am trying to remove index.php from the URL string for so many times. Finally i got some cases in which i want to share with you guys.


Verify mod_rewrite is enabled in your server you can check it to load php.ini file.

Just copied following code and paste into urlManager in main.php
Here's what I put under components file 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>',
'/'=>'site/index',
'blog'=>'post/index',
'<view:(about)>'=>'site/page',
'contact'=>'site/contact',
'login'=>'site/login',
),
//'urlSuffix'=>'.html',
),

Also,
You can make your own rule to your web page need:
Here, i have changed site/login to login and site/contact to contact.
Also , post/index to blog as you can see in the rules array.

Hope this is one helps you in many ways.




How to set Yii description tag and Meta keywords

Here I have put some stuff on how to set yii description tag and meta tag in head tag.

You can use as follows:

<?php 

//Setting yii Description

Yii::app()->clientScript->registerMetaTag('Welcome to Yii Description -  Yii frameworrk', 'description', null, array('lang' => 'en'));

// Setting yii set keywords

Yii::app()->clientScript->registerMetaTag('yii, php framework', 'keywords', null, array('lang' => 'en'));

?>


All you need to register is meta as simple we do in normal web page head struture.