This is a SUPER SIMPLE demonstration project that utilizes API communication to provide Google Gemini 2.0 AI-powered** responses to user queries. The project is structured following the Model-View-Controller (MVC) pattern with an emphasis on Object-Oriented Programming (OOP) principles. It uses AltoRouter for routing and a service container for dependency management.
- Controllers: Manages the request logic. The
ChatControllerhandles the form display and processes user input. - Services: Contains the
ApiServiceclass, which is responsible for making API requests using cURL. - Views: Contains the PHP files that render the user interface, such as
chat-form.view.phpandchat-response.view.php. - Core: Contains utility classes like
Validatorfor input validation.
- index(): Displays the question submission form.
- handleChat(Request $request): Handles form submission, validates user input, sends a request to the AI API, and displays the response or errors.
Handles the API communication logic, encapsulating the cURL requests to interact with the AI service.
A utility class that provides methods for validating input data, such as checking string length.
The project uses AltoRouter for handling routes. The main routes include:
GET /- Displays the question submission form.POST /chat- Processes the form submission and returns the AI response.
- Errors are caught using try-catch blocks in the controller.
- Error messages are logged and displayed to the user when appropriate.
- PHP 7.4 or higher
- AltoRouter
- A web server (e.g., Apache, Nginx)
-
Clone the repository:
git clone https://github.com/g-i-kala/php-api-ask-ai.git
-
Navigate to the project directory:
cd ai-question-answer-page -
Install dependencies (if any) using Composer:
composer install
-
Configure your web server to serve the application.
-
Create your own .env file accoridng to the provided example to be able to communicate with the Google Gemini 2.0 AI service. How to Quickstart with Gemini for Developers.
- Start your web server.
- Access the application in your browser at
http://localhost/(or your configured domain). - Enter your question in the form and submit to receive an AI-generated response.
This project is licensed under the MIT License.
Feel free to customize the content further to match your specific project details.