2013-08-16 135 views
1

我目前正在试图在索引页上创建一个允许用户创建项目的链接。我routes.php文件看起来像无法生成URL

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()只指数(),但然后我得到找不到控制器的方法。所以,这就是我使用getIndex()的原因。

我想我已正确设置我的创建控制器,但是当我去的项目/创建网址我得到一个

无法生成用于命名路线“items.store”这样路线的网址不存在。

错误。我试过使用store()和getStore()而不是postStore(),但我一直得到相同的错误。

有人知道问题可能是什么?我不明白为什么网址没有被生成。

+1

** + 1用于发布_broad代码sample_与您的问题!** –

回答

1

您正在使用路线::控制器(),它的确据我所知生成路径名。

即你指的是“items.store” - 这是一个路由名称。

你应该;

如果使用路由::资源 - 那么你就需要改变你的控制器名称

+0

Ohhhh我误解了控制器和资源。好的,这是有道理的。谢谢! –

+0

这个答案与[类似问题](http://stackoverflow.com/q/283​​88716/3334390)有什么关系?我有,使用Stylesheets函数加载同一文件夹中的一个文件,但另一个文件不是?在我的例子中,没有必要定义到样式表的路由,这是自动发生的。 –

0

的这个错误告诉你,那路线名称尚未定义:

无法生成用于命名路线“items.store”一个URL 这样路由不存在

查看Named Routes section中的Laravel 4 Docs。有几个的例子,这会让你清楚如何使用这些类型的路线。

也看看RESTful Controllers section

这里是你的问题的例子:

Route::get('items', array(
    'as' => 'items.store', 
    'uses' => '[email protected]', 
)); 
0

为转移Exchange表示,Route :: controller()不会生成名称,但可以使用第三个参数进行操作:

Route::controller( 'items', 
        'ItemController', 
        [ 
         'getIndex' => 'items.index', 
         'getCreate' => 'items.create', 
         'postStore' => 'items.store', 
         ... 
        ] 
);