Code/PHP

[Laravel] SiteMap 만들기

codens 2022. 5. 15. 15:52


    - php 라라벨에서 사이트맵 파일 생성하는 방법


 spatie / laravel-sitemap - 1.7k
https://github.com/spatie/laravel-sitemap
    - v6.1.0 , 2022-01


//-------------------------------------
    - php 7.3, laravel 6에서는 laravel-sitemap v5.8 설치
    - laravel-sitemap  v5.9 는 php7.4이상 필요


    - composer.json 파일 수정
"guzzlehttp/guzzle": "^6.3",


    - 설치
> composer require spatie/laravel-sitemap:^5.8


//-------------------------------------
    - 설정
> php artisan vendor:publish --provider="Spatie\Sitemap\SitemapServiceProvider" --tag=sitemap-config
        - 에러 발생 - 에러 메시지 : 
Unable to locate publishable resources.

        - 해결 방법
> php artisan vendor:publish
    - Provider: Spatie\Sitemap\SitemapServiceProvider 를 찾아서 번호를 입력


//-------------------------------------
config/sitemap.php 설정 파일 참고


//-------------------------------------
    - 명령 파일 생성
> php artisan make:command GenerateSitemap


//-------------------------------------
    - 생성된 파일 app/Console/Commands/GenerateSitemap.php 수정

use Spatie\Sitemap\SitemapGenerator;

class GenerateSitemap extends Command
{
    protected $signature = 'sitemap:generate';

    protected $description = 'Generate the sitemap.';

...

    public function handle()
    {
        SitemapGenerator::create(config('app.url'))
            ->writeToFile(public_path('sitemap.xml'));
    }




//-------------------------------------
    - sitemap 생성 명령
> php artisan sitemap:generate
        - public/sitemap.xml 파일 생성됨



//-------------------------------------
// 참고
How to Automatically Generate Sitemap with Laravel?
https://www.digitalocean.com/community/questions/how-to-automatically-generate-sitemap-with-laravel

 

반응형