我目前正在尝试在索引页面上创建一个允许用户创建项目的链接。我的routes.php看起来像
Route::controller('items', 'ItemController');
,我的ItemController看起来像
class ItemController extends BaseController
{
// create variable
protected $item;
// create constructor
public function __construct(Item $item)
{
$this->item = $item;
}
public function getIndex()
{
// return all the items
$items = $this->item->all();
return View::make('items.index', compact('items'));
}
public function getCreate()
{
return View::make('items.create');
}
public function postStore()
{
$input = Input::all();
// checks the input with the validator rules from the Item model
$v = Validator::make($input, Item::$rules);
if ($v->passes())
{
$this->items->create($input);
return Redirect::route('items.index');
}
return Redirect::route('items.create');
}
}
我尝试将getIndex()更改为index(),但后来找不到控制器方法。所以,这就是我使用getIndex()的原因。
我想我已正确设置了我的创建控制器,但当我转到items / create url时,我得到了一个
无法为命名路由“items.store”生成URL,因为此路由不存在。
错误。我尝试使用store()和getStore()而不是postStore(),但我一直得到同样的错误。
有人知道问题可能是什么吗?我不明白为什么没有生成URL。
答案 0 :(得分:1)
您正在使用Route :: controller(),据我所知,它会生成路由名称。
即。你指的是“items.store” - 这是一个路线名称。
你应该;
Route::resource('items', 'ItemController');
see docs here 如果您使用Route :: resource - 那么您需要更改控制器名称
答案 1 :(得分:0)
错误告诉您,路由名称未定义:
无法为指定路线生成URL“items.store”,因为此类路线不存在。
查看Named Routes section中的Laravel 4文档。有几个示例可以让您明白如何使用这些路线。
另请查看RESTful Controllers section。
以下是您问题的示例:
Route::get('items', array(
'as' => 'items.store',
'uses' => 'ItemController@getIndex',
));
答案 2 :(得分:0)
正如Shift Exchange所说,Route :: controller()不会生成名称,但您可以使用第三个参数来完成:
Route::controller( 'items',
'ItemController',
[
'getIndex' => 'items.index',
'getCreate' => 'items.create',
'postStore' => 'items.store',
...
]
);