问题
How does one pass a parameters to a Presenter? Reading the documentation there doesn't seem to be a way.
https://fuelphp.com/docs/general/presenters.html
Controller
$points = Presenter::forge('points', 'viewmy');
$points->set('id', 5);
Presenter class or view (I tried both):
var_dump($id);
var_dump($this->id);
Both var_dumps generate an undeclared variable error
This in the Presenter class also did not work: $id = $this->get('id');
回答1:
From the page you linked:
In your code, Views and Presenters are interchangeable. You can return Presenters from your controller actions, you can set a Presenter as a Theme partial, or assign it to a section of your page template. The basic API of the Presenter is compatible with the View. This makes it easy to swap a View for a Presenter in your code without having to do a major code overhaul.
They have the exact same interface as regular View objects.
So as per https://fuelphp.com/docs/general/views.html you can do things like using the set()
method or passing the parameters in the forge()
method.
回答2:
I finally got to the bottom of this issue. I was using the setview function in my Presenter to change based on condition. This was meaning that any values that I had set were getting lost.
Passing the view into the 4th parameter of forge and not setting it in the presenter fixed this issue.
来源:https://stackoverflow.com/questions/42964019/pass-parameters-to-presenters-fuelphp