在不使用composer的情况下,在laravel项目中生成动态站点地图 [英] generate dynamic sitemaps in a laravel project without using composer

查看:75
本文介绍了在不使用composer的情况下,在laravel项目中生成动态站点地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的laravel项目生成Dynamic sitemap.我已经在很多站点中搜索了答案.他们中的一些人使用作曲家对其进行了描述.我不知道该怎么做.在其他一些站点中,他们编写了代码以循环方式从db获取url.在我的项目数据库中,我没有保存任何网址.我的项目是医生和患者的网站. 所以有谁知道怎么写php / laravel codes for dynamic sitemap generation.?

I want to generate Dynamic sitemap for my laravel project. I had already searched in so many sites for an answer. some of them describes it using composer. I didn't get how to do that. and in some other sites they wrote codes to get urls from db in loops. In my project db I didn't saved any urls. my project is a site for doctor and patients. so is there any one knows how to write php / laravel codes for dynamic sitemap generation.?

修改

我是laravel的新手,所以我只是不熟悉这个作曲家.谁能告诉我我是否从github下载laravel-sitemap-master.zip,可以在其中解压缩并保存在我的项目目录中?如果有人请回答这个问题将非常有帮助.

I'm a newbie to laravel so i'm just unfamiliar with this composer. can anyone please tell me if i download the laravel-sitemap-master.zip from github where i can extract it and saves in my project directory? it will be so much helpful if anyone please answer this.

推荐答案

将此行添加到您的 routes.php

Route::get('/sitemap', function()
{
   return Response::view('sitemap')->header('Content-Type', 'application/xml');
});

创建新文件 app \ Http \ Middleware \ sitemap.php

<?php namespace App\Http\Middleware;

use Closure;
use Carbon\Carbon;
use Illuminate\Contracts\Auth\Guard;

class sitemap {

    /**
     * The Guard implementation.
     *
     * @var Guard
     */
    protected $auth;

    public function __construct(Guard $auth)
    {
        $this->auth = $auth;
    }


    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure                 $next
     *
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if ( !$request->is("sitemap") && $request->fullUrl() != '' && $this->auth->guest() )
        {
            $aSiteMap = \Cache::get('sitemap', []);
            $changefreq = 'always';
            if ( !empty( $aSiteMap[$request->fullUrl()]['added'] ) ) {
                $aDateDiff = Carbon::createFromTimestamp( $aSiteMap[$request->fullUrl()]['added'] )->diff( Carbon::now() );
                if ( $aDateDiff->y > 0 ) {
                    $changefreq = 'yearly';
                } else if ( $aDateDiff->m > 0) {
                    $changefreq = 'monthly';
                } else if ( $aDateDiff->d > 6 ) {
                    $changefreq = 'weekly';
                } else if ( $aDateDiff->d > 0 && $aDateDiff->d < 7 ) {
                    $changefreq = 'daily';
                } else if ( $aDateDiff->h > 0 ) {
                    $changefreq = 'hourly';
                } else {
                    $changefreq = 'always';
                }
            }
            $aSiteMap[$request->fullUrl()] = [
                'added' => time(),
                'lastmod' => Carbon::now()->toIso8601String(),
                'priority' => 1 - substr_count($request->getPathInfo(), '/') / 10,
                'changefreq' => $changefreq
            ];
            \Cache::put('sitemap', $aSiteMap, 2880);
        }
        return $next($request);
    }
}

并创建新的视图文件 resources \ views \ sitemap.blade.php

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"
        xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">
    @foreach( Cache::get('sitemap') as $url => $params )
    <url>
        <loc>{{$url}}</loc>
        <lastmod>{{$params['lastmod']}}</lastmod>
        <changefreq>{{$params['changefreq']}}</changefreq>
        <priority>{{$params['priority']}}</priority>
    </url>
    @endforeach
</urlset>

在文件 app \ Http \ Kernel.php 中向受保护的$ middleware数组添加一个条目

Add an entry to protected $middleware array in the file app\Http\Kernel.php

'sitemap' => 'App\Http\Middleware\sitemap'

这篇关于在不使用composer的情况下,在laravel项目中生成动态站点地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆