How to generate dynamic XML sitemap in laravel 7
We would like to share with you how to generate dynamic XML sitemap in laravel 7. we will see scratch step by step generate dynamic XML sitemap in laravel 7.
A sitemap is described to navigate the website. users can easily search to navigate the site by sitemap. it is creating in the XML coding.
A sitemap provides information about our site and google can easily read XML files and crawl our site.
We have given below example of how to generate dynamic XML sitemap using laravel. Add the below code in your controller file and Get the data from the database you want to create sitemap URL and pass it in the view file.
So you can follow the below steps.
Overview
Step 1: Install Laravel 7
Step 2: Create Routes
Step 3: Create a Controller
Step 4: Create a Model
Step 5: Create Blade File
Step 6: Run Our Laravel Application
Step 1: Install Laravel 7
We are going to install laravel 7, so first open the command prompt or terminal and go to go to xampp htdocs folder directory using the command prompt. after then run the below command.
1 | composer create-project --prefer-dist laravel/laravel laravel7_sitemap |
Step 2: Create Routes
Add the following route code in the “routes/web.php” file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?php /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('sitemap.xml','SitemapController@index'); Route::get('sitemap.xml/article','SitemapController@articles'); Route::get('sitemap.xml/category','SitemapController@categories'); ?> |
Step 3: Create a Controller
Here in this step, You can use the below command use to create a controller.
1 | php artisan make:controller SitemapController |
app/Http/Controllers/SitemapController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Post; use App\Category; class SitemapController extends Controller { public function index() { $articles = Post::all()->first(); $categories = Category::all()->first(); return response()->view('sitemap.index', [ 'article' => $articles, 'category' => $categories ])->header('Content-Type', 'text/xml'); } public function articles() { $article = Post::latest()->get(); return response()->view('sitemap.article', [ 'article' => $article, ])->header('Content-Type', 'text/xml'); } public function categories() { $category = Category::all(); return response()->view('sitemap.category', [ 'category' => $category, ])->header('Content-Type', 'text/xml'); } } ?> |
Step 4: Create Blade File
Finally, We will create an index.blade.php, article.blade.php, and category.blade.php files in the “resources/views/sitemap/” folder directory and paste the below code.
resources/views/index.blade.php
1 2 3 4 5 6 7 8 9 10 | <?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?> <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <sitemap> <loc>http://127.0.0.1:8000/sitemap.xml/article</loc> </sitemap> <sitemap> <loc>http://127.0.0.1:8000/sitemap.xml/category</loc> </sitemap> </sitemapindex> |
resources/views/article.blade.php
1 2 3 4 5 6 7 8 9 10 11 | <?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> @foreach ($article as $content) <url> <loc>{{ url('/') }}article/{{ $content->slug }}</loc> <lastmod>{{ $content->created_at->tz('UTC')->toAtomString() }}</lastmod> <changefreq>weekly</changefreq> <priority>0.9</priority> </url> @endforeach </urlset> |
resources/views/category.blade.php
1 2 3 4 5 6 7 8 9 10 11 | <?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> @foreach ($category as $category) <url> <loc>{{ url('/') }}article/category/{{ $category ->slug }}</loc> <lastmod>{{ $content->created_at->tz('UTC')->toAtomString() }}</lastmod> <changefreq>weekly</changefreq> <priority>0.9</priority> </url> @endforeach </urlset> |
Step 5: Run Our Laravel Application
We can start the server and run this example using the below command.
1 | php artisan serve |
Now we will run our example using the below Url in the browser.
1 2 3 | http://127.0.0.1:8000/sitemap.xml http://127.0.0.1:8000/sitemap.xml/article http://127.0.0.1:8000/sitemap.xml/category |