Category: Yii

  • Fragment Caching

    Fragment caching provides caching of a fragment of a web page. Step 1 − Add a new function called actionFragmentCaching() to the SiteController. In the above code, we created a new user and displayed a cachedview view file. Step 2 − Now, create a new file called cachedview.php in the views/site folder. We have enclosed a content generation logic in a pair of beginCache() and…

  • Caching

    Caching is an effective way to improve the performance of your application. Caching mechanisms store static data in cache and get it from cache when requested. On the server side, you may use cache to store basic data, such as a list of most recent news. You can also store page fragments or whole web…

  • Testing

    When we write a PHP class, we debug it step by step or use die or echo statements to verify how it works. If we develop a web application, we are entering test data in forms to ensure the page works as we expected. This test process can be automated. Automatic test approach makes sense…

  • Fields

    y overriding fields() and extraFields() methods, you can define what data can be put into a response. The difference between these two methods is that the former defines the default set of fields, which should be included in the response while the latter defines additional fields, which may be included in the response if an end user…

  • RESTful APIs in Action

    The controller class extends from the yii\rest\ActiveController class, which implements common RESTful actions. We specify the $modelClass property so that the controller knows which model to use for manipulating data. Step 1 − Create a file called UserController.php inside the controllers folder. Next we need to set up the urlManager component, so that the user data can be accessed and manipulated with…

  • 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…