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:
2. Create a database
Create a database named "house_rental" in MySQL, which contains the following tables:
3. Create Yii application
In the web server directory, use the command line tool provided by the Yii framework to create a Yii application:
php yii/framework/yii webapp house_rental
Among them, "house_rental" is the name of the Yii application.
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.
<?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'), ); } }
<?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'), ); } }
<?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.
<?php class HouseController extends Controller { public function actionIndex() { //查询所有房屋信息 $houses = HouseInfo::model()->findAll(); $this->render('house', array('houses' => $houses)); } }
<?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!