В Yii делаю робкие шаги и нужна конструктивная критика.
Будьте добры, объясните мне, правильно ли я делаю модели, контроллеры и вид? Если не правильно, то как бы вы сделали.
Код 100% рабочий, но хотелось бы его оптимизировать.
Контроллер
Код: Выделить всё
<?php
namespace app\controllers;
use yii\web\Controller;
use app\models\Posts;
class PostsController extends Controller
{
public function actionIndex()
{
$query = Posts::find()->select('post_id, post_title, post_short, post_img_prev, post_img')->orderBy('post_id DESC');
$pages = new \yii\data\Pagination(['totalCount' => $query->count(), 'pageSize' => 2, 'pageSizeParam' => false, 'forcePageParam'=> false]);
$posts = $query->offset($pages->offset)->limit($pages->limit)->all();
return $this->render('index', compact('posts', 'pages'));
}
public function actionView()
{
$id = \Yii::$app->request->get('id');
$post = Posts::findOne($id);
$images = $post->images;
if(empty($post)) throw new \yii\web\HttpException(404, 'Такой страницы нет...');
return $this->render('view', compact('post', 'images'));
}
}
Код: Выделить всё
<?php
namespace app\models;
use Yii;
use \yii\db\ActiveRecord;
/**
* This is the model class for table "posts".
*
* @property integer $post_id
* @property string $post_title
* @property string $post_short
* @property string $post_text
* @property integer $post_category_id
* @property string $post_img_prev
* @property string $post_img
* @property string $post_data
* @property string $post_url
*/
class Posts extends ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'posts';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['post_title', 'post_short', 'post_text', 'post_category_id', 'post_img_prev', 'post_img', 'post_data', 'post_url'], 'required'],
[['post_text'], 'string'],
[['post_category_id'], 'integer'],
[['post_data'], 'safe'],
[['post_title', 'post_img_prev', 'post_img', 'post_url'], 'string', 'max' => 100],
[['post_short'], 'string', 'max' => 500],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'post_id' => 'Post ID',
'post_title' => 'Post Title',
'post_short' => 'Post Short',
'post_text' => 'Post Text',
'post_category_id' => 'Post Category ID',
'post_img_prev' => 'Post Img Prev',
'post_img' => 'Post Img',
'post_data' => 'Post Data',
'post_url' => 'Post Url',
];
}
public function getImages()
{
return $this->hasMany(Images::className(), ['image_post_id' => 'post_id']);
}
}
Код: Выделить всё
<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "images".
*
* @property integer $image_id
* @property integer $image_post_id
* @property string $image_url
* @property string $image_preview_url
* @property string $image_alt_post_title
*/
class Images extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'images';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['image_post_id', 'image_url', 'image_preview_url', 'image_alt_post_title'], 'required'],
[['image_post_id'], 'integer'],
[['image_url', 'image_preview_url', 'image_alt_post_title'], 'string', 'max' => 100],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'image_id' => 'Image ID',
'image_post_id' => 'Image Post ID',
'image_url' => 'Image Url',
'image_preview_url' => 'Image Preview Url',
'image_alt_post_title' => 'Image Alt Post Title',
];
}
public function getPosts()
{
return $this->hasOne(Posts::className(), ['post_id' => 'image_post_id']);
}
}
Код: Выделить всё
<?php if (!empty($posts)): ?>
<?php foreach ($posts as $post): ?>
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">
<a href="<?= yii\helpers\Url::to(['posts/view', 'id' => $post->post_id])?>"><?= $post->post_title ?></a>
</h3>
</div>
<div class="panel-body">
<a href="<?= yii\helpers\Url::to(['posts/view', 'id' => $post->post_id])?>">
<img src="<?= $post->post_img_prev ?>" alt="<?= $post->post_title ?>" class="img-responsive img-thumbnail">
</a>
</br>
<?= $post->post_short ?>
</div>
</div>
<?php endforeach; ?>
<?= \yii\widgets\LinkPager::widget(['pagination' => $pages]) ?>
<?php endif; ?>
Код: Выделить всё
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">
<?=$post->post_title?></a>
</h3>
</div>
<div class="panel-body">
<div class="row">
<?php foreach ($images as $image): ?>
<!-- -->
<div class="col-xs-6 col-sm-4 col-md-3 col-lg-2">
<a href="#" data-toggle="modal" data-target="#myModal" data-text="<?= $image->image_url ?>" >
<img src="<?= $image->image_preview_url ?>" alt="<?= $post->post_title ?>" class="img-responsive img-thumbnail thumbnail">
</a>
</div>
<?php endforeach; ?>
</div>
<p>
<?= $post->post_text ?>
</p>
<!-- HTML-код модального окна -->
<div class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true" id="myModal">
<div class="modal-dialog">
<div class="modal-content">
<!-- Заголовок модального окна -->
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title"><?= $post->post_title ?></h4>
</div>
<div class="modal-body">
<div id="result">Загрузка...</div>
</div>
</div>
</div>
</div>
</div>
</div>
Код: Выделить всё
$('#myModal').on('shown.bs.modal', function (event) {
data = $( event.relatedTarget ).data( "text" );
data2 = '<img src="'+data+'" '+'class="img-responsive img-thumbnail"'+'>';
$( "#result" ).html( data2 );
});
$('#myModal').on('hidden.bs.modal', function (event) {
data = $( event.relatedTarget ).data( "text" );
$( "#result" ).html( '' );
});