Untitled
unknown
php
4 years ago
5.3 kB
3
Indexable
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use PhpOffice\PhpSpreadsheet\Shared\XMLWriter;
use App\Models\Town;
use App\Models\SeoPreset;
use App\Models\Blog;
use App\Models\Page;
use App\Models\Shop;
use App\Models\Brand;
use App\Models\Catalog;
use App\Models\Product;
class SitemapController extends Controller
{
  protected $xmlWriter;
  public function generate(Request $request)
  {
    $root_url = request()->root().'/';
    file_put_contents('sitemap.xml', '');
    $this->xmlWriter = new XMLWriter();
    $this->xmlWriter->openMemory();
    $this->xmlWriter->startDocument('1.0', 'UTF-8');
    $this->xmlWriter->startElement('urlset');
    $this->xmlWriter->writeAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');
    $this->xmlWriter->writeAttribute('xmlns:xhtml', 'http://www.w3.org/1999/xhtml');
      $this->createUrlElement($root_url, date('c'), '0.9');
      $this->createUrlElement($root_url.'ru/', date('c'), '0.9');
      $towns = Town::select('slug')->where('show',1)->get();
      $presets = SeoPreset::select('slug')->where('active',1)->get();
      $catalogs = Catalog::select('slug')->where('inmenu',1)->get(); // TODO insitemap ?
      // Towns main page
      foreach ($towns as $town) {
        $this->createUrlElement($root_url.$town->slug.'/', date('c'), '0.8');
        $this->createUrlElement($root_url.'ru/'.$town->slug.'/', date('c'), '0.8');
      }
      // Blog
      foreach ($blogs = Blog::select('slug','updated_at')->where('show',1)->get() as $blog) {
        $this->createUrlElement($root_url.$blog->slug.'/', $blog->updated_at->format('c'), '0.5');
        $this->createUrlElement($root_url.'ru/'.$blog->slug.'/', $blog->updated_at->format('c'), '0.5');
      }
      // Page
      foreach ($pages = Page::select('slug','updated_at')->where('show',1)->get() as $page) {
        $this->createUrlElement($root_url.$page->slug.'/', $page->updated_at->format('c'), '0.5');
        $this->createUrlElement($root_url.'ru/'.$page->slug.'/', $page->updated_at->format('c'), '0.5');
      }
      // Shop
      foreach ($shops = Shop::select('slug','updated_at')->where('show',1)->get() as $shop) {
        $this->createUrlElement($root_url.'/shop/'.$shop->slug.'/', $shop->updated_at->format('c'), '0.6');
        $this->createUrlElement($root_url.'ru/shop/'.$shop->slug.'/', $shop->updated_at->format('c'), '0.6');
      }
      // Brand
      foreach ($brands = Brand::select('slug','updated_at')->where('show',1)->get() as $brand) {
        $this->createUrlElement($root_url.'/brands/'.$brand->slug.'/', $brand->updated_at->format('c'), '0.6');
        $this->createUrlElement($root_url.'ru/brands/'.$brand->slug.'/', $brand->updated_at->format('c'), '0.6');
      }
      // Towns & Presets
      foreach ($presets as $preset) {
        $prst_url = substr($preset->slug, 1);
        $this->createUrlElement($root_url.$prst_url, date('c'), '0.7');
        $this->createUrlElement($root_url.'ru/'.$prst_url, date('c'), '0.7');
        foreach ($towns as $town) {
          $this->createUrlElement($root_url.$town->slug.'/'.$prst_url, date('c'), '0.7');
          $this->createUrlElement($root_url.'ru/'.$town->slug.'/'.$prst_url, date('c'), '0.7');
        }
      }
      // Towns & Catalogs
      foreach ($catalogs as $catalog) {
        $this->createUrlElement($root_url.$catalog->slug, date('c'), '0.7');
        $this->createUrlElement($root_url.'ru/'.$catalog->slug, date('c'), '0.7');
        foreach ($towns as $town) {
          $this->createUrlElement($root_url.$town->slug.'/'.$catalog->slug, date('c'), '0.7');
          $this->createUrlElement($root_url.'ru/'.$town->slug.'/'.$catalog->slug, date('c'), '0.7');
        }
      }
      // Products
      $parents = Product::select('articul')->whereNull('parent_id')->pluck('articul')->toArray();
      $products = Product::select('slug','updated_at')->whereIn('articul',$parents)->where('parent_id','>',0)->where('show',1)->get();
      foreach ($products as $product) {
        $this->createUrlElement($root_url.$product->slug.'/', $product->updated_at->format('c'), '0.8');
        $this->createUrlElement($root_url.'ru/'.$product->slug.'/', $product->updated_at->format('c'), '0.8');
      }
    $this->xmlWriter->endElement(); // urlset
    // Final flush to make sure we haven't missed anything
    file_put_contents('sitemap.xml', $this->xmlWriter->flush(true), FILE_APPEND);
    return 'OK';
  }
  private function createUrlElement($url, $date, $prority)
  {
    $this->xmlWriter->startElement('url');
      $this->xmlWriter->startElement('loc');
        $this->xmlWriter->text($url);
      $this->xmlWriter->endElement();
      $this->xmlWriter->startElement('lastmod');
        $this->xmlWriter->text($date); // last date
      $this->xmlWriter->endElement();
      $this->xmlWriter->startElement('changefreq');
        $this->xmlWriter->text('daily');
      $this->xmlWriter->endElement();
      $this->xmlWriter->startElement('priority');
        $this->xmlWriter->text($prority);
      $this->xmlWriter->endElement();
    $this->xmlWriter->endElement(); //url
    return true;
  }
}
Editor is loading...