2014-01-08 58 views
0

我难以显示我创建的视图。我正在学习laravel,并且我发现了这篇有用的文章,介绍如何创建一个简单的CRUD。如何在Laravel中显示控制器生成的视图?

http://scotch.io/tutorials/simple-laravel-crud-with-resource-controllers

但我被困在显示使用路由的第一个布局。这是我的代码到目前为止。

routes.php文件

<?php 

/* 
|-------------------------------------------------------------------------- 
| Application Routes 
|-------------------------------------------------------------------------- 
| 
| Here is where you can register all of the routes for an application. 
| It's a breeze. Simply tell Laravel the URIs it should respond to 
| and give it the Closure to execute when that URI is requested. 
| 
*/ 

Route::get('/', function() 
{ 
    return View::make('hello'); 
}); 

Route::resource('nerd','NerdController'); 

Nerd.php(模型)

<?php 

    class Nerd extends Eloquent 
    { 





    } 

?> 

NerdController.php(控制器)

<?php 

class NerdController extends \BaseController { 

    /** 
    * Display a listing of the resource. 
    * 
    * @return Response 
    */ 
    public function index() 
    { 

     //get all nerds 
     $nerds = Nerd::all(); 

     //load the view 
     return View::make('nerds.index')- 
      >with('nerds', $nerds); 

    } 

    /** 
    * Show the form for creating a new resource. 
    * 
    * @return Response 
    */ 
    public function create() 
    { 
     return View::make('nerds.create'); 
    } 

    /** 
    * Store a newly created resource in storage. 
    * 
    * @return Response 
    */ 
    public function store() 
    { 
     // 
    } 

    /** 
    * Display the specified resource. 
    * 
    * @param int $id 
    * @return Response 
    */ 
    public function show($id) 
    { 
     // 
    } 

    /** 
    * Show the form for editing the specified resource. 
    * 
    * @param int $id 
    * @return Response 
    */ 
    public function edit($id) 
    { 
     // 
    } 

    /** 
    * Update the specified resource in storage. 
    * 
    * @param int $id 
    * @return Response 
    */ 
    public function update($id) 
    { 
     // 
    } 

    /** 
    * Remove the specified resource from storage. 
    * 
    * @param int $id 
    * @return Response 
    */ 
    public function destroy($id) 
    { 
     // 
    } 

} 

数据库设置

'mysql' => array(
      'driver' => 'mysql', 
      'host'  => 'localhost', 
      'database' => 'laravel_forums', 
      'username' => 'root', 
      'password' => '', 
      'charset' => 'utf8', 
      'collation' => 'utf8_unicode_ci', 
      'prefix' => '', 
     ), 

index.blade.php(图)

<!DOCTYPE html> 
<html> 
<head> 
    <title>Look! I'm CRUDding</title> 
    <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css"> 
</head> 
<body> 
<div class="container"> 

<nav class="navbar navbar-inverse"> 
    <div class="navbar-header"> 
     <a class="navbar-brand" href="{{ URL::to('nerds') }}">Nerd Alert</a> 
    </div> 
    <ul class="nav navbar-nav"> 
     <li><a href="{{ URL::to('nerds') }}">View All Nerds</a></li> 
     <li><a href="{{ URL::to('nerds/create') }}">Create a Nerd</a> 
    </ul> 
</nav> 

<h1>All the Nerds</h1> 

<!-- will be used to show any messages --> 
@if (Session::has('message')) 
    <div class="alert alert-info">{{ Session::get('message') }}</div> 
@endif 

<table class="table table-striped table-bordered"> 
    <thead> 
     <tr> 
      <td>ID</td> 
      <td>Name</td> 
      <td>Email</td> 
      <td>Nerd Level</td> 
      <td>Actions</td> 
     </tr> 
    </thead> 
    <tbody> 
    @foreach($nerds as $key => $value) 
     <tr> 
      <td>{{ $value->id }}</td> 
      <td>{{ $value->name }}</td> 
      <td>{{ $value->email }}</td> 
      <td>{{ $value->nerd_level }}</td> 

      <!-- we will also add show, edit, and delete buttons --> 
      <td> 

       <!-- delete the nerd (uses the destroy method DESTROY /nerds/{id} --> 
       <!-- we will add this later since its a little more complicated than the other two buttons --> 

       <!-- show the nerd (uses the show method found at GET /nerds/{id} --> 
       <a class="btn btn-small btn-success" href="{{ URL::to('nerds/' . $value->id) }}">Show this Nerd</a> 

       <!-- edit this nerd (uses the edit method found at GET /nerds/{id}/edit --> 
       <a class="btn btn-small btn-info" href="{{ URL::to('nerds/' . $value->id . '/edit') }}">Edit this Nerd</a> 

      </td> 
     </tr> 
    @endforeach 
    </tbody> 
</table> 

</div> 
</body> 
</html> 

当我尝试使用此路径

http://mylocalhost/testsite/nerds 

查看索引
Not Found 

The requested URL /testsite/nerds was not found on this server. 

这就是所有人。我希望你能帮助我。

回答

2

假设应用根URL是http://website.loc/testsite/解决办法是:

一个: eighter访问此URL通网络浏览器
http://mylocalhost/testsite/nerd

B:

// or change this 
Route::resource('nerd', ...); 
// to this 
Route::resource('nerds', ...) 

你只是告诉了Laravel在URL中捕获/nerd,而是访问/nerds。我相信你现在得到它:)

+0

谢谢你的解决方案先生。我会尝试一下,希望它能起作用。以便我可以再次开始研究它。 – Jerielle

相关问题