🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
* 配置 视图view 依赖于smarty,需要composer安装smarty,原则上swoolefy一般不作为视图渲染的,定义为一个api框架 ``` composer require smarty/smarty ``` 在应用层配置文件中 ~~~ 'components' => [ // 第一种方式利用配置项设置 'view' => [ 'class' => 'Swoolefy\Core\View', ], // 第二种动态创建,更加灵活,推荐 'view' => function($com_name) { $view = new Swoolefy\Core\View(); return $view; } ] ~~~ * 获取实例 在应用中 ~~~ $view = Application::getApp()->view; ~~~ 我们在控制器中使用 ~~~ $this->assign('name', $name); $this->assign('books', $books); $this->display('test.html'); ~~~ 其实就是通过view组件实例进行封装的,例如在`Swoolefy\Core\AppTrait.php`中 ~~~ /** * assign * @param string $name * @param string|array $value * @return void */ public function assign($name,$value) { Application::getApp()->view->assign($name,$value); } /** * display * @param string $template_file * @return void */ public function display($template_file=null) { Application::getApp()->view->display($template_file); } /** * fetch * @param string $template_file * @return void */ public function fetch($template_file=null) { Application::getApp()->view->display($template_file); } /** * returnJson * @param array $data * @param string $formater * @return void */ public function returnJson($data,$formater = 'json') { Application::getApp()->view->returnJson($data,$formater); } ~~~