我的路线:
Route::controller('admin' , 'CategoriesController');
控制器:
<?php
class CategoriesController extends BaseController {
/**
* Display a listing of the resource.
* GET /categories
*
* @return Response
*/
public function __construct() {
$this->beforeFilter('csrf', array('on' =>'post' ));
}
public function getIndex()
{
return View::make('categories.index')
->with('categories', Category::all());
}
/**
* Show the form for creating a new resource.
* GET /categories/create
*
* @return Response
*/
public function postCreate()
{
$validator = Validator::make(Input::all(), Category::$rules);
if ($validator->passes()) {
$category = new Category;
$category->name = Input::get('name');
$category->save();
return Redirect::to('admin/categories/index')
->with('message' , 'Category created');
}
return Redirect::to('admin/categories/index')
->with('message' , 'Something went wrong')
->withErrors($validator)
->withInput();
}
/**
* Remove the specified resource from storage.
* DELETE /categories/{id}
*
* @param int $id
* @return Response
*/
public function postDestroy()
{
$category = Category::find(Input::get('id'));
if ($category){
$category->delete();
return Redirect::to('admin/categories/index')
->with('message' , 'category deleted');
}
return Redirect::to('admin/categories/index')
->with('message' , 'something went wronng');
}
}
型号:
<?php
class Category extends \Eloquent {
protected $fillable = array('name');
public static $rules = array('name'=>'required|min:3');
}
view / index.blade:
@section('content')
<div id="admin">
<h1>Categories Admin panel</h1>
<p>Here you can view, delete, and create new categories.</p>
<h2>Categories</h2>
<ul>
@foreach($categories as $category)
<li>
{{ $category->name }}
{{ Form::open(array('url'=>'admin/categories/destroy', 'class' => 'form-inline'))}}
{{ Form::hidden('id' , $category->id) }}
{{ Form::submit('delete')}}
{{ Form::close();}}
</li>
@endforeach
</ul>
<h2>Create New Category</h2>
@if($errors->has)
<div id="form-errors">
<p>The following erros</p>
<ul>
@foreach($errors->all() as $error)
<li>{{ $errors }}</li>
@endforeach
</ul>
</div><!--end form-errors-->
@endif
{{ Form::open(array('url'=>'admin/categories/create'))}}
<p>
{{ Form::label('name')}}
{{ Form::text('name')}}
</p>
{{ Form::submit('Create Category', array('class' => 'secodary-cart-btn'))}}
{{ Form::close()}}
</div><!--end admin-->
@stop
问题是:当我在浏览器中运行此代码时,它会显示undefined variable: category in index.blade.php
。
答案 0 :(得分:2)
请检查您尝试在浏览器中访问的路线。
问题是,您正在尝试使用RESTful控制器。在这种情况下,您必须使用restful方法访问URL。
如果您想在restful controller中访问getIndex()方法,那么在浏览器中,您必须访问
<强> http://www.yoursite.com/admin/index 强>
基本上,Route :: controller('admin','CategoriesController');意味着,当您在URL中有/ admin /时,它将访问此CategoriesController。然后将使用动词后跟URL路径。
如果您想采用非RESTful路线,请按以下方式之一进行
在路线中传递这样的$ categories数组
Route::get('/', function(){
return View::make('categories.index')->with('categories',Category::all());
});
OR
Route::get('/',array('uses'=>'CategoriesController@getIndex'));
然后在你的CategoriesController
中public function getIndex(){
return View::make('categories.index')
->with('categories', Category::all());
}
应该修复它:)