Quickstarters

How to Build a Backend for Yii 2?

40min

Introduction

In this tutorial, you will learn how to build a backend for Yii 2 using Back4app.

Yii 2 is a popular open source PHP framework that helps you create secure and efficient web applications.

By integrating the Yii framework with Back4app, you can take advantage of powerful features such as database management, cloud functions, REST and GraphQL APIs, user authentication, and real-time queries – all while speeding up your backend development process.

You will see how to leverage Back4app’s environment to cut down on manual server configuration, letting you focus on writing your Yii 2 code.

By following these steps, you will gain hands-on experience with essential functionalities, including robust security controls (ACLs, CLPs), scheduling recurring tasks, and setting up external integrations via webhooks.

By the end of this tutorial, you will have a solid backend structure in place for your Yii 2 project, ready to scale into production or enhance with custom business logic.

You will also be well-prepared to integrate third-party APIs or build new features into your web applications.

Prerequisites

To complete this tutorial, you will need:

  • A Back4app account and a new Back4app project Getting Started with Back4app. If you do not have an account, sign up for free and follow the linked guide to get your project ready.
  • A local Yii 2 development environment You can download Yii 2 using Composer and follow the Yii 2 Official Guide for setup.
  • PHP (version 7.4 or above) installed You’ll need a compatible PHP environment to run Yii 2 and manage Composer packages.
  • Familiarity with PHP and basic Yii 2 concepts If you are new to the Yii framework, check out the Official Yii 2 Documentation.

Make sure you have all of these prerequisites in place before you begin. Having your Back4app project set up and your local Yii 2 environment ready will allow you to follow along more easily.

Step 1 – Creating a New Project on Back4App and Connecting

Create a New Project

The first step to build a backend for Yii 2 is to create a new Back4app project. If you haven’t already done so, follow these steps:

  1. Log in to your Back4app account.
  2. Click the “New App” button in your Back4app dashboard.
  3. Name your app (e.g., “Yii2-Backend-Tutorial”).
Document image


Once the project is created, you will see it on your Back4app dashboard. This serves as the foundation for all backend configurations we will explore in this tutorial.

Connecting via Parse APIs

Back4app uses the Parse Platform under the hood. While the Parse PHP SDK exists, you can also integrate your Yii 2 application with Back4app using Parse’s REST or GraphQL APIs.

This approach is flexible, letting you write code that sends HTTP or GraphQL requests to Back4app from your Yii controllers or models.

Retrieve Your Parse Keys

  1. In your Back4app dashboard, go to your app’s “App Settings” or “Security & Keys”.
  2. Locate your Application ID, REST API Key, and the Parse Server URL (usually https://parseapi.back4app.com).
Document image


Keep these credentials close at hand. You will need them when making requests from your Yii 2 application to Back4app.

Step 2 – Setting Up the Database

Your Back4app project comes with a cloud-based database that is automatically managed by the Parse Server. You can create a data model and store objects in it using REST, GraphQL, or the AI agent in Back4app.

Creating a Data Model

  1. Open the “Database” section in your Back4app dashboard.
  2. Create a new class (e.g., “Todo”) and add columns such as title (String) and isCompleted (Boolean).
Create New Class
Create New Class


You can also let Parse create these columns automatically the first time your Yii code sends data. Furthermore, Back4app’s AI Agent can help you set up the schema:

  1. Open the AI Agent from your App Dashboard or the menu.
  2. Describe your data model (e.g., “Create a Todo App schema with a Todo class”).
  3. Allow the AI Agent to generate the schema.
Document image


Reading and Writing Data Using REST API

Below is an example of saving a Todo object using the REST API. You might implement this in a Yii 2 controller action using cURL or PHP’s file_get_contents() to post JSON data.

Bash


Example Yii 2 PHP code snippet (using cURL) in a controller:

PHP


Querying the same data with REST might look like:

Bash


Reading and Writing Data Using GraphQL API

You can also create and read data using Parse’s GraphQL interface. For instance, to create a Todo:

GraphQL


In Yii 2, you can use GraphQL libraries or simple HTTP requests to interact with the Back4app GraphQL endpoint: https://parseapi.back4app.com/graphql

Working with Live Queries (Optional)

For real-time updates, Back4app supports Live Queries, which let you subscribe to changes on a class. Although typical use in Yii 2 might be less common, you can still enable Live Queries in your Back4app settings and handle WebSocket connections in PHP. This is more advanced, so refer to the Parse Live Queries documentation for details on implementing real-time features in your PHP-based web applications.

Step 3 – Applying Security with ACLs and CLPs

Brief Overview

Back4app offers Access Control Lists (ACLs) and Class-Level Permissions (CLPs) to protect your data. ACLs apply to specific objects, while CLPs define global permissions for each class.

Document image


Setting up Class-Level Permissions

  1. Go to Database in your Back4app dashboard and select the class (e.g., “Todo”).
  2. Open the CLPs tab, and configure read/write permissions (e.g., “Requires Authentication” or “No Access”).

Configuring ACLs

You can set ACLs for individual objects by including an _ACL field when creating or updating data via REST or GraphQL. For example, using REST:

Bash


This object can now only be accessed by the user with the specified objectId.

Step 4 – Writing Cloud Code Functions

Why Cloud Code

Using Cloud Code on Back4app allows you to run custom server-side logic without managing your own servers. You can create business logic, data validations, or triggers for object creation, updates, and deletions.

Example Cloud Code Function

Below is a simple Cloud Code function (written in JavaScript) that calculates text length:

main.js


Deployment

You can deploy your Cloud Code via the Back4app CLI or directly in the Cloud Code > Functions section of your Back4app dashboard:

  • Back4app CLI:
  • Dashboard: Copy/paste your code into main.js and click Deploy.
Document image


Calling Your Cloud Function

You can call your function from Yii 2 using a simple REST POST request:

Bash


Or via GraphQL:

GraphQL


Step 5 – Configuring Authentication

User Authentication in Back4app

Back4app uses the Parse User class as a foundation for secure authentication. You can create and verify users through REST or GraphQL calls.

Creating a User with REST

Bash


Logging In a User

Bash


Social Login

For social logins (Google, Apple, Facebook, etc.), configure your OAuth settings in Back4app and your Yii 2 application to handle the necessary tokens. Refer to Back4app’s Social Login Docs for provider-specific guidelines.

Email Verification & Password Reset

Enable email verification and password reset in the Email Settings of your Back4app dashboard to improve user security:

  1. Go to Email Settings in your Back4app dashboard.
  2. Enable email verification and customize your email templates.
  3. Test that your emails are sent and received properly.

Step 6 – Handling File Storage

Back4app provides secure file storage via the Parse File system. While the Parse PHP SDK is an option, we’ll illustrate REST again for consistency.

Uploading Files via REST

Bash


This returns a url for the saved file. You can then store that URL in a class (like Photo) for later retrieval.

File Security Considerations

You can limit file uploads to only authenticated users or to specific roles by configuring Parse Server settings in your app. For more granular control, combine these settings with your ACL and CLP rules.

Step 7 – Email Verification and Password Reset

Overview

Email verification ensures users own the email addresses they register with, and password reset allows them to recover accounts.

Back4App Dashboard Configuration

  1. Enable Email Verification under App Settings > Email Settings.
  2. Customize the “From” address and email templates.
  3. Test by creating a new user to confirm the verification email is sent.

Implementation

In your Yii 2 application, direct users to a route that triggers Parse’s password reset endpoint. The rest of the flow (like sending the actual email) is handled by the Back4app infrastructure.

Step 8 – Scheduling Tasks with Cloud Jobs

What Cloud Jobs Do

Cloud Jobs in Back4app let you schedule routine tasks, such as cleaning data or sending notifications. Define them in your Cloud Code (main.js) and schedule them in the dashboard.

Example: Cleanup Job

main.js


Step 9 – Integrating Webhooks

Definition

Webhooks allow your Back4app application to send automated HTTP requests to external services when events occur, like creating a new Todo.

Configuration

  1. In your Back4app dashboard, go to More > WebHooks.
  2. Add a Webhook endpoint (e.g., https://your-external-service.com/webhook).
  3. Configure triggers (e.g., “New record in Todo class”).
Adding a Webhook
Adding a Webhook


With webhooks, you can send notifications to other services (like Slack, Stripe, or your custom API) whenever a Todo object is created or updated.

Step 10 – Exploring the Back4App Admin Panel

Where to Find It

The Back4app Admin App is a point-and-click interface for managing your data without writing code. You can enable it by visiting App Dashboard > More > Admin App and then Enable Admin App.

Enable Admin App
Enable Admin App


Features

After creating an Admin User, you’ll have a dedicated subdomain for your Admin App. This gives authorized team members a clean interface for CRUD operations on your Back4app database, reviewing logs, or viewing analytics — no code needed.

Document image


Conclusion

In this tutorial, you discovered how to build a backend for Yii 2 using Back4app.

You configured a cloud-based database, set up robust security with ACLs and CLPs, scheduled background tasks with Cloud Jobs, and integrated external services through webhooks. You also saw how to manage user authentication and file storage securely.

Having combined an open source Yii framework for building web applications with Back4app’s scalable infrastructure, you now have a potent environment ready for development and growth.

Next Steps

  • Move toward production by expanding your data models, applying additional security settings, and optimizing performance.
  • Integrate external APIs (payment gateways, email providers) via Cloud Code or direct webhooks.
  • Explore official Back4app docs for deeper topics like advanced security, log analysis, and performance optimizations.
  • Learn more about building complex web applications with Yii 2 by using role-based access control, caching strategies, and advanced database relationships.

With this foundation, you can continue to enhance your Yii 2 project, focus on business-specific logic, and deliver robust, scalable functionality for your users. Enjoy coding and building modern web applications