Reputation: 2552
Let say I have my user
controller and there are action_index()
, action_login()
, action_logout()
action_profile($userid)
methods in it. I want to make a routing that
www.mysite.com/user/xxxx
checks the xxxx
part of the url and if it is not one of (login,logout,index) it calls action_profile(xxxx)
method.
Now I am doing it like this:
my routing routs all www.mysite.com/user/xxxx
type of requests to action_index
and it checks whether xxxx
is a method name or not. If it is not a method name it calls action_profile(xxxx)
However, I think it is possible in a better way. How can I do it better?
Upvotes: 2
Views: 2434
Reputation: 18665
I would recommend you avoid using Route::controller()
in this instance. While it can be fine to use in some cases, for what you're after it would be better to map to the routes.
You could do it like so.
Route::get('user', 'user@index');
Route::get('user/(:num)', 'user@profile');
Route::get('user/(:any)', 'user@(:1)');
Or you could be a little stricter with your last route.
Route::get('user/(login|logout)', 'user@(:1)');
My reasoning for recommending you avoid Route::controller()
is that it does create duplicates to some content. For example, yoursite.com/user
will by duplicated on yoursite.com/user/index
. This can be bad for search engine optimization.
Mapping to your actions gives you that extra flexibility and control.
Upvotes: 3
Reputation: 3199
Hmm I'm not sure if I understand what you're asking. Routes in laravel are on a first match basis.
So you could just add the following to your routes.php
:
Route::get('user/(:num)', 'user@profile');
Route::controller('user');
The first line is for routing user/xxx
to action_profile()
in user controller where xxx
is any numerical value. While the second will maps any other URI (user/***/***
) to corresponding user controller's methods. That means it automatically maps user/login
to action_login()
, user/register
to action_register()
and so on.
Upvotes: 5