Reputation: 5787
I am having a sentry laravel 4 setup.
at this moment for profile view I should be going to link like this
http://localhost/ffdd/public/3/show
where 3 is user id
and I am using following route
Route::get('/{profile}/show', array('as' => 'profile.show', 'uses' => 'ProfilesController@show'));
I need the same result for the link
http://localhost/ffdd/public/3
what should I change in route.
and for future I want this user id place to username .
Edit 2
my current route structure is like this.
Route::get('/{userId?}', array('as' => 'profile', 'uses' => 'ProfilesController@index'));
Route::post('/{userId?}', array('as' => 'profile-koko', 'uses' => 'Controllers\Account\ProfileController@postIndex'));
Route::get('/{profile}/show', array('as' => 'profile.show', 'uses' => 'ProfilesController@show'));
when the index of profilescontroller receive a route request it just forward it to show method.
// User is logged in
$user = Sentry::getUser();
$user = $user->id;
//return View::make('profiles.index');
return Redirect::action('ProfilesController@show', array('userId'=>$user));
}
when I am using this
Route::get('{profile}',array(
'as' => 'test2',
'uses' => 'ProfilesController@show',
));
as my friend itachi said in his reply !
I am getting this error !
Some mandatory parameters are missing ("profile") to generate a URL for route "profile.show".
edit 3
<?php
# Profile
Route::get('/{userId?}', array('as' => 'profile', 'uses' => 'ProfilesController@index'));
Route::post('/{userId?}', array('as' => 'profile-koko', 'uses' => 'Controllers\Account\ProfileController@postIndex'));
Route::resource('profile', 'ProfilesController@show');
Route::get('{profile}/show',array(
'as' => 'profile.show',
'uses' => 'ProfilesController@show',
));
Route::get('{profile}',array(
'as' => 'test2',
'uses' => 'ProfilesController@show',
));
Upvotes: 0
Views: 2337
Reputation: 6393
i don't see any use of defining two routes for the same action. but still, if you want it, here it goes.
just create two routes pointing to the same action. e.g.
Route::get('{profile}/show',array(
'as' => 'test1',
'uses' => 'ProfileController@show',
));
Route::get('{profile}',array(
'as' => 'test2',
'uses' => 'ProfileController@show',
));
now, you can access both the routes http://localhost/ffdd/public/3/show
and http://localhost/ffdd/public/3
but there is a problem here. {profile}
parameter can match anything.
e.g.
http://localhost/ffdd/public/3/show [will call the ProfileController@show as intended]
http://localhost/ffdd/public/asdf/show [will also call the ProfileController@show which was NOT INTENDED!]
to avoid this issue, you have two ways.
Declare these two routes at the very end of the file so that other routes get precedence.
As {profile}
must be an id [for now], let's put a constraint to it by declaring the follwoing,
Route::pattern('profile', '[0-9]+');
Now, the {profile}
will match only for numerics.
So the whole code becomes
Route::pattern('profile', '[0-9]+');
Route::get('{profile}/show',array(
'as' => 'test1',
'uses' => 'ProfileController@show',
));
Route::get('{profile}',array(
'as' => 'test2',
'uses' => 'ProfileController@show',
));
there wont be any conflicts here as {profile}
must be a numeric to evoke the ProfileController@show
action.
Alternative way to put constraint:
There is also an alternative way to put constraint using where
.
e.g.
Route::get('{profile}/show',array(
'as' => 'test1',
'uses' => 'ProfileController@show',
))->where('profile', '[0-9]+');
but if you go this way, you need to put this where
in every routes that you use {profile}
.
Upvotes: 2