https://code.tutsplus.com/tutorials/how ... -cms-23222
https://coderius.biz.ua/blog/article/yi ... a-avtomate
Создал админ панель, которая создает статью, вставляет в поле (alias) ссылки от производной статьи. С этим проблем нет.
У меня сложность с выводом и переходом по ссылкам.
advanced/config/main.php
Код: Выделить всё
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'defaultRoute' => '/site/index',
'site/<slug>' => 'site/slug',
],
],
Код: Выделить всё
novosti/istoriya-v-nashih-serdtsah
video/istoriya-v-nashih-serdtsah
Контроллер(SiteController):
Код: Выделить всё
public function actionIndex()
{
$query = Blog::find()->select('id, title,images')->orderBy('id DESC');
$pages = new \yii\data\Pagination(['totalCount' =>$query->count(), 'pageSize' => 20, 'pageSizeParam' => false, 'forcePageParam' => false]);
$posts = $query->offset($pages->offset)->limit(5)->all();
return $this->renderFile('@app/views/site/index.php', compact('posts', 'pages'));
}
public function actionView(){
$id = \Yii::$app->request->get('id');
$post = Blog::findOne($id);
if(empty($post)) throw new \yii\web\HttpException(404,'Такой страницы нет');
return $this->renderFile('@app/views/site/view.php', compact('post'));
}
Код: Выделить всё
<?php
namespace site\models;
use yii\behaviors\SluggableBehavior;
/**
* This is the model class for table "news".
*
* @property int $id
* @property string|null $title
* @property string|null $text
* @property string|null $images
*/
class Blog extends \yii\db\ActiveRecord
{
public function behaviors()
{
return [
[
'class' => SluggableBehavior::className(),
'attribute' => 'title',
'slugAttribute' => 'alias',
],
];
}
/**
* {@inheritdoc}
*/
public static function tableName()
{
return '{{blog}}';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['title', 'text', 'images'], 'string'],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'title' => 'Заголовок',
'images' => 'Изображение',
'text' => 'Текст',
];
}
}
Код: Выделить всё
<div class="row">
<section class="grid">
<?php if (!empty($posts)): ?>
<?php foreach($posts as $post): ?>
<article class="grid-item">
<div class="image">
<img src="<?=$post->images?>" />
</div>
<div class="info">
<h2><?=$post->title?></h2>
<div class="info-text">
<p> <?=$post->anonce?></p>
</div>
<div class="button-wrap">
<?= Html::a(Yii::t('app', 'Подробнее'), ['site', 'id' => $post->id], ['class' => 'atuin-btn']) ?>
</div>
</div>
Код: Выделить всё
...
<div class="panel panel-default">
<div class="panel-heading">
<b><?=$post->title?></b>
</div>
<div class="panel-body">
<?=$post->id?><br>
</div>
<div class="panel-body">
<?= Html::a(Yii::t('app', 'назад'), ['site/view', 'id' => $post->id], ['class' => 'btn btn-success']) ?>
</div>
</div>
....
Но мне нужны ссылки типа:
site.ru/novosti/istoriya-v-nashih-serdtsah
site.ru/video/istoriya
и пр.
Как уже писал - категории и заголовки хранятся в поле alias
Но у меня не генерируются и не получается проходить по данным ссылкам когда я делаю следующее:
В контроллере пишу:
Код: Выделить всё
public function actionSlug($slug){
$id = \Yii::$app->request->get('alias');
$post = Blog::find()->where(['alias'=>$slug])->one();
if(empty($post)) throw new \yii\web\HttpException(404,'Такой страницы нет');
return $this->renderFile('@app/views/site/view.php', compact('post'));
}
public function actionSlugs($slug)
{
$model = Blog::find()->where(['alias'=>$slug])->one();
if (!is_null($model)) {
return $this->renderFile('@app/views/site/view.php', compact('post'));
} else {
return $this->redirect('/site/index');
}
}
В чем может быть ошибка?