Your First Application
Your First Application
This is a simple example of how to create a basic Hazaar application.
Setup the Application
To get started, we first need to set up a new Hazaar application. While there are a few ways to do this, the most common methods are using Composer or by using a Devcontainer.
Please refer to the installation documentation for detailed instructions on how to set up your environment using one of these methods.
Create Your First Route
The simplest way to create a response in Hazaar is by defining a route with a closure. Open the route.php file in your application config directory and add the following:
use Hazaar\Application\Router;
Router::get('/hello', function() {
return 'Hello World!';
});That's it! When you navigate to /hello in your browser, you'll see "Hello World!" displayed. This closure receives the request and returns a simple string response.
Understanding This Example
This is the most basic example of a Hazaar application. The route uses a closure (anonymous function) that returns a plain text string. Hazaar automatically handles the response and sends it to the browser. This approach is perfect for very simple endpoints or quick prototyping.
Using a Controller
As your application grows, you'll want to organize your code into controllers. Controllers are classes that group related functionality together. Let's convert our simple route to use a controller instead.
First, create a new controller in the controllers directory. For example, create a new file called Hello.php with the following content:
<?php
namespace App\Controller;
use Hazaar\Controller\Basic;
class Hello extends Basic {
public function index(): string {
return 'Hello World!';
}
}Next, update your route in the route.php file to use the controller:
use App\Controller\Hello;
use Hazaar\Application\Router;
Router::get('/hello', [Hello::class, 'index']);The result is the same, but now your code is organized in a controller class. This makes it easier to add more methods, share code between endpoints, and maintain your application as it grows.
Using a View
Now let's extend this example to use a view template. First, update the controller to use a view by extending the Action class instead of Basic, and then call the view method to render a template:
<?php
namespace App\Controller;
use Hazaar\Controller\Action;
use Hazaar\Controller\Response\View;
class Hello extends Action {
public function index(): View {
$this->view('hello', [
'name' => 'World',
'message' => 'Welcome to Hazaar!'
]);
}
}Next, create a view template file at views/hello.tpl:
<!DOCTYPE html>
<html>
<head>
<title>Hello Example</title>
</head>
<body>
<h1>Hello {$name}!</h1>
<p>{$message}</p>
</body>
</html>The view receives the data array passed from the controller, making $name and $message available as variables in the template. This approach separates your presentation logic from your business logic, making your application more maintainable.
Next Steps
You now understand how to create both simple text-based responses and template-based views in Hazaar. You can continue building more complex applications by adding more controllers, routes, and views. For more information, see the Hazaar documentation.