Sitemap hay còn gọi là bản đồ trang web, tập hợp các đường dẫn URL của một website. Nó giúp hỗ trợ điều hướng người dùng trên web cũng như giúp con bot của Google có thể dễ dàng thu thập dữ liệu và đánh chỉ mục website của bạn. Sitemap là file quan trọng và nên có ở tất cả các website.
Có 2 cách để tạo sitemap file.
- Hiện nay có nhiều website hỗ trợ tạo nhưng hạn chế của nó là khi website của mình cập nhật link mới thì chúng ta phải vào lại website của họ để xuất lại file sitemap, điều này rất bất tiện và mất thời gian. Nên mình sẽ hướng dẫn các bạn dùng code laravel để tạo file sitemap và tự động update file cùng với cronjob.
- Đầu tiên, cài đặt thư viện roumen/sitemap bằng lệnh composer require roumen/sitemap trên command line. Như vậy chúng ta đã cài đặt được thư viện để tạo file sitemap
- Tiếp theo chạy lệnh: php artisan make:command CreateSiteMap --command=sitemap:create . Lệnh này sẽ tạo 1 file CreateSiteMap.php trong thư mục app/console/commands có nội dung như sau:
<?php
namespace AppConsoleCommands;
use IlluminateConsoleCommand;
class CreateSiteMap extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'sitemap:create';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
//
}
}
Các bạn cần phải sửa lại cho phù hợp với website của mỗi người, đây là code của mình dùng để tạo sitemap cho blog:
<?php
namespace AppConsoleCommands;
use IlluminateConsoleCommand;
class CreateSiteMap extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'sitemap:create';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
// create new sitemap object
$sitemap = App::make("sitemap");
// add items to the sitemap (url, date, priority, freq)
$sitemap->add(URL::to('/'), '2021-02-25T20:10:00+02:00', '1.0', 'daily');
$categories = DB::table('categorys')->orderBy('created_at', 'desc')->get();
foreach ($categories as $category)
{
$sitemap->add(env('APP_URL'). "/danhmuc{$category->id}-{$category->slug}", $category->created_at, '0.8', 'daily');
}
// get all posts from db
$posts = DB::table('posts')
->where('deleted_at', '=', null)
->orderBy('created_at', 'desc')
->get();
// add every post to the sitemap
foreach ($posts as $post)
{
$sitemap->add(env('APP_URL'). "/post{$post->id}-{$post->slug}.html", $post->created_at, '0.7', 'daily');
}
// generate your sitemap (format, filename)
$sitemap->store('xml', './sitemap');
}
}
Trong file: appConsoleKernel.php function schedule() thêm lịch chạy command: $schedule->command('sitemap:create')->daily();
Sau đó chỉ cần chạy lệnh sau: php artisan sitemap:create trong project để tạo file. Bạn gõ lên trình duyệt yourdomain/sitemap.xml, nếu xuất hiện kiểu thế này là bạn đã tạo thành công
Như vậy là bạn đã hoàn thành việc tạo file sitemap cho website. Cuối cùng bạn chỉ cần chạy cronjob trên host để file sitemap được update hằng ngày (hằng tuần, hằng tháng, ...).
Bạn vào mục cronjob trên hosting của bạn để config. Ở đây mình dùng hosting của Tinohost, các bạn có thể đăng ký tại đây. Command để add là: /usr/local/bin/ea-php72 /home/nhatcode/nhatcoder.com/artisan sitemap:create.
Giải thích:
Cần chọn thêm thời gian để cronjob tự động chạy. Sau khi save cronjob nó sẽ tự động tạo file sitemap.xml trong folder /public của project. Nếu dự án của bạn không move tất cả file trong /public đến /public_html ngoài hosting thì tới đây là xong rồi, còn nếu đã move rồi thì bạn cần phải edit lại đường dẫn xuất file trong file CreateSiteMap.php phía trên tí nhé: $sitemap->store('xml', './../../public_html/sitemap'). Điều này sẽ tạo file sitemap.xml nằm cùng cấp với file index.php trong folder /public_html (sitemap phải nằm cùng cấp với file index.php)
OK, DONE.
Hi vọng bài viết sẽ giúp ích cho các bạn mới bắt đầu tạo sitemap và chạy cronjob.