Yii2 问题:本地托管正常工作和托管错误
Yii2 problems: Normal work on local hosting and error on hosting
需要做一个新闻页面。我做到了,在本地服务器(开放服务器)上一切正常,当上传到托管服务器时,它会出现 404 错误。 php 版本几乎相同(主机上为 7.0.2,本地上为 7.1+)。可能是什么错误?
控制器
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
]);
}
路由
'/news' => '/blog/news',
'/news/<slug:[A-Za-z0-9 -_.]+>' => 'blog/view',
'/view' => '/blog/view',
查看(文件在博客文件夹中)
<?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>
.htaccess
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/admin
RewriteRule ^admin\/?(.*) /backend/web/
RewriteCond %{REQUEST_URI} !^/(frontend/web|backend/web|admin)
RewriteRule (.*) /frontend/web/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /frontend/web/index.php
RewriteCond %{REQUEST_URI} ^/backend/web
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /backend/web/index.php
发现错误。就是这样,我没有“使用 yii\data\Pagination;”,而是“使用 yii\data\pagination;
”
需要做一个新闻页面。我做到了,在本地服务器(开放服务器)上一切正常,当上传到托管服务器时,它会出现 404 错误。 php 版本几乎相同(主机上为 7.0.2,本地上为 7.1+)。可能是什么错误?
控制器
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
]);
}
路由
'/news' => '/blog/news',
'/news/<slug:[A-Za-z0-9 -_.]+>' => 'blog/view',
'/view' => '/blog/view',
查看(文件在博客文件夹中)
<?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>
.htaccess
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/admin
RewriteRule ^admin\/?(.*) /backend/web/
RewriteCond %{REQUEST_URI} !^/(frontend/web|backend/web|admin)
RewriteRule (.*) /frontend/web/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /frontend/web/index.php
RewriteCond %{REQUEST_URI} ^/backend/web
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /backend/web/index.php
发现错误。就是这样,我没有“使用 yii\data\Pagination;”,而是“使用 yii\data\pagination;
”