no message
This commit is contained in:
parent
5d85fb8bc3
commit
a82f234853
30
app/Http/Controllers/RobotsController.php
Normal file
30
app/Http/Controllers/RobotsController.php
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use Illuminate\Http\Response;
|
||||||
|
|
||||||
|
class RobotsController extends Controller
|
||||||
|
{
|
||||||
|
public function index(): Response
|
||||||
|
{
|
||||||
|
// $sContent 保存 robots.txt 的具体规则
|
||||||
|
$sContent = implode("\n", [
|
||||||
|
'User-agent: *',
|
||||||
|
'Allow: /',
|
||||||
|
'',
|
||||||
|
'# 屏蔽 Laravel 系统路径',
|
||||||
|
'Disallow: /admin/',
|
||||||
|
'Disallow: /api/',
|
||||||
|
'Disallow: /storage/',
|
||||||
|
'Disallow: /vendor/',
|
||||||
|
'Disallow: /*?*', // 屏蔽带参数的重复 URL,防止稀释权重',
|
||||||
|
'',
|
||||||
|
'# 声明 Sitemap 绝对路径(请替换为你的实际域名)',
|
||||||
|
'Sitemap: ' . config('app.url') . '/sitemap.xml',
|
||||||
|
]);
|
||||||
|
|
||||||
|
return response($sContent, 200)
|
||||||
|
->header('Content-Type', 'text/plain');
|
||||||
|
}
|
||||||
|
}
|
||||||
33
app/Http/Controllers/SitemapController.php
Normal file
33
app/Http/Controllers/SitemapController.php
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use App\Models\Articles;
|
||||||
|
use Illuminate\Http\Response;
|
||||||
|
|
||||||
|
class SitemapController extends Controller
|
||||||
|
{
|
||||||
|
public function index(): Response
|
||||||
|
{
|
||||||
|
// 连表查询分类:按 aOpt['noindexday'] 动态过滤过期文章
|
||||||
|
$cArticles = Articles::where('iStatus', 1)
|
||||||
|
->whereHas('oCategory', function ($q) {
|
||||||
|
$q->where(function ($query) {
|
||||||
|
// 1. 没有配置 noindexday,或者 noindexday = 0 的文章,一直保留
|
||||||
|
$query->whereNull('aOpt')
|
||||||
|
->orWhereRaw("JSON_UNQUOTE(JSON_EXTRACT(aOpt, '$.noindexday')) IS NULL")
|
||||||
|
->orWhereRaw("JSON_UNQUOTE(JSON_EXTRACT(aOpt, '$.noindexday')) = '0'")
|
||||||
|
// 2. 配置了天数的,只取 N 天内创建的新文章
|
||||||
|
->orWhereRaw("articles.created_at >= NOW() - INTERVAL CAST(JSON_UNQUOTE(JSON_EXTRACT(aOpt, '$.noindexday')) AS UNSIGNED) DAY");
|
||||||
|
});
|
||||||
|
})
|
||||||
|
->orderBy('created_at', 'desc')
|
||||||
|
->get();
|
||||||
|
|
||||||
|
$sXmlContent = view('sitemap', [
|
||||||
|
'cArticles' => $cArticles
|
||||||
|
])->render();
|
||||||
|
|
||||||
|
return response($sXmlContent, 200)->header('Content-Type', 'text/xml');
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -76,6 +76,31 @@ public function toSearchableArray(): array
|
|||||||
'updated_at' => (int) $this->updated_at->timestamp,
|
'updated_at' => (int) $this->updated_at->timestamp,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断当前文章是否超过分类设置的 SEO 暴露天数
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function bIsSeoExpired(): bool
|
||||||
|
{
|
||||||
|
$oCategory = $this->oCategory;
|
||||||
|
|
||||||
|
// 未关联分类,或分类下的 aOpt 没设置,默认不处理(不过期)
|
||||||
|
if (!$oCategory || empty($oCategory->aOpt) || !isset($oCategory->aOpt['noindexday'])) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$iNoindexDay = (int) $oCategory->aOpt['noindexday'];
|
||||||
|
|
||||||
|
// 0 或负数代表不限制,一直暴露给搜索引擎
|
||||||
|
if ($iNoindexDay <= 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 判断文章创建时间是否早于 N 天前
|
||||||
|
$oCreateDate = \Carbon\Carbon::parse($this->created_at);
|
||||||
|
return $oCreateDate->lt(\Carbon\Carbon::now()->subDays($iNoindexDay));
|
||||||
|
}
|
||||||
|
|
||||||
protected static function booted()
|
protected static function booted()
|
||||||
{
|
{
|
||||||
|
|||||||
@ -1,2 +0,0 @@
|
|||||||
User-agent: *
|
|
||||||
Disallow:
|
|
||||||
19
resources/views/sitemap.blade.php
Executable file
19
resources/views/sitemap.blade.php
Executable file
@ -0,0 +1,19 @@
|
|||||||
|
{!! '<' . '?xml version="1.0" encoding="UTF-8"?' . '>' !!}
|
||||||
|
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
||||||
|
{{-- 首页 --}}
|
||||||
|
<url>
|
||||||
|
<loc>{{ config('app.url') }}</loc>
|
||||||
|
<priority>1.0</priority>
|
||||||
|
<changefreq>daily</changefreq>
|
||||||
|
</url>
|
||||||
|
|
||||||
|
{{-- 未过期的文章 --}}
|
||||||
|
@foreach($cArticles as $oArticles)
|
||||||
|
<url>
|
||||||
|
<loc>{{ config('app.url') }}/a/{{ $oArticles->sSlug }}.html</loc>
|
||||||
|
<lastmod>{{ \Carbon\Carbon::parse($oArticles->updated_at)->tz('UTC')->toAtomString() }}</lastmod>
|
||||||
|
<priority>0.8</priority>
|
||||||
|
<changefreq>daily</changefreq>
|
||||||
|
</url>
|
||||||
|
@endforeach
|
||||||
|
</urlset>
|
||||||
@ -9,6 +9,11 @@
|
|||||||
use App\Http\Controllers\Web\ArticlesController;
|
use App\Http\Controllers\Web\ArticlesController;
|
||||||
use App\Http\Controllers\Api\v1\ArticlesController as ApiV1ArticlesController;
|
use App\Http\Controllers\Api\v1\ArticlesController as ApiV1ArticlesController;
|
||||||
use App\Http\Controllers\Api\v1\FreenodeController as ApiV1FreenodeController;
|
use App\Http\Controllers\Api\v1\FreenodeController as ApiV1FreenodeController;
|
||||||
|
use App\Http\Controllers\RobotsController;
|
||||||
|
use App\Http\Controllers\SitemapController;
|
||||||
|
|
||||||
|
Route::get('robots.txt', [RobotsController::class, 'index']);
|
||||||
|
Route::get('sitemap.xml', [SitemapController::class, 'index']);
|
||||||
|
|
||||||
Route::get('/test/hello', [TestController::class, 'hello']); // 找个控制器为什么无效了
|
Route::get('/test/hello', [TestController::class, 'hello']); // 找个控制器为什么无效了
|
||||||
Route::get('/test/test1', [TestController::class, 'test1']);
|
Route::get('/test/test1', [TestController::class, 'test1']);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user