watson/sitemap 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

watson/sitemap

Composer 安装命令:

composer require watson/sitemap

包简介

Generate Google Sitemaps in Laravel

README 文档

README

Build Status Total Downloads Latest Stable Version Latest Unstable Version License

Sitemap is a package built specifically for Laravel that will help you generate XML sitemaps for Google. Based on laravel-sitemap this package operates in a slightly different way to better fit the needs of our project. A facade is used to access the sitemap class and we have added the ability to produce sitemap indexes as well as sitemaps. Furthermore, it's tested.

Read more about sitemaps and how to use them efficiently on Google Webmaster Tools.

Installation

composer require watson/sitemap

Usage

Creating sitemap indexes

If you have a large number of links (50,000+) you will want to break your sitemaps out into seperate sitemaps with a sitemap index linking them all. You add sitemap indexes using Sitemap::addSitemap($location, $lastModified) and then you return the sitemap index with Sitemap::renderSitemapIndex(). The $lastModified variable will be parsed and converted to the right format for a sitemap.

Here is an example controller that produces a sitemap index.

namespace App\Http\Controllers;

use Sitemap;

class SitemapsController extends Controller
{
    public function index()
    {
        // Get a general sitemap.
        Sitemap::addSitemap('/sitemaps/general');

        // You can use the route helpers too.
        Sitemap::addSitemap(route('sitemaps.posts'));

        // Return the sitemap to the client.
        return Sitemap::index();
    }
}

Simply route to this as you usually would, Route::get('sitemap', 'SitemapsController@index');.

Creating sitemaps

Similarly to sitemap indexes, you just add tags for each item in your sitemap using Sitemap::addTag($location, $lastModified, $changeFrequency, $priority). You can return the sitemap with Sitemap::renderSitemap(). Again, the $lastModified variable will be parsed and convered to the right format for the sitemap.

If you'd like to just get the raw XML, simply call Sitemap::xml().

Here is an example controller that produces a sitemap for blog posts.

namespace App\Http\Controllers;

use Post;
use Sitemap;

class SitemapsController extends Controller
{
    public function posts()
    {
        $posts = Post::all();

        foreach ($posts as $post) {
            Sitemap::addTag(route('posts.show', $post), $post->updated_at, 'daily', '0.8');
        }

        return Sitemap::render();
    }
}

If you just want to pass a model's updated_at timestamp as the last modified parameter, you can simply pass the model as the second parameter and the sitemap will use that attribute automatically.

If you're pulling a lot of records from your database you might want to consider restricting the amount of data you're getting by using the select() method. You can also use the chunk() method to only load a certain number of records at any one time as to not run out of memory.

Image sitemaps

You can use Google image extensions for sitemaps to give Google more information about the images available on your pages. Read the specification

Images are associated with page and you can use up to 1000 images per page.

Here is an example of adding image tag to usual page tag.

namespace App\Http\Controllers;

use Page;
use Sitemap;

class SitemapsController extends Controller
{
    public function pages()
    {
        $pages = Page::all();

        foreach ($pages as $page) {
            $tag = Sitemap::addTag(route('pages.show', $page), $page->updated_at, 'daily', '0.8');

            foreach ($page->images as $image) {
                $tag->addImage($image->url, $image->caption);
            }
        }

        return Sitemap::render();
    }
}

Here is the full list of arguments to add an image to a tag.

$tag->addImage($location, $caption, $geoLocation, $title, $licenceUrl);

Configuration

To publish the configuration file for the sitemap package, simply run this Artisan command:

php artisan config:publish watson/sitemap

php artisan vendor:publish --provider="Watson\Sitemap\SitemapServiceProvider"

Then take a look in config/sitemap.php to see what is available.

Caching

By default, caching is disabled. If you would like to enable caching, simply set cache_enabled in the configuration file to true. You can then specify how long you would like your views to be cached for. Keep in mind that when enabled, caching will affect each and every request made to the sitemap package.

Multilingual tags

If you'd like to use a mutlilingual tag, simply pass an instance of one to the addTag method. Below is a crude example of how you would pass alternate tag locations for different languages.

Sitemap::addTag(new \Watson\Sitemap\Tags\MultilingualTag(
    $location,
    $lastModified,
    $changeFrequency,
    $priority,
    [
        'en' => $location . '?lang=en',
        'fr' => $location . '?lang=fr'
    ]
));

watson/sitemap 适用场景与选型建议

watson/sitemap 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.39M 次下载、GitHub Stars 达 268, 最近一次更新时间为 2014 年 02 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「sitemaps」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 watson/sitemap 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 watson/sitemap 我们能提供哪些服务?
定制开发 / 二次开发

基于 watson/sitemap 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.39M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 274
  • 点击次数: 17
  • 依赖项目数: 6
  • 推荐数: 0

GitHub 信息

  • Stars: 268
  • Watchers: 10
  • Forks: 40
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-02-04