0

Yii2-Google URL Shortener

Yii2 Google URL Shortener at goo.gl is used by Google products to create short URLs that can be easily shared, tweeted, or emailed to friends using widget in Yii2. Widgets are reusable blocks and they are used in your views. The main advantage of Widget is you can write once and reuse it wherever you need it in your view.

 

To create a widget we must extend the base widget yii\base\Widget and we have to override the init() and/or run() methods of base widget.

Here is the simple steps for creating widget for Google URL Shortener.

Step1:
Make a folder named “components” in your project root folder.

 

Step2:
Crate a file with desired widget name inside components folder and copy past the below code and change the class name in the code. Example – GoogleURLShortner.php

In this class ‘GoogleURLShortner’ is our custom widget. ‘app\components’ is the namespace of this class and ‘GoogleURLShortner’ is a class name. Using both namespace and class name, we can access this widget like ‘app\components\GoogleURLShortner’.

Create a views folder inside components folder. Create a view file named GoogleURLShortner.php in components/views folder and put your content in it.

 

Step3:
You are done!. Now you can use your widget inside any of your app views.

Usage example

 

 

Prakash S

Prakash S

I would like to introduce myself as a Software professional opting for the career in software industry. I'm Prakash S, a MCA graduate and trained as industry level practice for Software technology. Basically I am a PHP Developer but now days exploring more in HTML5, CSS, AngularJS and jQuery libraries.