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 di
Suppose you want your website's sitemap.xml
file to include links to all doctors and patients you have in the database, you can do so like this:
in routes.php file..
Route::get("sitemap.xml", array(
"as" => "sitemap",
"uses" => "HomeController@sitemap", // or any other controller you want to use
));
in HomeController.php file (if you decided to use HomeController)..
public function sitemap()
{
$doctors = Doctor::remember(59) // chach this query for 59 minutes
->select(["id", "updated_at"])
// you may want to add where clauses here according to your needs
->orderBy("id", "desc")
->take(50000) // each Sitemap file must have no more than 50,000 URLs and must be no larger than 10MB
->get();
$patients = Patient::remember(59) // chach this query for 59 minutes
->select(["id", "updated_at"])
// you may want to add where clauses here according to your needs
->orderBy("id", "desc")
->take(50000) // each Sitemap file must have no more than 50,000 URLs and must be no larger than 10MB
->get();
$content = View::make('sitemap', ['doctors' => $doctors, 'patients' => $patients]);
return Response::make($content)->header('Content-Type', 'text/xml;charset=utf-8');
}
in views/sitemap.blade.php file..
' ?>
@foreach($doctors as $doctor)
{{ URL::route("doctors.show", [$doctor->id]) }}
{{ gmdate(DateTime::W3C, strtotime($doctor->updated_at)) }}
daily
1.0
@endforeach
@foreach($patients as $patient)
{{ URL::route("patients.show", [$patient->id]) }}
{{ gmdate(DateTime::W3C, strtotime($patient->updated_at)) }}
daily
1.0
@endforeach