<small id='6iX0A'></small><noframes id='6iX0A'>

<i id='6iX0A'><tr id='6iX0A'><dt id='6iX0A'><q id='6iX0A'><span id='6iX0A'><b id='6iX0A'><form id='6iX0A'><ins id='6iX0A'></ins><ul id='6iX0A'></ul><sub id='6iX0A'></sub></form><legend id='6iX0A'></legend><bdo id='6iX0A'><pre id='6iX0A'><center id='6iX0A'></center></pre></bdo></b><th id='6iX0A'></th></span></q></dt></tr></i><div id='6iX0A'><tfoot id='6iX0A'></tfoot><dl id='6iX0A'><fieldset id='6iX0A'></fieldset></dl></div>
    <bdo id='6iX0A'></bdo><ul id='6iX0A'></ul>

  • <tfoot id='6iX0A'></tfoot>
    1. <legend id='6iX0A'><style id='6iX0A'><dir id='6iX0A'><q id='6iX0A'></q></dir></style></legend>

      1. 在 laravel 项目中生成动态站点地图而不使用作曲家

        时间:2023-10-31

            <tbody id='07p1b'></tbody>

                <bdo id='07p1b'></bdo><ul id='07p1b'></ul>
              • <i id='07p1b'><tr id='07p1b'><dt id='07p1b'><q id='07p1b'><span id='07p1b'><b id='07p1b'><form id='07p1b'><ins id='07p1b'></ins><ul id='07p1b'></ul><sub id='07p1b'></sub></form><legend id='07p1b'></legend><bdo id='07p1b'><pre id='07p1b'><center id='07p1b'></center></pre></bdo></b><th id='07p1b'></th></span></q></dt></tr></i><div id='07p1b'><tfoot id='07p1b'></tfoot><dl id='07p1b'><fieldset id='07p1b'></fieldset></dl></div>

                <legend id='07p1b'><style id='07p1b'><dir id='07p1b'><q id='07p1b'></q></dir></style></legend>

                • <tfoot id='07p1b'></tfoot>

                  <small id='07p1b'></small><noframes id='07p1b'>

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

                  问题描述

                  我想为我的 laravel 项目生成 Dynamic sitemap.我已经在很多网站上搜索过答案.他们中的一些人使用作曲家来描述它.我不知道该怎么做.在其他一些站点中,他们编写代码以循环从 db 获取 url.在我的项目数据库中,我没有保存任何网址.我的项目是一个医生和病人的网站.那么有没有人知道如何编写 php/laravel 代码来生成动态站点地图.?

                  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');
                  });
                  

                  创建新文件appHttpMiddlewaresitemap.php

                  <?php namespace AppHttpMiddleware;
                  
                  use Closure;
                  use CarbonCarbon;
                  use IlluminateContractsAuthGuard;
                  
                  class sitemap {
                  
                      /**
                       * The Guard implementation.
                       *
                       * @var Guard
                       */
                      protected $auth;
                  
                      public function __construct(Guard $auth)
                      {
                          $this->auth = $auth;
                      }
                  
                  
                      /**
                       * Handle an incoming request.
                       *
                       * @param  IlluminateHttpRequest $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);
                      }
                  }
                  

                  并创建新的视图文件resourcesviewssitemap.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>
                  

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

                  Add an entry to protected $middleware array in the file appHttpKernel.php

                  'sitemap' => 'AppHttpMiddlewaresitemap'
                  

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

                  上一篇:如何让 Laravel 返回视图的“Content-Type"?标头为“应用程序/javascript&quo 下一篇:使用 laravel 验证检查名称在未删除项目中是否唯一

                  相关文章

                  1. <legend id='B2Vhu'><style id='B2Vhu'><dir id='B2Vhu'><q id='B2Vhu'></q></dir></style></legend>
                      <bdo id='B2Vhu'></bdo><ul id='B2Vhu'></ul>
                  2. <tfoot id='B2Vhu'></tfoot>

                    <small id='B2Vhu'></small><noframes id='B2Vhu'>

                    <i id='B2Vhu'><tr id='B2Vhu'><dt id='B2Vhu'><q id='B2Vhu'><span id='B2Vhu'><b id='B2Vhu'><form id='B2Vhu'><ins id='B2Vhu'></ins><ul id='B2Vhu'></ul><sub id='B2Vhu'></sub></form><legend id='B2Vhu'></legend><bdo id='B2Vhu'><pre id='B2Vhu'><center id='B2Vhu'></center></pre></bdo></b><th id='B2Vhu'></th></span></q></dt></tr></i><div id='B2Vhu'><tfoot id='B2Vhu'></tfoot><dl id='B2Vhu'><fieldset id='B2Vhu'></fieldset></dl></div>