Реклама
Рубрики блога
Архив статей
Облако тегов
Laravel - создание карты сайта
воскресенье, 13 января 2019 08:29:00, написал admin
Создадим карту сайта для Laravel с использованием Laravelium/laravel-sitemap
Ставим это расширение: https://github.com/Laravelium/laravel-sitemap
composer require laravelium/sitemap
Выполняем
php artisan vendor:publish --provider="Laravelium\Sitemap\SitemapServiceProvider"
Подключаем новый роутер
Route::get('sitemap', 'SitemapController@index');
Создаем контроллер
php artisan make:controller SitemapController
Листинг контроллера
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Jenssegers\Date\Date;
use Laravelium\Sitemap\Sitemap;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\URL;
class SitemapController extends Controller
{
public function index() {
$sitemap = App::make("sitemap");
$sitemap->add(URL::to('/'), Date::now(), '1.0', 'monthly');
$pages = DB::table('pages')->orderBy('created_at', 'desc')->get();
foreach ($pages as $page)
{
$sitemap->add(route('page', ['slug' =>$page->slug]), $page->updated_at, '0.5', 'yearly');
}
$articles = DB::table('posts')->orderBy('created_at', 'desc')->get();
foreach ($articles as $article)
{
$sitemap->add(route('blog.show', ['slug' =>$article->slug]), $article->updated_at, '0.5', 'yearly');
}
$categories = DB::table('categories')->orderBy('created_at', 'desc')->get();
foreach ($categories as $category)
{
$sitemap->add(route('blog.category', ['slug' =>$category->slug]), $category->updated_at, '0.5', 'yearly');
}
$tags = DB::table('categories')->orderBy('created_at', 'desc')->get();
foreach ($tags as $tag)
{
$sitemap->add(route('blog.tag', ['slug' =>$tag->slug]), $tag->updated_at, '0.5', 'yearly');
}
$sitemap->store('xml', 'sitemap');
}
}
Вызываем /sitemap и получаем на выходе /sitemap.xml
Можно поставить генерацию на крон
01 05 * * * user /usr/bin/curl -s http://yourserver.com/sitemap >> /dev/nullLaravel sitemap Laravel