如果必须将参数从控制器传递给视图编辑器,则可以为编写器创建包装类,并在需要时将数据传递给它.然后,当您完成数据设置后,您可以撰写视图:
ComposerWrapper类
public function __construct(array $data) { $this->data = $data; } public function compose() { $data = $this->data; View::composer('partial_name', function( $view ) use ($data) { //here you can use your $data to compose the view } ); }
调节器
public function index() { //get the data you need $data = ['first_value' = 1]; //pass the data to your wapper class $composerWrapper = new ComposerWrapper( $data ); //this will compose the view $composerWrapper->compose(); //other code... }