DARK MODE 

Posted on Monday, July 25, 2022 by

Create YouTube Clone with Yii (Part 7): Customizing the Update Video Page

1) Redirect user to the 'Update' page instead of view.

87
88
89
            if (Yii::$app->request->isPost && $model->save()) {
                return $this->redirect(['update', 'video_id' => $model->video_id]);
            }
backend > controllers > VideoController.php 



2) Update the Create page link in the navbar


15
16
17
18
    $menuItems[] = [
        'label' => 'Create',
        'url' => ['/video/create'],
    ];
backend > views > layouts > _header.php 

3) Change the URL format for Update page


Right now, to visit the Update page, the url would be something like /video/update?video_id=s1Den-ln . We want to change it to /video/update/s1Den-ln .

42
43
44
45
46
47
48
        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
                'video/update/<video_id>' => 'video/update'
            ],
        ],
backend > config > main.php 



4) Write what to return to the View in Model & Set Frontend url


26
27
28
29
..
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
..
138
139
140
141
142
143
class Video extends \yii\db\ActiveRecord
{
    const STATUS_UNLISTED = 0;
    const STATUS_PUBLISHED = 1;
...
    // Return timestamp
    public function behaviors()
    {
        return [
            TimestampBehavior::class,
            [
                'class' => BlameableBehavior::class,
                'updatedByAttribute' => false
            ]
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['video_id', 'title'], 'required'],
            [['description'], 'string'],
            [['status', 'has_thumbnail', 'created_at', 'updated_at', 'created_by'], 'integer'],
            [['video_id'], 'string', 'max' => 16],
            [['title', 'tags', 'video_name'], 'string', 'max' => 512],
            [['video_id'], 'unique'],
            ['has_thumbnail', 'default', 'value' => 0], // set default thumbnail value
            ['status', 'default', 'value' => self::STATUS_UNLISTED], // set default status value
            [['created_by'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['created_by' => 'id']],
        ];
    }
...
    // Return the link to the video
    public function getVideoLink()
    {
        return Yii::$app->params['frontendUrl'] . 'storage/videos/' . $this->video_id . '.mp4';
    }
}
common > models > Video.php 

1
2
3
4
5
<?php

return [
    'frontendUrl' => 'http://yutub.test/'
];
common > config > params-lcoal.php 




5) Change the look of the Update page


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<div class="video-form">

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

    <div class="row mr-2">
        <div class="col-sm-8">
            <?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>

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

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

            <div class="form-group">
                <?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
            </div>
        </div>
        <div class="col-sm-4 bg-light p-0">
            <div class=>
                <video class="embed-responsive" src="<?php echo $model->getVideoLink() ?>" controls></video>
                <div class="m-3">
                    <div class="text-muted ts-2">Video link</div>
                    <a href="<?php echo $model->getVideoLink() ?>">Open Video</a>
                </div>
            </div>
            <div class="m-3">
                <div class="text-muted">Filename</div>
                <?php echo $model->video_name ?>
                <?= $form->field($model, 'status')->textInput() ?>
            </div>
        </div>


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

    </div>
</div>
backend > views > video > _form.php 



6) Turn the status into a dropdown

Create a new function in Controller and call the new method in the view.

94
95
96
97
98
99
100
    public function getStatusLabels()
    {
        return [
            self::STATUS_UNLISTED => 'Unlisted',
            self::STATUS_PUBLISHED => 'Published',
        ];
    }
common > models > Video.php 

23
24
25
26
27
            <div class="m-3">
                <div class="text-muted">Filename</div>
                <?php echo $model->video_name ?>
                <?= $form->field($model, 'status')->dropdownList($model->getStatusLabels()) ?>
            </div>
backend > views > video > _form.php 





No comments:

Post a Comment