Thinkphp5.0 控制器向视图view的赋值


方式一(使用fetch()方法的第二个参数赋值):

<?php
namespace app\index\controller;
use think\Controller; class Index extends Controller
{
public function study_view(){ return $this->fetch('study_view',[
'id' => 1,
'name' => 'li lei',
'age' => 10
]);
}
}

方法二(使用assign()方法赋值):

<?php
namespace app\index\controller;
use think\Controller; class Index extends Controller
{ public function study_view(){
$this->assign('id',1);
$this->assign('name','li lei');
$this->assign('age','12');
return $this->fetch();
}
}

方法三(使用think\Controller类的view对象赋值):

<?php
namespace app\index\controller;
use think\Controller; class Index extends Controller
{ public function study_view(){
$this->view->id = 2;
$this->view->name = 'li lei';
$this->view->age = 15;
return $this->fetch();
}
}

方法四(使用View类的静态方法赋值):

<?php
namespace app\index\controller;
use think\Controller;
use think\View; class Index extends Controller
{ public function study_view(){
View::share('id',6);
View::share('name','zhang san');
View::share('age',20);
return $this->fetch();
}
}

05-25 15:09