首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Laravel辛烷值中生成站点地图?

如何在Laravel辛烷值中生成站点地图?
EN

Stack Overflow用户
提问于 2022-08-29 19:30:19
回答 1查看 42关注 0票数 1

我试图为我的网站生成一个站点地图,但我做不到,我的网站使用的是拉拉辛烷。spatie/laravel-sitemap包生成的urls如下所示:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
    <url>
        <loc>http://127.0.0.1:8000/</loc>
        <lastmod>2022-08-27T00:17:40+00:00</lastmod>
        <changefreq>daily</changefreq>
        <priority>0.8</priority>
        </url>
    <url>
        <loc>http://127.0.0.1:8000/my-url</loc>
        <lastmod>2022-08-27T00:17:40+00:00</lastmod>
        <changefreq>daily</changefreq>
        <priority>0.8</priority>
        </url>
    <url>

它没有设置正确的url域:http://myweb.com/my-url,它正在生成这个http://127.0.0.1:8000/my-url。我使用nginx作为反向代理,请求被重定向到辛烷,正如laravel所说:https://laravel.com/docs/9.x/octane#serving-your-application-via-https

另外,sitemap是使用一个命令生成的:

代码语言:javascript
复制
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\URL;
use Spatie\Sitemap\SitemapGenerator;

class GenerateSitemap extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'sitemap:generate';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Generate the sitemap';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        //SitemapGenerator::create(config('app.url'))        
        SitemapGenerator::create('https://myweb.com')
            ->writeToFile(public_path('sitemap.xml'));
    }
}

在调度器中调用该命令:

代码语言:javascript
复制
$schedule->command('sitemap:generate')->daily();

我能做什么?谢谢

EN

回答 1

Stack Overflow用户

发布于 2022-08-29 21:39:56

也许这个能帮到你。

代码语言:javascript
复制
namespace App\Console\Commands;

use Illuminate\Http\Request;
use App\Models\Post;

class SitemapController extends Controller {
    public function index($value = '') {
        $posts = Post::latest()->get();

        return response()->view('sitemap', [
            'posts' => $posts
        ])->header('Content-Type', 'text/xml');
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73533784

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档