Skip to content
Matěj Bucek edited this page Mar 16, 2021 · 2 revisions

MVC

To make the best of our framework, you can use the AbstractController to help you with MVC.

Controller

#[Controller]
class MyController extends AbstractController {
    #[Route("/", methods: ["GET"])]
    public function getIndex(HttpRequest $request): HttpResponse{
        ...
        return $this->render("template.latte", ["params" => "My param :^)"]);
        return $this->response("<h1>Hello, world!</h1>");
        return $this->responseWithJson($myObject);
        return $this->redirect("/");
    }
}

Routes

In this example, you can see that there is a new Attribute called Route.

Route takes the path, methods and name. You don't have to specify the name.

Every Route have to return HttpResponse. You can easily access methods to create one by extending with AbstractController.

HttpRequest

namespace SimpleFW\HttpBasics;

class HttpRequest
{
    private $path;
    private $method;
    private $params;

    public function getPath(){
        return $this->path;
    }
    
    public function getMethod(){
        return $this->method;
    }
    
    public function getParams(){
        return $this->params;
    }
}

This object contains the basic parameters about current request.

HttpResponse

namespace SimpleFW\HttpBasics;

class HttpResponse
{
    private $data;
    private $headers;
    private $status;
    private $redirect;
}

This object contains the payload and headers for response.

Generating HttpResponse

You can use few functions from AbstractController to create your response.

Using template

return $this->render("template.latte", ["params" => "My param :^)"]);

You can render your template using function render(templateName, params) which takes the template name and parameters.

Using simple text response

return $this->response(text);

You can send text, that will be showed to the user.

Using JSON

return $this->responseWithJson(myObject);

Instance of your object will be automatically casted to JSON and send.

Using redirect

return $this->redirect(path);

You can redirect user to some other page.

Clone this wiki locally