Support the ongoing development of Laravel.io →
posted 11 years ago
Views Blade

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!

Last updated 3 years ago.
0

You need to name your route, like so :

Route::get('dashboard', array('as' => 'dashboard', 'uses' => 'PagesController@showDashboard'));
Last updated 3 years ago.
0

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.

Last updated 3 years ago.
0

Thank you both very much! That's very helpful.

Last updated 3 years ago.
0

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
Last updated 9 years ago.
0

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>
Last updated 8 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

zocios zocios Joined 28 May 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.