# How can I bypass view rendering?

**URL:** <https://discourse.cakephp.org/t/how-can-i-bypass-view-rendering/10244>\
**Category:** Need Help\
**Created:** [March 24, 2022, 6:51pm UTC](https://discourse.cakephp.org/t/how-can-i-bypass-view-rendering/10244 "2022-03-24T18:51:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gcarreno](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/gcarreno/32/1794_2.png) [@gcarreno](https://discourse.cakephp.org/u/gcarreno)\
**Post date:** [March 24, 2022, 6:51pm UTC](https://discourse.cakephp.org/t/how-can-i-bypass-view-rendering/10244/1 "2022-03-24T18:51:20Z")

</div>

Hey there!!

I have to implement this temporary legacy controller that is attached to the route “/packagelist.json”.  
I don’t want to have a route to `/packagelist` with the option for the extension `json`  
This means I want to completely bypass the whole view thing and dump my own JSON directly from the action on the controller.

So my question, or questions are:

1. How do I set the appropriate header for `application/json`. I’m guessing `$this->response->withHeader("Content-type". "application/json")` but I’m not sure
2. How do I then dump a string and then just finish the whole thing to be served immediately?

I’m on `CakePHP 4.1`

Cheers,  
Gus

---

<div class="post-metadata">

**Author:** ![raul338](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/raul338/32/69_2.png) [@raul338](https://discourse.cakephp.org/u/raul338)\
**Post date:** [March 24, 2022, 8:52pm UTC](https://discourse.cakephp.org/t/how-can-i-bypass-view-rendering/10244/2 "2022-03-24T20:52:51Z")

</div>

Aside from the header, you just return the response with the contents you want.

```php
$json = "{.....}";
return $this->response->withStringBody($json);

```

I beleive there is `withType` (i haven’t tried, this is just from memory). You can try

```php
$json = "{.....}";
return $this->response
    ->withType('json')
    ->withStringBody($json);

```

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [March 25, 2022, 9:32am UTC](https://discourse.cakephp.org/t/how-can-i-bypass-view-rendering/10244/3 "2022-03-25T09:32:05Z")

</div>

you can disable the view rendering via calling

```auto
$this->disableAutoRender();

```

---

<div class="post-metadata">

**Author:** ![gcarreno](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/gcarreno/32/1794_2.png) [@gcarreno](https://discourse.cakephp.org/u/gcarreno)\
**Post date:** [March 25, 2022, 5:46pm UTC](https://discourse.cakephp.org/t/how-can-i-bypass-view-rendering/10244/4 "2022-03-25T17:46:24Z")

</div>

Hey Raul,

Thank you so very much for the reply. This looks like what I really wanted done.

In the mean time I kinda solved it another way, cuz you know, many ways of skinning cats and all…

I used the `ajax` layout, and on the `index.php` view I just returned the value of the `json`.

But your solution is more to the point of what I wanted to do.

Cheers,  
Gus

---

<div class="post-metadata">

**Author:** ![gcarreno](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/gcarreno/32/1794_2.png) [@gcarreno](https://discourse.cakephp.org/u/gcarreno)\
**Post date:** [March 25, 2022, 5:48pm UTC](https://discourse.cakephp.org/t/how-can-i-bypass-view-rendering/10244/5 "2022-03-25T17:48:18Z")

</div>

Hey Kevin,

Thank you so much for the input. I’ll keep this in mind!!

Cheers,  
Gus

---

<div class="post-metadata">

**Author:** ![gcarreno](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/gcarreno/32/1794_2.png) [@gcarreno](https://discourse.cakephp.org/u/gcarreno)\
**Post date:** [March 25, 2022, 5:56pm UTC](https://discourse.cakephp.org/t/how-can-i-bypass-view-rendering/10244/6 "2022-03-25T17:56:23Z")

</div>

Hey Raul,

I’ve now tried your suggestion and it works like a charm!!

Again, may thanks for the input, really appreciated!!

Cheers,  
Gus
