Home  >  Article  >  PHP Framework  >  Create a rental website using Yii framework

Create a rental website using Yii framework

WBOY
WBOYOriginal
2023-06-21 15:06:13892browse

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>房屋列表</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