Is this the logged in user? If so, this is not the route I'd be going down... just reference auth()->user()->id where needed.
Problem with accessing global variable in Laravel application
I am trying to make a global variable in AppServiceProvider.php that I will need throught my whole application meaning in all blade files. This variable is $profile which gets the profile data from user and displays them in blades. I made it so when I am on my profile it shows authenticated user which is me and it is fine (in url is like this profile/Authuser), that Authuser is username from database. Problem is when I go to some other profile then I get error undefined username (in url profile/Someuser). I need help on to get that username in AppServiceProvider.php. Problem is in that $username in service provider. I don't know how to pass it in there globally. Any help is appreciated. Here is my code.
AppServiceProvider.php
public function boot()
{
$profileId = $this->getIdFromUsername($username); // Here is problem, I don't know how to get that username
view()->composer('*', function ($view) {
$view->with('profile', Auth::id() ? UserProfile::profileDetails($profileId, Auth::user()->id) : []);
});
Builder::defaultStringLength(191); // Update defaultStringLength
}
public function getIdFromUsername($username)
{
if ($user = User::where('username', $username)->first()) {
return $user->id;
}
return abort(404);
}
UserProfileController.php
public function showProfile($username, Request $request)
{
$profileId = User::getIdFromUsername($username);
$user = User::with('userProfile')->where('id', $profileId)->first();
$viewer = User::find(Auth::id());
$owner = User::where('username', $username)->first();
if($viewer->id !== $owner->id && !$owner->active){
abort(404);
}
if(!$profileId){
abort(404);
}
$profile = UserProfile::profileDetails($profileId, $viewer->id);
return view('profile.show', compact('user', 'profile'));
}
UserProfile.php
public static function profileDetails($profileId, $viewerId)
{
$profile = static::profileData($profileId, $viewerId);
if ($profile) {
if ($viewerId != $profileId) {
# Viewer is displaying another user's profile.
$myProfile = false;
} else {
$myProfile = true;
}
}
$profile = [
'viewerId' => $viewerId,
'profile_id' => $profileId,
'myProfile' => $myProfile,
];
return $profile;
}
web.php
Route::get('profile/{profile}', 'UserProfileController@showProfile')->name('profile.show');
To create a new middleware, use the make:middleware Artisan command:
php artisan make:middleware ImpersonateUser
<?php
namespace App\Http\Middleware;
use Closure;
class CheckAge
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
// If the request does not say which username to get, it's not trying to impersonate
if (request('username')) {
return $next($request);
}
// Otherwise, set the impersonated User if the request contains "impersonate_username".
// Only set this if it isn't already in the session.
if (
(session()->has('impersonatedUser') == false)
&& (request('impersonate_username'))
) {
session(['impersonatedUser' => User::where('username', request('impersonate_username'))->first()]);
}
// Go to the next request.
return $next($request);
}
}
Register the middleware in your $routeMiddleware. See: https://laravel.com/docs/7.x/middleware#assigning-middleware-to-routes
In your routes file (presumably web.php):
Route::group(['middleware' => 'impersonate'], function ($app) {
// ------------------------ YOUR ROUTES GO HERE ------------------------ //
});
I'm not sure what you mean when you say the username comes from the database. I understand you store a user in the database, but your application must somehow be told which username to use. That's going to come from a limited number of places. Being sent in a request (usually in an /impersonate?impersonate_username=crashoveride, but I suppose you could also hard-code it into a config file if it's only ever going to be one username.)
For what it's worth, I think I'd place a
STORE /impersonate/{user}
where {user} was a userId to impersonate. It would be wrapped in auth middleware and also abort_if the logged in user doesn't have the authority to impersonate the given user.
I'd also add a
DELETE /impersonate
That simply removes the impersonatedUser from the session.
Please or to participate in this conversation.