Getting username based profile in laravel & sentry 2 instead of user id

1.8k Views Asked by At

I am using laravel. for authentication sentry 2. When the user is visiting at my site they are going to the page

http://dolovers.com/profile/13

Here 13 is the user id.

I am researching to have a better procedure to find user by its username

and also I want to reduce the link by:

http://dolovers.com/<username>

how should I start with sentry2 and laravel. Thanks for the help :) route for profile :

# Profile
// Route::get('profile', array('as' => 'profile', 'uses' => 'Controllers\Account\ProfileController@getIndex'));
Route::get('profile', array('as' => 'profile', 'uses' => 'ProfilesController@index'));
Route::post('profile', 'Controllers\Account\ProfileController@postIndex');
1

There are 1 best solutions below

7
On BEST ANSWER

This is how you find your user by id:

$user = Sentry::findUserById(13);

Or login

$user = Sentry::findUserByLogin('[email protected]');

$user = Sentry::findUserByLogin('sagiruddin-mondal');

To reduce your link, you need to edit your route:

Route::get('/{userId}', 
             array(
                    'as' => 'user.find.get', 
                    'uses' => 'ProfilesController@index'
                  ) 
           );

Route::post('/{userId}', 
             array(
                    'as' => 'user.find.post', 
                    'uses' => 'ProfilesController@postIndex'
                  ) 
           );

Route::get('/', 
             array(
                    'as' => 'home', 
                    'uses' => 'HomeController@index'
                  ) 
           );

It should let you use route like

http://dolovers.com/<userid>

And also let you create urls using

URL::route('user.find.get', array(13));

URL::route('user.find.post', array(13));

I also define a second route (home) to point to

http://dolovers.com/

Your UsersController would look like:

class UsersController extends Controller {

    protected function findUser($username)
    {
        if ( ! $user = Sentry::findUserByLogin('sagiruddin-mondal'))
        {
            return "user not found";
        }

         return $user->name;
    }

}