Welcome Guest, Not a member yet? Register   Sign In
Returning a view within a filter
#1

Hello, 

I have a filter that runs before all requests and performs some various checks in a database prior to executing the request. If a specific condition is met, I want to render a certain view and stop the action of the controller. In the documentation it says "...You can easily do this by returning [b]any non-empty[/b] result"

When I put:

Code:
return view('myviewname');


it doesn't load that view and instead just keeps processing the request through the targeted controller. The only way I've found to get that view to render within the filter is to put:


Code:
echo view('myviewname');
die();


Am I interpreting the documentation incorrectly? Or is this a bug? Shouldn't returning a view stop the action of the requested controller?
Reply
#2

(This post was last modified: 02-15-2022, 08:35 PM by kenjis.)

Return Response object.

(02-15-2022, 05:06 PM)derekairdrie Wrote: Am I interpreting the documentation incorrectly? Or is this a bug? Shouldn't returning a view stop the action of the requested controller?

The documentation may be difficult to understand or misleading.
Returning non-empty result just stops filter executions, not controller execution.

You don't have to use this tricky feature.
Reply
#3

(This post was last modified: 02-16-2022, 01:17 AM by iRedds.)

You can return an instance of the Response class.
PHP Code:
return \Config\Services::response()->setBody(view(....)); 
Filters "after", nor the event will not work.

@kenjis I think that in 5.x it is necessary to lay the ability to process the returned result of the filter as a string as a response.
Reply
#4

@iRedds I would like to replace the current filter with PSR-15 in 5.x, and I don't know it is possible or not that the customization that you say.
Reply
#5

@kenjis To be honest, I don't understand PSR-15 well.
Reply
#6

(02-16-2022, 01:15 AM)iRedds Wrote: You can return an instance of the Response class.
PHP Code:
return \Config\Services::response()->setBody(view(....)); 
Filters "after", nor the event will not work.

@kenjis  I think that in 5.x it is necessary to lay the ability to process the returned result of the filter as a string as a response.

This is exactly what I needed, thanks!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB