How to create custom component in Yii Framework 2.0

Yii2.0 framework having default application components and it is giving different type services. For example 'urlManager' component, 'db' component etc. Every application component has an uniqueID and will call through expression format. We can create Application components like global or local variables.

Syntax And Core Components

Application compoent syntax

Yii::$app->componentID
sample Core Application Components

Yii::$app->db
Yii::$app->cache
Yii::$app->request
Yii::$app->session
Yii::$app->mailer
etc

Create Your Own Component In Yii2.0

Create a folder named "components" in the project root directory. Now create one class 'MessageComponent' with extends class 'Component' inside the components folder. Using this component, we will display message. Please see the below code to create a custom component class.

<?php
namespace app\components;

use yii\base\Component;

class MessageComponent extends Component{
	public $content;
	
	public function init(){
		parent::init();
		$this->content= 'Hello Yii 2.0';
	}
	
	public function display($content=null){
		if($content!=null){
			$this->content= $content;
		}
		echo Html::encode($this->content);
	}
	
}
?>

Config Component In Yii2.0

We have to register 'MessageComponent' by configuring the yii\base\Application::$components property inside the config/web.php file (application configurations).

    'components' => [
		'message' => [
            'class' => 'app\components\MessageComponent',
		],
	],
Now we can access this component using 'Yii::$app()->message' expression.

Call Yii Custom Component Function

Using configured 'message' component, we can call the method inside the 'MessageComponent'.

<?php 
	Yii::$app->message->display('I am Yii2.0 Programmer');
?>
  • Rahul Fbdev

    how make components in side models folder and how to use

    • m bala

      Add this code in “models/MessageComponent.php”
      <?php
      namespace appmodels;
      use yiibaseComponent;
      use yiihelpersHtml;

      class MessageComponent extends Component{
      public $content;

      public function init(){
      parent::init();
      $this->content= ‘Hello Yii 2.0′;
      }

      public function display($content=null){
      if($content!=null){
      $this->content= $content;
      }
      echo Html::encode($this->content).’Here’;
      }

      }
      ?>
      In “config/web.php”
      <?php
      ……..
      ‘components’ => [
      ‘message_models’ => [
      ‘class’ => ‘appmodelsMessageComponent’,
      ],
      ]
      ……
      ?>

      In view.php
      Yii::$app->message_models->display(‘I am Yii2.0 Programmer’);

      • Rahul Fbdev

        i want load Component with help of modules.
        i don’t want write any code in web.php for Component

  • Arjen Meijer

    How do I make the component global? That is, the content of the fields can be accessed in all controllers after they are set in one controller?

    • m bala

      To access the component in global, Just we have to configure it in config/web.php file. Follow step 3 and 4

      • Arjen Meijer

        I have in web.php
        ‘components’ => [
        ‘spel’ => [
        ‘class’ => ‘appcomponentsSpelComponent’,
        ],

        in components in Spelcomponent.php:

        spelvorm_id = 0;
        $this->spelronde_id = 0;
        $this->persona_id = 0;
        }

        public function setSpelrondeid($id) {
        $this->spelronde_id = $id;
        $actief = Spelrondeactievespelers::findOne([‘spelronde_id’=>$this->spelronde_id, ‘user_id’=>Yii::$app->user->id]);
        $this->spelvorm_id = $actief->spelronde->spelvorm_id;
        $this->persona_id = $actief->persona_id;
        }

        /* Getter for spelnummer */
        public function getSpelrondeid() {
        return $this->spelronde_id;
        }

        More getters.

        However, if call the function getSpelrondeid in a view that starts from a different class that first class that called the component, spelronde_id is alway 0.

        So I am not thinking correctly. What do I have improve?

        • m bala

          It is working for me. I checked with my code ‘MessageComponent’ class

          //first time assign and display
          Yii::$app->message->display(‘I am Yii2.0 Programmer’);
          //reassign
          Yii::$app->message->content=”hai”;
          //display only
          Yii::$app->message_models->display();