This is a basic book catalogue project built with Laravel. It fetches book data from the Gutendex API and displays it on the /books URL. The project demonstrates the use of Laravel's Blade templating, controller logic, and separation of API client logic into a dedicated file.
- Fetches book data from the Gutendex API.
- Displays books with details like title, author, language, subjects, and summaries.
- Allows filtering by language, sorting (ascending, descending, popular), and searching by title.
- Pagination for navigating through the book list.
- Responsive UI built with Bootstrap 5.
- Demonstrates Laravel best practices:
- Blade templating.
- Separation of concerns by moving API logic to a dedicated client file.
- Clean controller logic.
Follow these steps to set up the project locally:
- PHP >= 8.1
- Composer
- Laravel >= 10.x
- A web server (e.g., Apache, Nginx, or Laravel's built-in server)
- Node.js and npm (optional, for frontend asset compilation)
-
Clone the Repository:
git clone https://github.com/gayali/laravel-book-catalogue.git cd laravel-book-catalogue -
Install Dependencies: Run the following command to install PHP dependencies:
composer install
-
Set Up Environment: Copy the .env.example file to .env:
cp .env.example .env
Update the .env file with your application settings (e.g., database connection, app URL).
-
Generate Application Key:
php artisan key:generate
-
Run the Application: Start the Laravel development server:
php artisan serve
The application will be available at http://127.0.0.1:8000
- Navigate to
/booksto view the book catalogue. - Example URL:
http://127.0.0.1:8000/books.
-
Search:
- Use the search bar to find books by title.
-
Filter by Language:
- Click the "Filters" button to open the left panel.
- Select one or more languages (e.g., English, French) and click "Apply Filter".
-
Sort:
- Use the "Sort By" dropdown to sort books by:
- Ascending
- Descending
- Popular
- Use the "Sort By" dropdown to sort books by:
-
Pagination:
- Use the "Previous Page" and "Next Page" buttons to navigate through the book list.
-
View Details:
- Click the "View Details" button on any book card to open a modal with detailed information about the book.
The routes are defined in routes/web.php:
Route::get('/', function () {
return redirect()->route('books.index');
});
Route::get('/books', [BookController::class, 'index'])->name('books.index');The main logic is handled in BookController.php:
// filepath: app/Http/Controllers/BookController.php
public function index(Request $request)
{
$validated = $request->validate([
'search' => 'nullable|string|max:255',
'page' => 'nullable|string|min:1',
'languages' => 'nullable|array',
'languages.*' => 'string|size:2|regex:/^[a-z]{2}$/',
'sort' => 'nullable|string|in:ascending,descending,popular',
]);
try {
if (isset($validated['languages']) && is_array($validated['languages'])) {
$validated['languages'] = implode(',', $validated['languages']);
}
$books = (new BookClient)->fetchBooks($validated);
if (!$books) {
Log::error('Error fetching books: Books not found');
return response(['error' => 'Books not found'], 404);
}
return view('books.index', compact('books'));
} catch (Exception $e) {
Log::error($e);
return response(['error' => 'Something went wrong, please try again later'], 500);
}
}The API logic is separated into BookClient.php:
// filepath: app/Clients/BookClient.php
namespace App\Clients;
use Illuminate\Support\Facades\Http;
class BookClient
{
const SITE_URL = 'https://gutendex.com';
public function fetchBooks($query = [])
{
$query = array_filter($query, function ($value) {
return !is_null($value) && $value !== '';
});
$response = Http::get(self::SITE_URL . '/books', $query);
if ($response->successful()) {
return $response->json();
}
return null;
}
}- Purpose: Handles API requests to the Gutendex API.
- Method:
fetchBooks($query)fetches books based on the query parameters.
The frontend is built using Blade templates. Key files:
-
Layout:
resources/views/layouts/app.blade.php: Contains the base HTML structure and includes Bootstrap.
-
Books Index:
resources/views/books/index.blade.php: Displays the book catalogue with filters, sorting, and pagination.
The UI is built with Bootstrap 5 (via CDN). Key features:
-
Navbar:
- Displays the application title.
-
Filters:
- A left panel (offcanvas) for filtering by language.
-
Sorting:
- A dropdown for sorting books.
-
Pagination:
- Buttons for navigating between pages.
-
Responsive Design:
- The layout is fully responsive and works on all screen sizes.
The application sends requests to the Gutendex API with query parameters like:
GET https://gutendex.com/books?search=frankenstein&languages=en,fr&sort=popular&page=2-
Add More Filters:
- Update the
BookControllerandindex.blade.phpto include additional filters (e.g., subjects, authors).
- Update the
-
Styling:
- Modify
app.blade.phpto include custom CSS or use a different frontend framework.
- Modify
-
Extend API Logic:
- Add more methods to
BookClient.phpfor fetching specific book details or other endpoints.
- Add more methods to
-
No Books Found:
- Ensure the Gutendex API is reachable.
- Check the query parameters being sent.
-
Styling Issues:
- Ensure the Bootstrap CDN is loaded correctly.
-
Validation Errors:
- Check the validation rules in
BookController.php.
- Check the validation rules in
This project is open-sourced software licensed under the MIT license.