LARAVEL

Laravel-də avtomatik olaraq sitemap necə yaratmaq olar?

14.04.2021 3 dəq oxuma
  1.  Birinci web.php -də route yaradırıq:
    // Sitemap.xml
    Route::get('sitemap-generate',[sitemapController::class,'index'])
        ->name('sitemap.generate');
  2. php artisan make:controller sitemapController əmrini çalışdırırıq 
    <?php
    
    namespace App\Http\Controllers;
    
    use App\Models\AlternativeSlug;
    use App\Models\Blog;
    use App\Models\Category;
    use Illuminate\Support\Facades\File;
    
    class sitemapController extends Controller
    {
        public function index()
        {
            // All blogs
            $blogs = Blog::pluck('slug')->toArray();
    
            $blog_slug = array_map(function ($slug) {
                return '
            <url>
                <loc>'.route('front.single.blog',$slug).'</loc>
                <lastmod>'.date('Y-m-d').'</lastmod>
                <changefreq>weekly</changefreq>
                <priority>0.9</priority>
            </url>
            ';
            }, $blogs);
    
            // All Alternative slugs
            $alternative_slug = AlternativeSlug::pluck('slug')->toArray();
    
            $alternative_slug = array_map(function ($slug) {
                return '
            <url>
                <loc>'.route('front.single.blog',$slug).'</loc>
                <lastmod>'.date('Y-m-d').'</lastmod>
                <changefreq>weekly</changefreq>
                <priority>0.9</priority>
            </url>
            ';
            }, $alternative_slug);
    
            // All Categories
            $categories_slug = Category::pluck('slug')->toArray();
    
            $categories_slug = array_map(function ($slug) {
                return '
            <url>
                <loc>'.route('front.blog.category',$slug).'</loc>
                <lastmod>'.date('Y-m-d').'</lastmod>
                <changefreq>weekly</changefreq>
                <priority>0.9</priority>
            </url>
            ';
            }, $categories_slug);
    
            $data = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
    
            // Front homepage
            $data .= '
            <url>
                <loc>'.route('front.index').'</loc>
                <lastmod>'.date('Y-m-d').'</lastmod>
                <changefreq>weekly</changefreq>
                <priority>0.9</priority>
            </url>
            ';
    
            // Login view
            $data .= '
            <url>
                <loc>'.route('login').'</loc>
                <lastmod>'.date('Y-m-d').'</lastmod>
                <changefreq>weekly</changefreq>
                <priority>0.9</priority>
            </url>
            ';
    
            // Logout
            $data .= '
            <url>
                <loc>'.route('logout').'</loc>
                <lastmod>'.date('Y-m-d').'</lastmod>
                <changefreq>weekly</changefreq>
                <priority>0.9</priority>
            </url>
            ';
    
            // Front About us
            $data .= '
            <url>
                <loc>'.route('front.about').'</loc>
                <lastmod>'.date('Y-m-d').'</lastmod>
                <changefreq>weekly</changefreq>
                <priority>0.9</priority>
            </url>
            ';
    
            // Contact
            $data .= '
            <url>
                <loc>'.route('front.contact').'</loc>
                <lastmod>'.date('Y-m-d').'</lastmod>
                <changefreq>weekly</changefreq>
                <priority>0.9</priority>
            </url>
            ';
    
            // Front Blog
            $data .= '
            <url>
                <loc>'.route('front.blogs').'</loc>
                <lastmod>'.date('Y-m-d').'</lastmod>
                <changefreq>weekly</changefreq>
                <priority>0.9</priority>
            </url>
            ';
    
            // Front Single Blog
            $data .= implode(' ',$blog_slug);
    
            $data .= implode(' ',$alternative_slug);
    
            $data .= implode(' ',$categories_slug);
    
            $data .= '</urlset>';
            File::put(public_path('sitemap.xml'), $data);
    
            return redirect(config('app.url').'/sitemap.xml');
        }
    }
    

  3.  
Digər dildə: EN