我目前正在学习创建Laravel PHP控制器、视图和;路线。这是我的代码:
Authors2.php(控制器)
class Authors2_Controller extends Base_Controller {
public function contact() {
$view = View::make('authors2.index', array('name'=>'Andrew Perkins'))
->with('age', '28');
$view->location = 'California';
$view['specialty'] = 'PHP';
return $view;
}
}
index.php(视图)
<h1>Authors2 Home Page </h1>
<?php echo $name; ?><br />
<?php echo $age; ?><br />
<?php echo $location; ?><br />
<?php echo $specialty; ?><br />
routes.php(路由)
Route::get('authors2', array('uses' =>'Authors2_Controller@contact'));
电流输出
Authors2 Home Page
所需输出
Authors2 Home Page
Andrew Perkins
28
California
PHP
请帮助我,这样我就可以理解为什么php数据没有正确显示在标题下。非常感谢。
您必须向视图传递数据,如下所示:
$view = View::make('authors2.index', array('name'=>'Andrew Perkins'))
->with('age', '28')
->with('location', 'California')
->with('specialty', 'PHP);
或者像这样:
return View::make('authors2.index', array(
'name' => 'Andrew Perkins',
'age' => 28,
'location' => 'California',
'specialty' => 'PHP'
));
或者像这样:
$name = 'Andrew Perkins';
$age = 28;
$location = 'California';
$specialty = 'PHP';
return View::make('authors2.index', compact('name', 'age', 'location', 'specialty'));
或者,使用与上述相同的变量:
return View::make('authors2.index')->with(compact('name', 'age', 'location', 'specialty'));
或者,你也可以这样做:
$view = View::make('authors2.index');
$name = 'Andrew Perkins';
$view->with(compact('name'));
$view->with('age', 28);
return $view;