【发布时间】:2014-01-25 14:08:04
【问题描述】:
我知道我的代码有点错误(因此我的帖子!)。我希望在访问者访问网站的“/”部分时显示我的“主页”视图。
目前,当用户访问网站的“/home”部分时,视图会起作用。我目前正在为如何做到这一点而烦恼!
Route.php:
Route::controller('/', 'HomeController');
Route::controller('users', 'UsersController');
Route::get('events/{id}/{slug}', 'EventsController@show');
Route::controller('events', 'EventsController');
HomeController.php:
<?php
class HomeController extends BaseController {
protected $layout = "layouts.main";
public function getHome(){
$events = myApp\Event::where('date','>=', DB::raw('CURDATE()'))->first();
$this->layout->content = View::make('home', array('events' => $events));
}
}
Home.blade.php:
<div class="col-md-4">
<h2>Next Event</h2>
<h3>{{$events->title}}</h3>
<p>Presented by {{ $events->consultant()->first()->title }} {{ $events->consultant()->first()->surname }}</p>
<b><p>{{ date("j F Y", strtotime($events->date)) }} from {{ date("g:ia", strtotime($events->start_time)) }}</p></b>
<a class="btn btn-success" href="{{ URL::to('events/' . $events->slug) }}">Book your place now.</a>
</div>
通过在我的 routes.php 中使用它,我设法让视图与“/”目录一起工作:
Route::get('/', function(){
return View::make('home');
});
但是,我遇到了错误:
未定义变量:事件(视图:/Users/Sites/gp/app/views/home.blade.php)。
就好像,HomeController 没有将“事件”数组传递到视图中,只是通过更改路由?!任何帮助/补救措施/解释将不胜感激。
【问题讨论】: