Yii2 :: Forms -> Working with ActiveForm via JavaScript

PHP side of ActiveForm, which is usually more than enough for majority of projects, is described well in the official Yii 2.0 guide (http://www.yiiframework.com/doc-2.0/guide-input-forms.html).  It is getting a bit trickier when it comes to advanced things such as adding or removing form fields dynamically or triggering individual field validation using unusual conditions. In this recipe … Read more

Yii2 :: Forms -> Using and customizing CAPTCHA

According to Wikipedia CAPTCHA means “Completely Automated Public Turing test to tell Computers and Humans Apart”. In other words, CAPTCHA provides a problem human can solve easily but computer can’t. The purpose of it is to prevent automated abuse such as posting comments containing links to malicious websites or voting for a particular candidate in … Read more

Yii2 :: Ajax Request

Examples Submitting Ajax form View file: <?php use yii; use yii\bootstrap\ActiveForm; use yii\helpers\Html; ?> <?php $form = ActiveForm::begin([ ‘action’ => [‘comments/ajax-comment’], ‘options’ => [ ‘class’ => ‘comment-form’ ] ]); ?> <?= $form->field($model, ‘comment’); ?> <?= Html::submitButton(“Submit”, [‘class’ => “btn”]); ?> <?php ActiveForm::end(); ?> Javascript: jQuery(document).ready(function($) { $(“.comment-form”).submit(function(event) { event.preventDefault(); // stopping submitting var data = … Read more

Yii2 :: Active Record

Remarks AR is perfect when you need to delete, update or create one or more records sequentially. Its support of dirty attributes (saving only what was really changed) results in optimized UPDATE statements which lifts the load from database significantly and reduces chances for various conflicts connected with editing same record by multiple persons at … Read more

Yii2 :: SEO essentials

1. Enable pretty URLs Sometimes users want to share your site URLs via social networks.  For example, by default your about page URL looks like http://mkchowdhury.com/index.php?r=site%2Fabout.  Let’s imagine this link on Facebook page. Do you want to click on it? Most of users have no idea what is index.php and what is %2.  They trust … Read more

Yii2 :: Handling incoming third party POST requests

By default Yii uses CSRF protection that verifies that POST requests could be made only by the same application.  It enhances overall security significantly but there are cases when CSRF should be disabled i.e.  when you expect incoming POST requests from a third party service. Additionally, if third party is posting via XMLHttpRequest (browser AJAX), … Read more

Yii2 :: Managing cookies

Managing HTTP cookies isn’t that hard using plain PHP but Yii makes it a bit more convenient. In this recipe we’ll describe how to perform typical cookie actions. 1. Setting a cookie To set a cookie i.e.  to create it and schedule for sending to the browser you need to create new \yii\web\Cookie class instance … Read more

Yii2 :: Working with different response types

Web and mobile applications are more than just rendered HTML nowadays. Modern architecture moves the UI to the client, where all user interactions are handled by the client-side, utilizing server APis to drive the frontend. The JSON and XML formats are often used for serializing and transmitting structured data over a network, so the ability … Read more

Yii2 :: URL with many parameters

There are many cases when you need to get variable number of parameters via URL. For example one may want URL such as http://mkchowdhury.com/product/male/jeans to lead to ProductController: :actionCategory where it’s expected to get an array containing male and jeans. 1   Get  Ready First of all, we need to enable pretty URLs.  In the application … Read more

How to install Yii2 framework on Xampp and SEO friendly URL

Step by step guide to Install Yii2 framework by composer or from Archive file on Xampp. Then  instruction to how to make easy to understand and internet searcher agreeable URLs for your web application. There are two Yii2 framework templates available: Basic Template – A basic frontend application template. Advanced Template – Consisting of a … Read more