Sorry, I'm just learning Laravel and I'm stumped on what I'm sure is a very simple issue: How to link_to_route when the route is defined in a non-resource controller.
I am trying to use controllers for all my routing. So, I have several miscellaneous pages that don't fit into resources, and are listed in my PagesController. I'll show only one for this discussion:
Route:
/* PagesController */
Route::get('dashboard', 'PagesController@showDashboard');
PagesController:
public function showDashboard()
{
return View::make('dashboard');
}
I have no trouble linking to the dashboard page using a non-Blade link. e.g. this works:
<a href='../dashboard' class='body'>Dashboard</a>
But if I try to use Blade linking to the route rather than specifying the path in a link (so I can use the same code regardless of the page I'm linking from), it doesn't work, e.g., this does NOT work, yielding a "Route not defined" error:
{{ link_to_route('dashboard', 'Dashboard', '', array('class'=>'body')) }}
Routing to 'showDashboard' also does not work.
What am I not doing correctly? Thanks!
You need to name your route, like so :
Route::get('dashboard', array('as' => 'dashboard', 'uses' => 'PagesController@showDashboard'));
You can also use the action
helper when building the link like so:
<a href="{{ action("PagesController@showDashboard") }}">link</a>
It will generate a URL based on the routes table.
Thank you both very much! That's very helpful.
Please note
<a href="{{ action("PagesController@showDashboard") }}">link</a>
does not work if the controller manages a number of different views
However, you can check the actual routes (rather than url)'s using:
php artisan route:list
I prefer to use route() wherevere possible, don't know if it is a common aproach though:
Route::get('dashboard', ['as' => 'dashboard', 'uses' => 'PagesController@showDashboard']);
...
<a href="{{ route('dashboard') }}">Dashboard</a>
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community