0
votes

I have the following route:

Route::controller('/boards', 'BoardController');

which responds to the following routes:

/boards -> function: getIndex()
/boards/board/Some-board-Alias -> function: getBoard()

what I want to do is - when I enter the following route:

/boards/board/Some-board-Alias/items

it will execute "BoardItemController", function: getItem()

I tried doing something like this:

Route::controller('/boards/board/{board_alias}/items', 'BoardItemController');

But when I enter the following route:

/boards/board/Some-board-Alias/items

it displays the content from:

/boards/board/Some-board-Alias

Looks like it treats "items" as a param and not as part of the route. is there a way using another controller for sub route?


Just to make it clear The route:

/boards/board/Some-board-Alias

Should access BoardController@getBoard (using Route::controller)

/boards/board/some-board-Alias/items

Should access BoardItemController@getIndex (using Route::controller) also:

/boards/board/some-board-alias/items/item/123

Should access BoardItemController@getItem($id) (using Route::controller)

1
it will be very easy if you map the calls. without it, the codes will always be very dirty and unreliable. (if you change something in future, it can be a headache).itachi
What do you mean map the calls?Vlad Kucherov

1 Answers

1
votes

You can have your routes defined like this. Order is important, since Laravel will take the first route matching the URL pattern.

// If it's this specific route, go to `BoardItemController->getItem()`
Route::get('boards/board/{board_alias}/items', 'BoardItemController@getItem');
// If not, try to find another one in the rest of the file
Route::controller('boards', 'BoardController');

Then, in your controller:

// BoardItemController.php
public function getItem($alias)
{
    // do awesome stuff
}

As a side note, don't use the leading slash when defining your routes:

Route::controller('boards'); // right
Route::controller('/boards'); // wrong