## 视图 现在我们在给控制器添加视图文件功能,我们在`application/index`目录下面创建一个`view`目录,然后添加模板文件`view/index/hello.html`(注意大小写),我们添加模板内容如下: ~~~ <html> <head> <title>hello {$name}</title> </head> <body> hello, {$name}! </body> </html> ~~~ 要输出视图,必须在控制器方法中进行模板渲染输出操作,现在修改控制器类如下: ~~~ <?php namespace app\index\controller; use think\Controller; class Index extends Controller { public function hello($name = 'thinkphp') { $this->assign('name', $name); return $this->fetch(); } } ~~~ >[danger] ### [ 新手须知 ] > * * * * * > 这里使用了`use`来导入一个命名空间的类库,然后可以在当前文件中直接使用该别名而不需要使用完整的命名空间路径访问类库。也就说,如果没有使用 > ~~~ > use think\Controller; > ~~~ > 就必须使用 > ~~~ > class Index extends \think\Controller > ~~~ > 这种完整命名空间方式。 > > 在后面的内容中,如果我们直接调用系统的某个类的话,都会假设已经在类的开头使用`use`进行了别名导入。 注意,`Index`控制器类继承了 `think\Controller`类之后,我们可以直接使用封装好的`assign`和`fetch`方法进行模板变量赋值和渲染输出。 `fetch`方法中我们没有指定任何模板,所以按照系统默认的规则(**视图目录/控制器/操作方法**)输出了`view/index/hello.html`模板文件。 接下来,我们在浏览器访问 ~~~ http://tp5.com/index.php/index/index/hello ~~~ 输出: `hello,thinkphp!`