需要制作新闻页面。我做到了,在本地服务器(开放服务器)上,一切正常,上传到托管服务器时,出现404错误。 php版本几乎完全相同(托管版本为7.0.2,本地版本为7.1+)。可能是个错误吗?
控制器
OnDestroy()
路由
public function actionNews()
{
$category = NewsCategory::find()->where(['slug' => 'news'])->one();
$news = $category->getChilds()->where(['status' => 'published']);
$countQuery = clone $news;
$pages = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' => 8]);
$models = $news->orderBy('date DESC')->offset($pages->offset)
->limit($pages->limit)
->all();
return $this->render('news', [
'model' => $models,
'pages' => $pages,
]);
}
public function actionView($slug)
{
$model = News::findOne(['slug' => $slug]);
if(!$model = News::findOne(['slug' => $slug])){
throw new NotFoundHttpException('Page not found');
}
return $this->render('view', [
'model' => $model
]);
}
查看(文件位于Blog文件夹中)
'/news' => '/blog/news',
'/news/<slug:[A-Za-z0-9 -_.]+>' => 'blog/view',
'/view' => '/blog/view',
.htaccess
<?php
use yii\helpers\Html;
use yii\helpers\Url;
use yii\widgets\LinkPager;
$this->title = Yii::$app->name . " | Новости";
$this->title = Yii::t('app','Новости');
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="container">
<h3>News company</h3>
<div class="row news-list">
<?php foreach($model as $item) { ?>
<div class="col-sm-3">
<a class="news-img" href="<?=Url::toRoute(['/blog/view', 'slug' =>
$item->slug])?>">
<img src="<?= $item->getImage()->getUrl('920x800') ?>" alt="">
</a>
<a href="#"><?= $item->name; ?></a>
<div class="small grey">
<p><i class="fa fa-calendar"></i> <?= date('d.m.Y',
strtotime($item->date)) ?></p>
<p><?= mb_substr($item->text, 0, 80); ?>...</p>
</div>
</div>
<?php } ?>
</div>
<?
echo LinkPager::widget([
'pagination' => $pages,
]);
?>
</div>
答案 0 :(得分:0)
发现一个错误。那是我不是“使用yii \ data \ Pagination;”,而是使用“ yii \ data \ pagination;