简体   繁体   中英

how to generate a sitemap in Laravel octane?

I am trying to generate a sitemap for my website but I can't, my website is using laravel octane. The urls that the spatie/laravel-sitemap package generates are like these:

<?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>

It is no setting the proper url domain: http://myweb.com/my-url it is generating this http://127.0.0.1:8000/my-url . I am using nginx as reverse proxy and the requests are redirected to octane, as the laravel docs says: https://laravel.com/docs/9.x/octane#serving-your-application-via-https

Also the sitemap is being generated using a command:

<?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'));
    }
}

and that command is called in the scheduler:

$schedule->command('sitemap:generate')->daily();

What can I do? thanks.

Maybe this will help you.

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');
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM