8000 Add sitemap generation functionality · oriolmiro/BookPriceTracker@1f2d956 · GitHub
[go: up one dir, main page]

Skip to content

Commit

Permalink
Add sitemap generation functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
oriolmiro committed Mar 17, 2024
1 parent 4a896a3 commit 1f2d956
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
10000
Original file line numberDiff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ yarn-error.log
/.idea
/.vscode
database/data/books.json
public/sitemap.xml
23 changes: 23 additions & 0 deletions app/Http/Controllers/BookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,27 @@ public function destroy(Book $book)
$book->delete();
return redirect()->route('books.index');
}

/**
* Create a sitemap for the books in file storage.
*/
public function createSitemap(){
$books = Book::all();

// Iniciar el document XML del sitemap
$sitemap = new \SimpleXMLElement('<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"></urlset>');

// Afegir cada llibre al sitemap
foreach ($books as $book) {
$url = $sitemap->addChild('url');
$url->addChild('loc', htmlspecialchars($book->route()));
$url->addChild('lastmod', $book->updated_at->toAtomString());
$url->addChild('changefreq', 'monthly');
$url->addChild('priority', '0.8');
}

// Guardar el sitemap a l'arrel del domini
$sitemap->asXML(public_path('sitemap.xml'));

}
}
2 changes: 1 addition & 1 deletion app/Models/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function alerts()
}
public function route()
{
return route('books.show', array('title' => $this->title, 'author' => $this->author, 'id' => $this->id));
return route('books.show', array('title' => $this->title, 'author' => $this->author === '' ? 'none' : $this->author, 'id' => $this->id));
}

}
1 change: 1 addition & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@
Route::get('/bookStores', [BookStoreController::class, 'APIindex']);
Route::get('/bookPriceUpdate/', [BookPriceUpdateController::class, 'takeISBM']);
Route::post('/bookPriceUpdate/', [BookPriceUpdateController::class,'updatePrice']);
Route::get('/createSitemap', [BookController::class, 'createSitemap']);

0 comments on commit 1f2d956

Please sign in to comment.
0