John Davidson

PHP MVC pass data in response object to body(view)?

0 comments
Message:


I am using php + blade and wrote simple mvc and now I am wondering how to pass data to view.
My response object contains header, status code and body(view). Is it ok to pass data to response body section next to view?

->setBody(['view'=>'result','somedata'=>$somedata]);


or response object should have statuscode, header, body(view), and data separately from body(view)?


now in controller i have:


return Response::create()
->setStatusCode(200)
->setHeader(['Content-Type' => 'text/html'])
->setBody(['view'=>'result']);

my response class:


class response implements ResponseInterface
{
protected $statusCode;
protected $headers;
protected $body=[];

public function setStatusCode(string $code) :object
{
$this->statusCode = $code;
return $this;
}

public function setHeader(array $header):object
{
$this->headers=$header;
return $this;
}

public function setBody(array $body): object
{
$this->body = $body;
return $this;
}

public function getStatusCode():string
{
return $this->statusCode;
}
public function getHeaders():array
{
return $this->headers;
}
public function getBody():array
{
return $this->body;
}
public static function create(string $statusCode = null, array $headers = null, string $body = null): response
{
return new Response($statusCode, $headers, $body);
}
public function send(): void
{
foreach ($this->headers as $header => $value) {
header(strtoupper($header).': '.$value);
}

if ($this->headers['Content-Type']==="text/html" && $this->statusCode === "200") {
View::renderTemplate($this->body['view']);
}


}
}

i am able to pass data like this, by adding fx datatable variable in controllers and it works:


 return Response::create()
->setStatusCode(200)
->setHeader(['Content-Type' => 'text/html'])
->setBody(['view'=>'result','datatable'=>$datatable]);

and then need to adjust response send action to:


public function send(): void
{
foreach ($this->headers as $header => $value) {
header(strtoupper($header).': '.$value);
}

if ($this->headers['Content-Type']==="text/html" && $this->statusCode === "200") {
View::renderTemplate($this->body['view'],'datatable'=>$this->body['datatable']);
}

}



My Answer:
Yes, it is common practice to pass data to the view by including it in the response body section. In PHP MVC, you can pass data to the view by setting variables in the controller and then accessing those variables in the view file.

For example, in your controller, you can set a variable like this:

php
$data = [
'name' => 'John Doe',
'age' => 30
];

$response->setBody('view.php', $data);


And in your view file (view.php), you can access the data like this:

php

Hello,


You are years old




This way, you can pass data from the controller to the view and display it in the response body section.

Rate this post

5 of 5 based on 5982 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us