-
Notifications
You must be signed in to change notification settings - Fork 0
MVC
To make the best of our framework, you can use the AbstractController to help you with MVC.
#[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("/");
}
}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.
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.
namespace SimpleFW\HttpBasics;
class HttpResponse
{
private $data;
private $headers;
private $status;
private $redirect;
}This object contains the payload and headers for response.
You can use few functions from AbstractController to create your response.
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.
return $this->response(text);You can send text, that will be showed to the user.
return $this->responseWithJson(myObject);Instance of your object will be automatically casted to JSON and send.
return $this->redirect(path);You can redirect user to some other page.