Author: Awais Farooq

  • RESTful APIs

    Yii provides the following useful features for implementing RESTful APIs − To show RESTful APIs in action, we need data. Preparing the DB Step 1 − Create a new database. Database can be prepared in the following two ways. Step 2 − Configure the database connection in the config/db.php file. The following configuration is for the system used currently.…

  • Theming

    Theming helps you replace a set of views with another one without the need of modifying original view files. You should set the theme property of the view application component to use theming. You should also define the following properties − For example, if you call $this->render(‘create’) in UserController, the @app/views/user/create.php view file will be rendered. Nevertheless, if you enable theming…

  • Database Migration

    During the developing of a database-driven application, the database structure evolves with the source code. Yii provides the database migration feature that allows you to keep track of database changes. Yii provides the following migration command line tools − Creating a Migration Let us create a new database migration. Step 1 − Inside the project root of the…

  • Active Record

    Active Record provides an object-oriented API for accessing data. An Active Record class is associated with a database table. Yii provides the Active Record support for the following relational databases − Additionally, the Active Record class supports the following NoSQL databases − After declaring an Active Record class(MyUser model in our case) for a separate database table, you…

  • Query Builder

    Query builder allows you to create SQL queries in a programmatic way. Query builder helps you write more readable SQL-related code. To use query builder, you should follow these steps − To build an yii\db\Query object, you should call different query builder functions to define different parts of an SQL query. Step 1 − To show a typical…

  • Data Access Objects

    To execute an SQL query, you should follow these steps − Step 1 − Create a function called actionTestDb in the SiteController. The above example shows various ways of fetching data from a DB. Step 2 − Go to the address http://localhost:8080/index.php?r=site/test-db, you will see the following output. Create an SQL Command To create an SQL command with parameters, you should…

  • Database Access

    Yii DAO (Database Access Object) provides an API for accessing databases. It also serves as the foundation for other database access methods: active record and query builder. Yii DAO supports the following databases − Creating a Database Connection Step 1 − To create a database connection, you need to create an instance of the yii\db\Connection class.…

  • Dependency Injection

    A DI(dependency injection) container is an object that knows how to instantiate and configure objects. Yii provides the DI container via the yii\di\Container class. It supports the following kinds of DI − The DI container supports constructor injection with the help of type hints − Property and setter injections are supported through configurations − In case…

  • Configurations

    Configurations are used to create new objects or initializing the existing ones. Configurations usually include a class name and a list of initial values. They may also include a list of event handlers and behaviors. The following is an example of the database configuration − The Yii::createObject() method takes a configuration array and creates an object based…

  • Creating a Behavior

    Assume we want to create a behavior that will uppercase the “name” property of the component the behavior is attached to. Step 1 − Inside the components folder, create a file called UppercaseBehavior.php with the following code. In the above code we create the UppercaseBehavior, which uppercase the name property when the “beforeValidate” event is triggered. Step 2 − To…