Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change behaviour of render method #52

Closed
xepozz opened this issue Feb 9, 2020 · 7 comments
Closed

Change behaviour of render method #52

xepozz opened this issue Feb 9, 2020 · 7 comments
Labels
status:ready for adoption Feel free to implement this issue. type:enhancement Enhancement

Comments

@xepozz
Copy link
Contributor

xepozz commented Feb 9, 2020

What steps will reproduce the problem?

Using render function to render content of view file looks too hard.
Instead of return the result of render function we should to create response before and write data after.
Let's delegate creating the Response object to render function.

What is the expected result?

User controllers will be more simpler and cleaner

public function index(): Response
{
    return $this->render('index');
}

What do you get instead?

public function index(): Response
{
    $output = $this->render('index');
    $response = $this->responseFactory->createResponse();
    $response->getBody()->write($output);
    return $response;
}
@xepozz xepozz changed the title Change behaviour of render object Change behaviour of render method Feb 9, 2020
@samdark
Copy link
Member

samdark commented Feb 12, 2020

View component is not aware of request-response. Its job is rendering templates. I don't think it's a good idea to couple it like suggested.

Still, I agree that it looks a bit cumbersome to create response like that (while it makes perfect sense if redirect or sending headers is involved).

Possible solutions:

  1. Allow returning strings from handlers (actions).
  2. Provide a shortcut method like createResponseWithBody($string).
  3. ???

@xepozz
Copy link
Contributor Author

xepozz commented Feb 12, 2020

If you want to render and collect result to string you can use $this->getView()->render(...);
Controller::render() is only wrapper over $view->render. I do not see any problems in returning Response

Moreover, such a template is familiar to all PHP developers (at least), because all frameworks provide such a convenient interface.

@samdark
Copy link
Member

samdark commented Feb 12, 2020

Returning response directly from render()? Some references would not hurt...

@yiiliveext
Copy link
Contributor

yiiliveext commented Feb 12, 2020

I think @xepozz means something like this

//abstract class Controller
protected function render(string $view, array $parameters = []): string
{
    $response = $this->responseFactory->createResponse();
    $content = $this->view->render($view, $parameters, $this);
    $response->getBody()->write($this->renderContent($content));

    return $response;
}

@samdark
Copy link
Member

samdark commented Feb 12, 2020

Yes but that would be:

protected function render(string $view, array $parameters = []): Response

@yiiliveext
Copy link
Contributor

Yes but that would be:

Of course, I've just forgotten to change after copying.

@samdark
Copy link
Member

samdark commented Feb 12, 2020

Thought more about it. It doesn't sound bad to do it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:ready for adoption Feel free to implement this issue. type:enhancement Enhancement
Projects
None yet
Development

No branches or pull requests

3 participants