search
HomePHP FrameworkYIICreate a rental website using Yii framework

As one of the most popular PHP frameworks at the moment, the Yii framework has the advantages of high performance, high scalability, and high security. More and more developers choose to use the Yii framework to develop various types of applications. This article introduces how to use the Yii framework to create a rental website.

1. Environment configuration

To use the Yii framework to create a rental website, you first need to install the necessary environment and tools:

  1. PHP environment: PHP5.4 or above is required ;
  2. Database: This example uses the MySQL database;
  3. Server: This example uses the Apache server;
  4. Yii framework: Download and unzip the Yii framework to the web server directory.

2. Create a database

Create a database named "house_rental" in MySQL, which contains the following tables:

  1. House information table ( house_info): stores house information, including house ID, address, size, price and other information;
  2. Tenant information table (tenant_info): stores tenant information, including tenant ID, name, contact information and other information;
  3. Order information table (order_info): stores order information, including order ID, house ID, tenant ID, rental time, order status and other information.

3. Create Yii application

In the web server directory, use the command line tool provided by the Yii framework to create a Yii application:

  1. Open the command line Tool, enter the web server directory and execute the following command:
php yii/framework/yii webapp house_rental

Among them, "house_rental" is the name of the Yii application.

  1. After creation, you can see the newly created Yii application folder in the web server directory.

4. Configure the database

In the Yii application folder, open the protected/config/main.php file. In this file, replace the following code segment with your own database configuration information:

'db'=>array(
     'connectionString' => 'mysql:host=localhost;dbname=house_rental',
     'emulatePrepare' => true,
     'username' => 'username',
     'password' => 'password',
     'charset' => 'utf8',
),

Where "localhost" is the database host address, "house_rental" is the database name created in the previous step, "username" and "password" are the database login account and password respectively.

5. Create data model

In the models folder of the Yii application folder, create three data model files HouseInfo.php, TenantInfo.php and OrderInfo.php, corresponding to the above three files respectively. A table.

  1. HouseInfo.php file:
<?php
 
class HouseInfo extends CActiveRecord
{
     //指定数据库表名
     public function tableName()
     {
          return 'house_info';
     }
 
     //定义验证规则
     public function rules()
     {
          return array(
               array('address, size, price', 'required'),
               array('size', 'numerical', 'integerOnly'=>true),
               array('address', 'length', 'max'=>200),
               array('price', 'length', 'max'=>50),
          );
     }
 
     //定义关联关系,HouseInfo和OrderInfo是一对多的关系
     public function relations()
     {
          return array(
               'order_info'=>array(self::HAS_MANY, 'OrderInfo', 'house_id'),
          );
     }
}
  1. TenantInfo.php file:
<?php
 
class TenantInfo extends CActiveRecord
{
     //指定数据库表名
     public function tableName()
     {
          return 'tenant_info';
     }
 
     //定义验证规则
     public function rules()
     {
          return array(
               array('name, phone', 'required'),
               array('name', 'length', 'max'=>50),
               array('phone', 'length', 'max'=>20),
          );
     }
 
     //定义关联关系,TenantInfo和OrderInfo是一对多的关系
     public function relations()
     {
          return array(
               'order_info'=>array(self::HAS_MANY, 'OrderInfo', 'tenant_id'),
          );
     }
}
  1. OrderInfo.php file:
<?php
 
class OrderInfo extends CActiveRecord
{
     //指定数据库表名
     public function tableName()
     {
          return 'order_info';
     }
 
     //定义验证规则
     public function rules()
     {
          return array(
               array('house_id, tenant_id, order_date, status', 'required'),
               array('status', 'in', 'range'=>array('pending', 'reserved', 'paid', 'cancelled')),
               array('house_id, tenant_id', 'length', 'max'=>11),
          );
     }
 
     //定义关联关系,OrderInfo和HouseInfo是多对一的关系
     public function relations()
     {
          return array(
               'house_info'=>array(self::BELONGS_TO, 'HouseInfo', 'house_id'),
          );
     }
}

6. Create controllers and views

In the Yii application folder, create a controller file HouseController.php and a view file house.php.

  1. HouseController.php file:
<?php
 
class HouseController extends Controller
{
     public function actionIndex()
     {
          //查询所有房屋信息
          $houses = HouseInfo::model()->findAll();
          $this->render('house', array('houses' => $houses));
     }
}
  1. house.php file:
<?php
$this->pageTitle=Yii::app()->name.' - 房屋列表';
$this->breadcrumbs=array(
     '房屋列表',
);
?>
 
<h1 id="房屋列表">房屋列表</h1>
 
<?php foreach($houses as $house): ?>
 
<div class="house">
     <h2><?php echo $house->address; ?></h2>
     <div class="info">
          <p><strong>面积:</strong><?php echo $house->size; ?></p>
          <p><strong>价格:</strong><?php echo $house->price; ?></p>
     </div>
     <p><a href="#">查看更多</a></p>
</div>
 
<?php endforeach; ?>

7. Start the application

Enter http://localhost/house_rental/index.php in the browser, and you can see the house list on the web page.

At this point, a simple rental website has been created. You can extend and beautify the functions according to your needs. Using the Yii framework to develop applications can improve development efficiency and code quality, and is easy to maintain and update. Hope this article can be helpful to you.

The above is the detailed content of Create a rental website using Yii framework. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Essential Soft Skills for Yii Developers: Communication and CollaborationEssential Soft Skills for Yii Developers: Communication and CollaborationMay 08, 2025 am 12:11 AM

Soft skills are crucial to Yii developers because they facilitate team communication and collaboration. 1) Effective communication ensures that the project is progressing smoothly, such as through clear API documentation and regular meetings. 2) Collaborate to enhance team interaction through Yii's tools such as Gii to improve development efficiency.

Laravel MVC : What are the best benefits?Laravel MVC : What are the best benefits?May 07, 2025 pm 03:53 PM

Laravel'sMVCarchitectureoffersenhancedcodeorganization,improvedmaintainability,andarobustseparationofconcerns.1)Itkeepscodeorganized,makingnavigationandteamworkeasier.2)Itcompartmentalizestheapplication,simplifyingtroubleshootingandmaintenance.3)Itse

Yii: Is It Still Relevant in Modern Web Development?Yii: Is It Still Relevant in Modern Web Development?May 01, 2025 am 12:27 AM

Yiiremainsrelevantinmodernwebdevelopmentforprojectsneedingspeedandflexibility.1)Itoffershighperformance,idealforapplicationswherespeediscritical.2)Itsflexibilityallowsfortailoredapplicationstructures.However,ithasasmallercommunityandsteeperlearningcu

The Longevity of Yii: Reasons for Its EnduranceThe Longevity of Yii: Reasons for Its EnduranceApr 30, 2025 am 12:22 AM

Yii frameworks remain strong in many PHP frameworks because of their efficient, simplicity and scalable design concepts. 1) Yii improves development efficiency through "conventional optimization over configuration"; 2) Component-based architecture and powerful ORM system Gii enhances flexibility and development speed; 3) Performance optimization and continuous updates and iterations ensure its sustained competitiveness.

Yii: Exploring Its Current UsageYii: Exploring Its Current UsageApr 29, 2025 am 12:52 AM

Yii is still suitable for projects that require high performance and flexibility in modern web development. 1) Yii is a high-performance framework based on PHP, following the MVC architecture. 2) Its advantages lie in its efficient, simplified and component-based design. 3) Performance optimization is mainly achieved through cache and ORM. 4) With the emergence of the new framework, the use of Yii has changed.

Yii and PHP: Developing Dynamic WebsitesYii and PHP: Developing Dynamic WebsitesApr 28, 2025 am 12:09 AM

Yii and PHP can create dynamic websites. 1) Yii is a high-performance PHP framework that simplifies web application development. 2) Yii provides MVC architecture, ORM, cache and other functions, which are suitable for large-scale application development. 3) Use Yii's basic and advanced features to quickly build a website. 4) Pay attention to configuration, namespace and database connection issues, and use logs and debugging tools for debugging. 5) Improve performance through caching and optimization queries, and follow best practices to improve code quality.

Yii's Features: Examining Its AdvantagesYii's Features: Examining Its AdvantagesApr 27, 2025 am 12:03 AM

The Yii framework stands out in the PHP framework, and its advantages include: 1. MVC architecture and component design to improve code organization and reusability; 2. Gii code generator and ActiveRecord to improve development efficiency; 3. Multiple caching mechanisms to optimize performance; 4. Flexible RBAC system to simplify permission management.

Beyond the Hype: Assessing Yii's Role TodayBeyond the Hype: Assessing Yii's Role TodayApr 25, 2025 am 12:27 AM

Yii remains a powerful choice for developers. 1) Yii is a high-performance PHP framework based on the MVC architecture and provides tools such as ActiveRecord, Gii and cache systems. 2) Its advantages include efficiency and flexibility, but the learning curve is steep and community support is relatively limited. 3) Suitable for projects that require high performance and flexibility, but consider the team technology stack and learning costs.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Atom editor mac version download

Atom editor mac version download

The most popular open source editor