带有 Laravel 的站点地图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23468906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:28:54  来源:igfitidea点击:

Sitemap with laravel

phpxmllaravelsitemap

提问by phper

I works in my project by LARAVEL.

我在 LARAVEL 的项目中工作。

I want to make sitemap. This is my controller:

我想做站点地图。这是我的控制器:

class SitemapController extends BaseController {
        public function index() {
                header("Content-Type: text/xml;charset=utf-8");
                return View::make('sitemap');
        }
}

And This is my view sitemap.blade.php:

这是我的观点sitemap.blade.php

{{<?xml version="1.0" encoding="UTF-8" ?>}}
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
        <url>
                <loc>{{url()}}</loc>
                <priority>0.5</priority>
        </url>
        .
        .
        .
</urlset>

But result not appeared as XML. It appeared as a normal text.

但结果没有以 XML 形式出现。它以普通文本的形式出现。

采纳答案by phper

It works when I used:

它在我使用时有效:

{{'<?xml version="1.0" encoding="UTF-8" ?>'}}

and I updated my controller as:

我将控制器更新为:

class SitemapController extends BaseController {
        public function index() {
                $content = View::make('sitemap');
                return Response::make($content)->header('Content-Type', 'text/xml;charset=utf-8');
        }
}

回答by Sophy

I suggest you should use this package https://github.com/RoumenDamianoff/laravel-sitemap

我建议你应该使用这个包https://github.com/RoumenDamianoff/laravel-sitemap

Installation

安装

Add the following to your composer.jsonfile :

将以下内容添加到您的composer.json文件中:

"roumen/sitemap": "dev-master"

"roumen/sitemap": "dev-master"

Then register this service provider with Laravel :

然后用 Laravel 注册这个服务提供者:

'Roumen\Sitemap\SitemapServiceProvider',

'Roumen\Sitemap\SitemapServiceProvider',

Publish configuration file. (OPTIONAL)

发布配置文件。(可选的)

php artisan config:publish roumen/sitemap

回答by Amr

Add the XML declaration like this:

像这样添加 XML 声明:

<?php echo '<?xml version="1.0" encoding="UTF-8"?>' ?>

回答by Dave

Wrap the XML declaration in single quotes:

将 XML 声明括在单引号中:

{{'<?xml version="1.0" encoding="UTF-8" ?>'}}

Works for my in Laravel.

在 Laravel 中对我来说有效。