日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網(wǎng)為廣大站長提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請做好本站友鏈:【 網(wǎng)站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

隨著互聯(lián)網(wǎng)的普及以及人們對電影的熱愛,電影網(wǎng)站成為了一個受歡迎的網(wǎng)站類型。在創(chuàng)建一個電影網(wǎng)站時,一個好的框架是非常必要的。Yii框架是一個高性能的PHP框架,易于使用且具有出色的性能。在本文中,我們將探討如何使用Yii框架創(chuàng)建一個電影網(wǎng)站。

    安裝Yii框架

在使用Yii框架之前,需要先安裝框架。安裝Yii框架非常簡單,只需要在終端執(zhí)行以下命令:

composer create-project yiisoft/yii2-app-basic

登錄后復(fù)制

該命令將在當(dāng)前目錄中創(chuàng)建一個基本的Yii2應(yīng)用程序。現(xiàn)在你已經(jīng)準(zhǔn)備好開始創(chuàng)建你的電影網(wǎng)站了。

    創(chuàng)建數(shù)據(jù)庫和表格

Yii框架提供了ActiveRecord,這是一種使操作數(shù)據(jù)庫變得容易的方式。在本例中,我們將創(chuàng)建一個名為movies的數(shù)據(jù)表,該表包含電影ID、標(biāo)題、導(dǎo)演、演員、年份、類型和評分等信息。要創(chuàng)建表,請在終端中進(jìn)入應(yīng)用程序根目錄,然后運(yùn)行以下命令:

php yii migrate/create create_movies_table

登錄后復(fù)制

然后將生成的遷移文件編輯為以下內(nèi)容:

<?php

use yiidbMigration;

/**
 * Handles the creation of table `{{%movies}}`.
 */
class m210630_050401_create_movies_table extends Migration
{
    /**
     * {@inheritdoc}
     */
    public function safeUp()
    {
        $this->createTable('{{%movies}}', [
            'id' => $this->primaryKey(),
            'title' => $this->string()->notNull(),
            'director' => $this->string()->notNull(),
            'actors' => $this->text()->notNull(),
            'year' => $this->integer()->notNull(),
            'genre' => $this->string()->notNull(),
            'rating' => $this->decimal(3,1)->notNull(),
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public function safeDown()
    {
        $this->dropTable('{{%movies}}');
    }
}

登錄后復(fù)制

現(xiàn)在運(yùn)行遷移以創(chuàng)建movies數(shù)據(jù)表。

php yii migrate

登錄后復(fù)制

    創(chuàng)建電影模型

在Yii框架中,使用ActiveRecord非常容易定義數(shù)據(jù)表的模型。我們可以在models目錄下創(chuàng)建一個名為Movie的模型,并在模型定義中指定表格名和字段名。

<?php

namespace appmodels;

use yiidbActiveRecord;

class Movie extends ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%movies}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['title', 'director', 'actors', 'year', 'genre', 'rating'], 'required'],
            [['year'], 'integer'],
            [['rating'], 'number'],
            [['actors'], 'string'],
            [['title', 'director', 'genre'], 'string', 'max' => 255],
        ];
    }

    /**
    * {@inheritdoc}
    */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'title' => 'Title',
            'director' => 'Director',
            'actors' => 'Actors',
            'year' => 'Year',
            'genre' => 'Genre',
            'rating' => 'Rating'
        ];
    }
}

登錄后復(fù)制

    創(chuàng)建電影控制器

電影控制器將負(fù)責(zé)處理有關(guān)電影的所有請求,例如添加、編輯、刪除和顯示電影列表等請求。我們可以在controllers目錄下創(chuàng)建一個名為MovieController的控制器,并添加以下代碼:

<?php

namespace appcontrollers;

use Yii;
use yiiwebController;
use appmodelsMovie;

class MovieController extends Controller
{
    /**
     * Shows all movies.
     *
     * @return string
     */
    public function actionIndex()
    {
        $movies = Movie::find()->all();
        return $this->render('index', ['movies' => $movies]);
    }

    /**
     * Creates a new movie.
     *
     * @return string|yiiwebResponse
     */
    public function actionCreate()
    {
        $model = new Movie();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['index']);
        }

        return $this->render('create', [
            'model' => $model,
        ]);
    }

    /**
     * Updates an existing movie.
     *
     * @param integer $id
     * @return string|yiiwebResponse
     * @throws yiiwebNotFoundHttpException
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['index']);
        }

        return $this->render('update', [
            'model' => $model,
        ]);
    }

    /**
     * Deletes an existing movie.
     *
     * @param integer $id
     * @return yiiwebResponse
     * @throws yiiwebNotFoundHttpException
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the Movie model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     *
     * @param integer $id
     * @return ppmodelsMovie
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Movie::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }
}

登錄后復(fù)制

其中,actionIndex方法將顯示所有電影的列表,actionCreate和actionUpdate方法將用于創(chuàng)建和編輯電影,actionDelete方法將刪除電影。

    創(chuàng)建電影視圖

接下來,我們需要創(chuàng)建視圖文件來顯示電影列表、添加電影和編輯電影的表單。將視圖文件存儲在views/movie目錄中。

index.php – 用于顯示電影列表

<?php

use yiihelpersHtml;
use yiigridGridView;

/* @var $this yiiwebView */
/* @var $movies appmodelsMovie[] */

$this->title = 'Movies';
$this->params['breadcrumbs'][] = $this->title;
?>

<h1><?= Html::encode($this->title) ?></h1>

<p>
    <?= Html::a('Create Movie', ['create'], ['class' => 'btn btn-success']) ?>
</p>

<?= GridView::widget([
    'dataProvider' => new yiidataArrayDataProvider([
        'allModels' => $movies,
        'sort' => [
            'attributes' => [
                'title',
                'director',
                'year',
                'genre',
                'rating',
            ],
        ],
    ]),
    'columns' => [
        ['class' => 'yiigridSerialColumn'],

        'title',
        'director',
        'actors:ntext',
        'year',
        'genre',
        'rating',

        ['class' => 'yiigridActionColumn'],
    ],
]); ?>

登錄后復(fù)制create.php – 用于創(chuàng)建新的電影

<?php

use yiihelpersHtml;
use yiiwidgetsActiveForm;

/* @var $this yiiwebView */
/* @var $model appmodelsMovie */

$this->title = 'Create Movie';
$this->params['breadcrumbs'][] = ['label' => 'Movies', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<h1><?= Html::encode($this->title) ?></h1>

<div class="movie-form">

    <?php $form = ActiveForm::begin(); ?>

    <?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'director')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'actors')->textarea(['rows' => 6]) ?>

    <?= $form->field($model, 'year')->textInput() ?>

    <?= $form->field($model, 'genre')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'rating')->textInput() ?>

    <div class="form-group">
        <?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>

登錄后復(fù)制update.php – 用于編輯電影

<?php

use yiihelpersHtml;
use yiiwidgetsActiveForm;

/* @var $this yiiwebView */
/* @var $model appmodelsMovie */

$this->title = 'Update Movie: ' . $model->title;
$this->params['breadcrumbs'][] = ['label' => 'Movies', 'url' => ['index']];
$this->params['breadcrumbs'][] = ['label' => $model->title, 'url' => ['view', 'id' => $model->id]];
$this->params['breadcrumbs'][] = 'Update';
?>
<h1><?= Html::encode($this->title) ?></h1>

<div class="movie-form">

    <?php $form = ActiveForm::begin(); ?>

    <?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'director')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'actors')->textarea(['rows' => 6]) ?>

    <?= $form->field($model, 'year')->textInput() ?>

    <?= $form->field($model, 'genre')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'rating')->textInput() ?>

    <div class="form-group">
        <?= Html::submitButton('Save', ['class' => 'btn btn-primary']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>

登錄后復(fù)制

    運(yùn)行電影網(wǎng)站

現(xiàn)在我們已經(jīng)完成了Yii框架電影網(wǎng)站的創(chuàng)建,所有代碼都已經(jīng)就緒。要在本地運(yùn)行電影網(wǎng)站,請在終端中進(jìn)入應(yīng)用程序根目錄,然后執(zhí)行以下命令:

php yii serve

登錄后復(fù)制

這將啟動一個本地Web服務(wù)器,并在端口8000上運(yùn)行你的應(yīng)用程序。現(xiàn)在,你可以在瀏覽器中打開http://localhost:8000/,看到你的電影網(wǎng)站了。

在這篇文章中,我們已經(jīng)演示了如何使用Yii框架創(chuàng)建電影網(wǎng)站。使用Yii框架會加快你的開發(fā)速度,并提供很多有用的特性,例如ActiveRecord、MVC架構(gòu)、表單驗證、安全性等等。要深入了解Yii框架,請查看其文檔。

以上就是使用Yii框架創(chuàng)建電影網(wǎng)站的詳細(xì)內(nèi)容,更多請關(guān)注www.xfxf.net其它相關(guān)文章!

分享到:
標(biāo)簽:Yii框架 創(chuàng)建 電影網(wǎng)站
用戶無頭像

網(wǎng)友整理

注冊時間:

網(wǎng)站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學(xué)四六

運(yùn)動步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績評定2018-06-03

通用課目體育訓(xùn)練成績評定