# Framework Structure

Serverless-PHP is a focuses on component based development like angular framework, here we call them handlers.&#x20;

Handlers are the classes which implements to the **HandlerInterface**. They must be created inside the ./src/app/ folder. Handlers contain business logic of your app. they must have a public method **handle( )** which takes $\_DATA as an input provided in the router file and returns an object of **Response** class, which can be returned to the API Gateway to respond the user. In the handlers class you can use the **Request** class to access the parameters of the request made by the end user of your application to the API Gateway.

We define all these handlers and routes to access them in the ./src/routes.php. In this file we use Router class to set routes.

Here is a simple example of Handler Class

{% tabs %}
{% tab title="src/routes.php" %}

```php
<?php
/**
* "path" is your request path 
   for ex api.example.com/login -> here /login is your path
   
* "handler" is the relative path of your handler class file 
   inside the app directory, here we don't include .php extension
   For ex your handler class SimpleLogin is inside the dir 
   ./src/app/login/SimpleLogin.php then value would be login/SimpleLogin.php
   Here we should note one Important thing that classname and filename
   of the handler should be same.
   
* "data" array is passed as a parameter to the 
*/
use ServerlessPHP\Router;
Router::setRoutes([
    [
        "path" => "/login",
        "handler" => "login/LoginHandler",
        "data" => [
            "data_index" => "data_value"
        ]
    ],
    [
        "path" => "/logout",
        "handler" => "login/LogoutHandler",
        "data" => [
            "some_another_data" => "other_value"
        ]
    ],
]);
```

{% endtab %}

{% tab title="src/app/login/SimpleLogin.php" %}

```php
<?php

namespace ServerlessPHP\Handler;

use ServerlessPHP\HandlerInterface;
use ServerlessPHP\Request;
use ServerlessPHP\Response;

class SimpleLogin implements HandlerInterface
{
    public function handle(array $_DATA):Response
    {
        $res = new Response();
        $req = new Request();
        
        //returns posted values to the API Gateway - i.e. $_POST
        
        $post_vars = $req->getPost(); 
        
        $username = $post_vars["username"];
        $password = $post_vars["password"];
        
        if($this->loginUser($username, $password)
        {
            $res->setBody(json_encode([
                "login_success" => true
            ]));
            
            $res->setStatusCode(200);
        }
        else
        {
            $res->setBody(json_encode([
                "login_success" => false
            ]));
             
            $res->setStatusCode(403);
        }
        
        return $res;
            
    }
    
    private function loginUser(string $username, string $password)
    {
        // Your application logic
    }

}
?>
```

{% endtab %}
{% endtabs %}

![Serverless-PHP Project Structure](https://661009907-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M5VfNdmUIATyF-w4h6i%2F-M5p_ZW6emdyWJ5zz5yx%2F-M5phXuznKRNR1FnVRgd%2FAnnotation%202020-04-26%20142648.png?alt=media\&token=1e809314-7f89-413e-8c13-61bb1cb672d1)

1. **./index.php** - This file is executed when function is invoked
2. **./main.php** - You can specify global code in this file (It will be executed every time when the function is invoked, you can set global constants here.
3. **./serverless.yml** - Stores deployment configurations for serverless framework.
4. **./php/conf.d/php.ini** - php ini file, here you can specify your php configuration.
5. **./src/routes.php** - Stores your application routes.
6. **./src/app/DemoHandler.php** - Stores handler class for the /demo route specified in ./src/routes.php
7. **./src/classes/Request.php** - Request class which is used to get the request parameters sent by the Api Gateway to the Lambda Function.
8. **./src/classes/Response.php** - Response class for generating response in Api Gateway fromat.
9. &#x20;**./src/classes/Router.php** - Router class for managing routing of the application.
10. **./src/interfaces/HandlerInterface** - Interface for route handler classes.&#x20;

{% hint style="info" %}
This is a simple introduction to project structure, we will learn about every component one by one in next sections.
{% endhint %}
