Laravel route /users/id or /users/username to return users.show

I would like to be able to accept either a 40-character string ID /users/{id} or the user's username /users/{username} and then return the users.show view.

  1. Is this possible?
  2. Where would the checks go?

I have found another solution to this. I don't know which is better though. Hopefully the community can vote...

Within my UsersController.php I have:

public function show($id_or_username)
    {
        $user = User::where('id' , '=', $id_or_username)->orWhere('username', $id_or_username)->firstOrFail();
        return View::make('users.show', compact('user'));
    }

using username on url instead of id, I have found another solution to this. I don't know which is better though. Hopefully the community can vote Within my UsersController.php I have: Route filters provide a convenient way of limiting access to a given route, which is useful for creating areas of your site which require authentication. There are several filters included in the Laravel framework, including an auth filter, an auth.basic filter, a guest filter, and a csrf filter. These are located in the app/filters.php file.

When I need to match certain patterns, I use the Route::pattern() method in the top of my routes.php file. For example:

Route::pattern('userid', '[0-9]{40}');
Route::pattern('username', '[a-zA-Z0-9]+';

You can easily adapt that to use your own regular expression to match whatever you need and then you just create two routes to match the requests:

Route::get('/users/{userid}', 'UserController@showByUserID');
Route::get('/users/{username}, 'UserController@showByUserName');

Those two controller methods would get the user differently, but both would call the same view file and pass in the user.

display user profile URI by username instead of ID, i tried to change the id to username in my route but im getting error In show method you need to find users by their username instead: public function show​( App\User $user ) { return view('users.show', As long you type-hint the User class in both controller's methods, Laravel will automatically bind the  The throttling is unique to the user's username / e-mail address and their IP address. Manually Authenticating Users. Note that you are not required to use the authentication controllers included with Laravel. If you choose to remove these controllers, you will need to manage user authentication using the Laravel authentication classes directly.

Implicit Model Binding

For those who are looking for the Laravel 5.3+ answer:

Jeffery Way talked about this in one of his laracasts; not sure which one though.

Basically, you can pass the username through a route like this:

// you will need to set this up so the route ('/user/{user}')
// matches the Model you're binding to -> App\<User>
// ('/user/{username}') will not work 

Route::get('/user/{user}', function(App\User $user){
    // $user should now be App\User
    return view('user.show', compact('user'));

    // access the user in the 'user.show' view
    // like: $user->username
});

However, to make this work, you need to add this to the User class:

public function getRouteKeyName()
{
    return 'username';
}

Otherwise this would only work if you passed the id of the user in place of the username -> /user/1

Authentication - Laravel, At the moment, the user profile is accessible by its ID, like so /profile/5 I would like to access it else { $user = User::find(Auth::user()->id); } return view('user/profile'​, [ 'user' => $user ]); } https://laravel.com/docs/5.1/routing. Redirect responses are instances of the Illuminate\Http\RedirectResponse class, and contain the proper headers needed to redirect the user to another URL. There are several ways to generate a RedirectResponse instance. The simplest method is to use the global redirect helper: Sometimes you may wish to redirect the user to their previous

Routing - Laravel, Routing; Views; Authenticating; Retrieving The Authenticated User; Protecting Routes $user = Auth::user(); // Get the currently authenticated user's ID. the check method on the Auth facade, which will return true if the user is authenticated: The Laravel query builder uses PDO parameter binding to protect your application against SQL injection attacks. There is no need to clean strings being passed as bindings. PDO does not support binding column names. Therefore, you should never allow user input to dictate the column names referenced by your queries, including "order by" columns

auth middleware - Authentication - Laravel, For example, you may need to capture a user's ID from the URL. You may do so by defining route parameters: Route::get('user/{id}', function ($id) { return 'User '. A quick great tip for you guys. Laravel default authentication allows you to login with an email only but we can also use some other field like username. Laravel by default only allows one field email for login but we can also use username with it as well. In this tutorial, we will see how …

HTTP Routing - Laravel, The simplest Laravel routes consist of a URI and a Closure callback. '[0-9]+'); Route::get('user/{id}', function($id) { // Only called if {id} is numeric. }); Route::get​('user', array('before' => 'old', function() { return 'You are over 200 years old!'; }));  Building a User Invitation System with Laravel March 30, 2017 / Joe Dixon In the not too distant past at ubisend , we needed to build a solution to provide our users the ability to invite additional users to manage their account.

Route Model Binding: How to Change The Key, By default, Laravel includes an App\User model in your app directory. for the user. The attempt method will return true if authentication was successful. To log a user into the application by their ID, use the loginUsingId method: Route middleware can be used to allow only authenticated users to access a given route. So im trying to have a table in the admin panel where i can delete users. So i have managed to get it deleting but its deleting the logged in user and not the user in the table row. This is my controller

Comments
  • The moment you set getRouteKeyName you won't be able to navigate by ID, only username. OP's question specifies they want to be able to route by both at the same time.