Josh L
Josh L

Reputation: 105

Unable to generate a URL

I am currently trying to create a link on the index page that'll allow users to create an item. My routes.php looks like

Route::controller('items', 'ItemController');

and my ItemController looks like

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');
  }
}

I have tried changing the getIndex() to just index() but then I get a controller method not found. So, that is why I am using getIndex().

I think I have set up my create controllers correctly but when I go to the items/create url I get a

Unable to generate a URL for the named route "items.store" as such route does not exist.

error. I have tried using just store() and getStore() instead of postStore() but I keep getting the same error.

Anybody know what the problem might be? I don't understand why the URL isn't being generated.

Upvotes: 1

Views: 715

Answers (3)

Antonio Carlos Ribeiro
Antonio Carlos Ribeiro

Reputation: 87719

As The Shift Exchange said, Route::controller() doesn't generate names, but you can do it using a third parameter:

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

Upvotes: 0

Laurence
Laurence

Reputation: 60038

You are using Route::controller() which does generate route names as far as I know.

i.e. you are referring to "items.store" - that is a route name.

You should either;

If you use Route::resource - then you'll need to change your controller names

Upvotes: 1

Markus Hofmann
Markus Hofmann

Reputation: 3447

The error tells you, that the route name is not defined:

Unable to generate a URL for the named route "items.store" as such route does not exist.

Have a look in the Laravel 4 Docs in the Named Routes section. There are several examples that'll make you clear how to use these kind of routes.

Also have a look at the RESTful Controllers section.

Here's an example for your question:

Route::get('items', array(
    'as'   => 'items.store',
    'uses' => 'ItemController@getIndex',
));

Upvotes: 0

Related Questions