Ultear's avatar

Laravel named routes returns "Whoops" error

Hello,

I'm new to Laravel and i have my first problem standing in front of me.

I've set 2 routes:

Route::get('/', function() { return 'Welcome to index file'; });

Route::get('user/profile', [ 'as' => 'profile', function(){ return 'UserController@showProfile'; } ]);

Either i use localhost/ or localhost/user/profile both are working, problem is when i want to use "profile" alias, then my laravel returns "Whoops" error. Mod_rewrite with Allow override All are set

Debug says that requested file doesnt exist.

I'm sorry if i wrote on wrong forum, didn't know which should i use, I'm new here :)

Thank you for help

0 likes
16 replies
toniperic's avatar

But do you set the routes in the routes.php file accordingly? Can you show us the content of routes.php file?

Ultear's avatar
/* |-------------------------------------------------------------------------- | Application Routes |-------------------------------------------------------------------------- | | Here is where you can register all of the routes for an application. | It's a breeze. Simply tell Laravel the URIs it should respond to | and give it the Closure to execute when that URI is requested. | */

Route::get('/', function() { return 'Welcome to index file'; });

Route::get('user/profile', [ 'as' => 'profile', function(){ return 'UserController@showProfile'; } ]);

This is my routes.php @sabuncuserhat i dont want to call controller just return string to see if it works, but it doesnt

toniperic's avatar

Well it won't respond to http://localhost/profile because you haven't set it the right way. Change

Route::get('user/profile', [ 'as' => 'profile', function(){ return 'UserController@showProfile'; } ]);

to

Route::get('profile', [ 'as' => 'profile', function(){ return 'UserController@showProfile'; } ]);
usman's avatar

This is not how named routes work, you cannot simply call them inside url. They help you in generating urls for instance:


//somewhere in your view 
<a href="{{route('profile')}}"> profile</a>

This will automatically generate the anchor with href="localhost/user/profile" .

Ultear's avatar

@toniperic that works, just one more question

first parameter is url to fire up link in "as" parameter yes?

Ultear's avatar

@usman i've already read this, just tell me one thing

Lets say i've shop controller with show item action, url would look like localhost/shop/showitem/id

What should i do to make it look like

localhost/showitem/id?

Is it part of named routes functionality?

usman's avatar

No. You can specify a route like this:

Route::get('showitem/{id}', 'YourController@showItem');

//To get benefits of named routing you can use:
Route::get('showitem/{id}', ['as'=>'item.show','uses'=>'YourController@showItem']);
//Now using the route name you can conveniently generate urls for the 'showitem/{id}' using <a href="{{route('item.show',$item->id)}}">edit</a>.

It is always up to you.

Ultear's avatar

Is "item.show" from "as" parameter tied with someting or you just type there whatever you want and what is most comfortable?

usman's avatar

Yeah it is a matter of choice. This way you can differentiate your routes from the others.

Please or to participate in this conversation.