Yii2 Request Lifecycle

The following diagram describe how an application handles a request and response. You are also requested to scroll down to read the step..

  1. A user makes a request to the entry script web/index.php.
  2. The entry script loads the application configuration and creates an application instance to handle the request.
  3. The application resolves the requested route with the help of the request application component.
  4. The application creates a controller instance to handle the request.
  5. The controller creates an action instance and performs the filters for the action.
  6. If any filter fails, the action is cancelled.
  7. If all filters pass, the action is executed.
  8. The action loads a data model, possibly from a database.
  9. The action renders a view, providing it with the data model.
  10. The rendered result is returned to the response application component.
  11. The response component sends the rendered result to the user’s browser.

Yii2 with Netbeans

NetBeans IDE lets you quickly and easily develop Java desktop, mobile, and web applications, as well as HTML5 applications with HTML, JavaScript, and CSS. The IDE also provides a great set of tools for PHP  developers. It is free and open source and has a large community of users and developers around the world.      Download Now       must have JDK download           

Simple Pagination with php-mysql

Simple pagination code for PHP developers.
Step 1 :Create database name “pagination” in mysql using phpMyadmin.

-- Table structure for table `exclusive_news`

CREATE TABLE `exclusive_news` (
  `id` int(2) NOT NULL,
  `post_id` int(6) DEFAULT NULL,
  `date` varchar(25) CHARACTER SET latin1 NOT NULL,
  `title` longtext CHARACTER SET utf8 COLLATE utf8_general_mysql500_ci NOT NULL,
  `content` longtext NOT NULL,
  `media` varchar(500) CHARACTER SET latin1 NOT NULL


Step 2 :import the sql file from db folder of project to database with phpMyadmin.

Logic for pagination 

  • find the total number of records in table. suppose you have 20 records in table.
  • set the how many records want to display in page. suppose 5 records per page.
  • now calculate  total page for pagination : total_record/records_per_page. example : 20/5 . so total page will 4.
  • use $_GET[] method to collect page number.use isset() operator to verify page click.
  • now create html table and set limit in SQL query.see the code/comment for help.
  • use pagination hyperlink code .
  • enjoy the simple php-mysql pagination.
  • If you have any query please comment.
    <!DOCTYPE html>
    include_once './Dbi.php';
            <meta charset="UTF-8">
            <title>Pagination Demo by learn.iotwebplanet.com </title>
            // Create object ob DB class for db connectivity
             $db=new Dbi();
             mysqli_set_charset($db_con, 'utf8');
             //set data to display per page
             //checking paging by GET
             if (isset($_GET["page"])) 
                 $page  = $_GET["page"];   //get value of page
             else { 
                      $page=1;    //if first page then set 1
     //pagination trick or logic              
    $start_from = ($page-1) * $num_rec_per_page; 
    //create query to check total record 
    $psql = 'SELECT * FROM exclusive_news'; 
    //run query and save result 
    //count total number of record in result
    $total_records = mysqli_num_rows($rs_result);  
    //set how many pagination will be visible totalrecord/record per page
    $total_pages = ceil($total_records / $num_rec_per_page); 
    //code for display pagination 
            echo "<a href='index.php?page=1'>".'|<'."</a> "; // Goto 1st page  
    for ($i=1; $i<=$total_pages; $i++) 
                echo "<a href='index.php?page=".$i."'>".$i."</a> "; 
    echo "<a href='index.php?page=$total_pages'>".'>|'."</a> "; // Goto last page
    //end of pagination code
            <table border="2" width="1" cellspacing="1" cellpadding="1" style="width: 500px;">
    //this query for loding data in table based on pagination
               $sql = "SELECT  * FROM exclusive_news LIMIT $start_from, $num_rec_per_page"; 
                $table_data=  mysqli_query($db_con,$sql);
               // $sl=$start_from;
                while ($row = mysqli_fetch_array($table_data)) {
                        <td><?php  echo $row['id']; ?></td>
                       <td><?php  echo $row['date']; ?></td>
                         <td><?php  echo $row['title']; ?></td>
    //code for display pagination 
            echo "<a href='index.php?page=1'>".'|<'."</a> "; // Goto 1st page  
    for ($i=1; $i<=$total_pages; $i++) 
                echo "<a href='index.php?page=".$i."'>".$i."</a> "; 
    echo "<a href='index.php?page=$total_pages'>".'>|'."</a> "; // Goto last page
    //end of pagination code

    Step 3 :Copy project folder to your server www/htdocs folder.
    Step 4 : open url http://localhost/PhpPagination/index.php
    Step 5 : Yeah!

How to call webservice from kotlin in android

Step 1:

  1. create new application on Android Studio 3.0
  2. Create GUI  textView, button and progress bar
  3. Add some external library
  4. create kotlin data class
  5. define your webservice url in MainActivity.kt
  6. add internet uses permission your mainfest.xml file


package com.iotwebplanet.learn.myapplication1

import android.os.Bundle
import android.support.design.widget.FloatingActionButton
import android.support.design.widget.Snackbar
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.Toolbar
import android.util.Base64
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.Button
import android.widget.TextView
import java.net.HttpURLConnection
import java.net.URL
import android.os.StrictMode
import android.widget.ProgressBar
import com.beust.klaxon.*
import com.github.salomonbrys.kotson.fromJson
import com.google.gson.Gson

class MainActivity : AppCompatActivity() {

    public fun parse(name: String):Unit {
        val cls = Parser::class.java
        cls.getResourceAsStream(name)?.let { inputStream ->
            return (Parser().parse(inputStream) as Unit?)!!
    override fun onCreate(savedInstanceState: Bundle?) {

        //Thread policy for internet access
        val SDK_INT = android.os.Build.VERSION.SDK_INT
        if (SDK_INT > 8) {
            val policy = StrictMode.ThreadPolicy.Builder()

        val toolbar = findViewById(R.id.toolbar) as Toolbar

//kotlin typecasting of view

        val button=findViewById(R.id.button) as Button
        var tv=findViewById(R.id.mytext) as TextView
        val progress_bar=findViewById(R.id.progressBar) as ProgressBar

//on button click event

            progress_bar.visibility = View.VISIBLE

// Kotlin + HttpURLConnection

            val connection = URL("example.com").openConnection() as HttpURLConnection
            val text = connection.inputStream.use { it.reader().use { reader -> reader.readText() } }

//suppose sample data from http get calls

            println(text)   //sample data output
            //setting text in txt view for demo.

//optional block
//response to pojo work . following statement may be different in your case plz do as per your data
            val gson = Gson()        //gson object
            val list1 = gson.fromJson<List<Product>>(text) //convert/store response data to kotlin data class Product

println("Saved Data "+list1.size)   //how much data saved
            val s=list1.size;   //size of collection
//printing saved data in pojo class
            for (i in 0..s-1) {
                //printing list from loop
                println("Products Data : $list1.get(i).sku $list1.get(i).position $list1.get(i).category_id")

// end of optional block

            progress_bar.visibility = View.GONE;  //hide progress bar

        })   //end of button event


    //optional code not related to tutorial
    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        // Inflate the menu; this adds items to the action bar if it is present.
        menuInflater.inflate(R.menu.menu_main, menu)
        return true

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        val id = item.itemId

        if (id == R.id.action_settings) {
            return true

        return super.onOptionsItemSelected(item)


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
<uses-permission android:name="android.permission.INTERNET"></uses-permission>

    <application android:allowBackup="true" android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true" android:theme="@style/AppTheme">
        <activity android:name=".MainActivity" android:label="@string/app_name"
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />


Product.kt .  data class(optional)

package com.iotwebplanet.learn.myapplication1
 * Created by developer on 10/06/17.
data class Product(val sku: String, val position: Int, var category_id: Int)

Important to add in Build.Gradle(Module:app)

compile 'com.beust:klaxon:0.30'
compile 'com.github.salomonbrys.kotson:kotson:2.5.0'


How to Transfer Data between Android Activity using Kotlin

Here is i am describing how to transfer variable or value from one  Activity to Another Activity in Android Application.I am using Android Studio 3.0.I hope my code will help you. You can use this code in button event or any other place where it is required.

Set Data in Activity

val myvalue="I am from Main Activity"
val myActivity = Intent(applicationContext, Another_activity::class.java) //name of activity to launch 
  myActivity.putExtra("value",myvalue) // value will save of your myvalue variable used for another activity
   startActivity(myActivity) //to launch another activity

Get Data from Activity

//create this code in Another_activity.kt
//getting/fetch Intent value
     val data = intent.extras.getString("value")
    //Now its your choice whatever you want to do with data.

feel free to comment and feedback

Good bye findViewById() use Kotlin Android Extensions

Hi Developers as we always juggle with findViewById() in Android activity class . Some time we forget to typecast xml into Kotlin or java . So good news for Kotlin android developer.

Kotlin Android Extensions

Kotlin Android Extensions are another Kotlin plugin that is included in the regular one, and that will allow to recover/find views from Fragments, Activities,   and Views in an effective  way.

see how easy it is ….

Step 1.

Add Kotlin Android Extensions in Gradle (Project)

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions' //this the main plugin

And that’s all you need to sync your gradle. You’re now ready to start working with it.


Recovering/find views in an Activity or Fragment

import your xml layout like below code in activity . here my activity xml file name is activity_main it would be different in your case. so add it in your activitity.

import kotlinx.android.synthetic.main.activity_main.*

The code that the plugin generates is able to store a view cache, so if you ask the view again, this won’t require another findViewById.

Simple Call Xml View by name .


Here textView is my text view component inside xml.