samkitano / seo-gen
在 Laravel 4.2 中生成 sitemap.xml 和 robots.txt 文件,支持多语言。基于 Hettiger/SeoAggregator
dev-master
2015-06-16 11:27 UTC
Requires
- php: >=5.4.0
- illuminate/support: 4.2.*
This package is not auto-updated.
Last update: 2024-09-14 17:32:03 UTC
README
在 Laravel 4.2 中生成 robots.txt 和多语言 sitemap.xml 文件。
安装
使用 composer 需求
// composer.json "require": { "php": ">=5.4.0", "samkitano/seo-gen": "dev-master", // ... },
在 app\config\app.php
中添加提供者
// ... 'Samkitano\SeoGen\SeoGenServiceProvider',
发布配置文件
php artisan config:publish samkitano/seo-gen
修改 app\config\packages\samkitano\seo-gen\config.php
以适应您的需求。
在 'translated_routes_file'
中提供您用于翻译路由的文件名。不包括扩展名。
如果您不需要翻译 URL 前缀(在这种情况下您现在不会在这里,对吧?)您可能需要使用另一个包,但请随时提出拉取请求并进行一些修改 :)
例如,我们将使用默认的 'translated_routes_file' => 'routes',
Laravel 4.2 的使用示例
显然,您需要准备您的翻译文件
/** * app\lang\en\routes.php */ return array( 'home' => 'home', 'portfolio' => 'portfolio', 'contacts' => 'contacts', 'pages' => 'pages', // ... );
/** * app\lang\pt\routes.php */ return array( 'home' => 'inicio', 'portfolio' => 'portefolio', 'contacts' => 'contactos', 'pages' => 'paginas', // ... );
/** * app\lang\fr\routes.php */ return array( 'home' => 'accueil', 'portfolio' => 'portefeuille', 'contacts' => 'contacts', 'pages' => 'pages', // ... );
/** * app\lang\de\routes.php */ return array( 'home' => 'startseite', 'portfolio' => 'portefeuille', 'contacts' => 'kontakte', 'pages' => 'seiten', // ... );
SeoGen 将查看您的应用配置文件以确定应用的默认语言和可用语言。
/** * app\config\app.php */ /* |-------------------------------------------------------------------------- | Application Locale Configuration |-------------------------------------------------------------------------- | | The application locale determines the default locale that will be used | by the translation service provider. You are free to set this value | to any of the locales which will be supported by the application. | */ 'locale' => 'en', /* |-------------------------------------------------------------------------- | Available Languages |-------------------------------------------------------------------------- | | Supported Languages | */ 'languages' => array('en', 'pt', 'fr', 'de'),
现在您可以在应用中的任何地方使用 SeoGen。
为了简化示例,我们将在 app\routes.php
中直接进行,但您应该使用控制器。
/** * app/routes.php */ use Samkitano\SeoGen\Facades\Sitemap; use Samkitano\SeoGen\Facades\Robots; // Language Selection $languages = array('en', 'pt', 'fr', 'de'); $locale = Request::segment(1); if ( in_array($locale, $languages) ) { \App::setLocale($locale); Session::put('locale', $locale); } else { $locale = null; } Route::group( array('prefix' => $locale), function() { Route::get( trans('routes.home'), array('as' => 'home', 'uses' => 'ExampleController@home') ); Route::get( trans('routes.portfolio'), array('as' => 'portfolio', 'uses' => 'ExampleController@portfolio') ); Route::get( trans('routes.contacts'), array('as' => 'contacts', 'uses' => 'ExampleController@contacts') ); Route::get( trans('routes.pages' . '/{slug}'), array('as' => 'pages', 'uses' => 'ExampleController@pages') ); } ); Route::get('sitemap.xml', function() { $date_time = new DateTime('now'); Sitemap::addLink($date_time, 'home'); Sitemap::addLink($date_time, 'portfolio'); Sitemap::addLink($date_time, 'contacts'); $collection = Pages::all(); Sitemap::addCollection($collection, 'pages'); return Response::make( Sitemap::getSitemapXml() ) ->header('Content-Type', 'text/xml'); }); Route::get('robots.txt', function() { Robots::disallowPath('/admin'); return Response::make( Robots::getRobotsDirectives(true) ) ->header('Content-Type', 'text/plain'); });
上面的示例应该返回一个包含类似以下内容的 sitemap
<?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"> <url> <loc>http://example.com/en/home</loc> <xhtml:link rel="alternate" hreflang="pt" href="http://example.com/pt/inicio"/> <xhtml:link rel="alternate" hreflang="fr" href="http://example.com/fr/accueil"/> <xhtml:link rel="alternate" hreflang="de" href="http://example.com/de/startseite"/> <lastmod>2015-06-16</lastmod> </url> <url> <loc>http://example.com/en/portfolio</loc> <xhtml:link rel="alternate" hreflang="pt" href="http://example.com/pt/portefolio"/> <xhtml:link rel="alternate" hreflang="fr" href="http://example.com/fr/portefeuille"/> <xhtml:link rel="alternate" hreflang="de" href="http://example.com/de/portefeuille"/> <lastmod>2015-06-16</lastmod> </url> <url> <loc>http://example.com/en/contacts</loc> <xhtml:link rel="alternate" hreflang="pt" href="http://example.com/pt/contactos"/> <xhtml:link rel="alternate" hreflang="fr" href="http://example.com/fr/contacts"/> <xhtml:link rel="alternate" hreflang="de" href="http://example.com/de/kontakte"/> <lastmod>2015-06-16</lastmod> </url> <url> <loc>http://example.com/en/pages/page-1</loc> <xhtml:link rel="alternate" hreflang="pt" href="http://example.com/pt/paginas/page-1"/> <xhtml:link rel="alternate" hreflang="fr" href="http://example.com/fr/pages/page-1"/> <xhtml:link rel="alternate" hreflang="de" href="http://example.com/de/seiten/page-1"/> <lastmod>2015-06-10</lastmod> </url> <url> <loc>http://example.com/en/pages/page-2</loc> <xhtml:link rel="alternate" hreflang="pt" href="http://example.com/pt/paginas/page-2"/> <xhtml:link rel="alternate" hreflang="fr" href="http://example.com/fr/pages/page-2"/> <xhtml:link rel="alternate" hreflang="de" href="http://example.com/de/seiten/page-2"/> <lastmod>2015-06-10</lastmod> </url> <url> <loc>http://example.com/en/pages/page-3</loc> <xhtml:link rel="alternate" hreflang="pt" href="http://example.com/pt/paginas/page-3"/> <xhtml:link rel="alternate" hreflang="fr" href="http://example.com/fr/pages/page-3"/> <xhtml:link rel="alternate" hreflang="de" href="http://example.com/de/seiten/page-3"/> <lastmod>2015-06-10</lastmod> <url> </urlset>
和 robots.txt
User-agent: *
Disallow: /admin
Sitemap: http://example.com/sitemap.xml
注意事项
目前 SeoGen 不翻译 slugs。
许可证
SEO Aggregator 是开源软件,许可协议为 MIT 协议