yii2

[Yii2] Cache Speed Comparison

[Yii2] Cache Speed Comparison

Most website developers tend to ignore caching without realizing that it can harm their applications. A one-second delay in access time can mean you're losing customers or users to competitors. Yii2 supports caching in a variety of methods. Supports fragment caching, data caching, page caching, and dynamic content. Cache storage components can be exchanged without having to change the code that uses the cache. In this article, we will make a comparison of cache speeds in several methods. Without using cacheFileCacheDbCacheRedis Steps to make a cache speed comparison on yii2 Set up a table by using MySQL CREATE TABLE 'test_cache' ( 'id' INTEGER(11) NOT NULL AUTO_INCREMENT, 'data' VARCHAR(20) COLLATE utf8mb4_general_ci DEFAULT NULL, PRIMARY KEY USING BTREE ('id') ) ENGINE=InnoDB AUTO_INCREMENT=1 ROW_FORMAT=DYNAMIC CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_general_ci'; Create a simple table with id and data columns. Then create a record for the table. In this trial, we added a data column with random characters with 20 characters. The number of lines we create is 10,000 lines. Create a model of the table, for example with the name "Testcache".Create a Controller to display a response, for example under the name "TestcacheController" "TestcacheController" without cache public function actionIndex() { $data=TestCache::find()->all(); Yii::$app->response->format = yiiwebResponse::FORMAT_JSON; return $data; } "TestcacheController" with cache public function actionIndex(){ $data =...

Real-Time Application Dashboard with Yii2, Node.js, MySQL and Chart.js

Real-Time Application Dashboard with Yii2, Node.js, MySQL and Chart.js

In this article we will create a dashboard that can display graphics from data in real-time. The data we use as simulations is the population of a city. If you update your resident data, add or delete it, the graphic will change in real-time without the need to refresh from the page. Before we begin, we need to prepare: Webserver is installed, see How to Configure Virtual Host XAMPP in Windows 10.Yii2 is installed, see How to Install Yii2 Advanced via Composer.Node.js is already installed, see Create real-time applications with Nodes.js.Chart.js. Steps to Create a Real Time Application Dashboard with Yii2, Node.js, MySQL and Chart.js A. Create a table with MySQL Create tables with the name "city", and columns with the names "id" and "population". Or it's easier to run the following sql scripts. CREATE TABLE 'city' ( 'id' CHAR(16) NOT NULL PRIMARY KEY, 'population' INT(11) NOT NULL DEFAULT '0' ) ENGINE=InnoDB DEFAULT CHARSET=utf8; B. Create a Node server.js Set up a Node server.js as in Creating real-time applications with Nodes.js.In the "server.js" file, what we need to create is an "API" that will update the data on the graph we create with the Chart.js after the data is successfully stored by Yii.When the user first opens the dashboard page, we check whether the initial data for the dasboard graph is available, if not Yii will send the initial data. app.post('/status', (req, res) => { res.send(isInitData); }); app.post('/initData', (req, res) => { if(!isInitData){ myData...

[Yii2] Create a RESTful API

[Yii2] Create a RESTful API

An API is software that integrates the applications we create with other applications. The purpose of the creation is to share data between applications that have been integrated. The RESTful API/REST API is the application of the API(Application Programming Interface). REST(Representational State Transfer)is an architecture of communication methods that uses the HTTP protocol for data exchange where this method is often applied in application development. With the aim to make the system have good performance, fast and easy to develop (scale) especially in the exchange and communication of data. Building a REST API in Yii is actually quite easy. You can take advantage of the existing MVC framework, but you create different access points that you want to access by different types of services (not website visitors). Here, we use an example to illustrate how you can build a set of RESTful APIs with little coding effort. The Yii we will use is the yii2 advanced template. Assume you want to expose user data via RESTful APIs. The user table in this example you created when you first installed Yii. For how to install Yii you can see on How to Install Yii2 Advanced via Composer. Steps to Create a RESTful API on Yii2 Advanced A. Create endpoint APIs In the Yii2 Advanced template, we have a backend and frontend directory. To separate the API features, we'll create a new directory named "API" as the endpoint. Copy the backend directory and rename it to "fire". Edit...

[Yii2] How to create and use Functions on Yii2

[Yii2] How to create and use Functions on Yii2

A function is an organized and reusable block of code that is used to perform one related action or achieve a specific result. In creating applications using Yii2, often we need a function to simplify and speed up the creation of the application we are creating. Well... Now how to make the function we create we can access in view or controller. In this article, we will learn how to create and use functions on Yii2 basic and advanced templates. The steps of creating and using functions on Yii2 are basic and advanced templates. Create a folder with the name "components". For Yii2 basic create the folder in root and on Yii2 advanced create that folder in the "common" folder. Create a"MyFunction.php"file in the components folder, and write the function as follows: namespace commoncomponents; //Yii2 Advanced template namespace appcomponents; //Yii2 basic template use yiibaseComponent; class MyFunction extends Component { public function hello() { ...... return "Hello, World!"; } } } In Yii2 Advanced, edit the file"common/config/main-local.php"and add the following code: return [ 'components' => [ ............... 'MyFunction'=>[ 'class' =>'commoncomponentsMyFunction' ], ........... ], ]; While on Yii2 Basic, edit the file"config/web.php" and copy the following code ........... $config = [ ............ 'components' => [ ............ 'MyFunction'=>[ 'class' =>'appcomponentsMyFunction', ], ], .............. ]; ........... To use the function that you have created is enough to call...

[yii2] How to create new extensions without version or Git controls

[yii2] How to create new extensions without version or Git controls

Using Git in building extensions is great. When using Git, you must load the extension through Composer. Likewise when updating or correcting errors that occur. Of course, this will be troublesome, when the extension made is still in the development stage. To avoid this, we can use composers locally without version or Git controls. So that every code change made can immediately function without the need to update and commit changes through composer or git. Here are the steps to do so: 1. Generate Extension via Gii The first thing you should and do is create an extension template from gii, you can open"http://hostname/index.php?r=gii". Then select"Extension Generator". After that complete the form filling in accordance with the extension you created. The most important thing in the filling is the "output path", you can fill it with "@vendorbardimin" if you create an extension in the vendor directorybardimin. After that select "Generate" to create the extension files. Next, go to the directory "vendorbardiminyii2-myextension" if you fill out the form above like an example. Formed 3 files under the name AutoloadExample.php, composer.json, and README.md. 2. Edit Composer.json Then open the composer.json file from the extension that has been created earlier and add "minimum-stability": "dev", 3. Edit Yii2 Composer.json After that, go back to the yii2 installation directory root and open the composer.json file. Then add "bardimin/yii2-myextension": "@dev" to"require-dev"and...