【发布时间】:2020-04-10 13:46:12
【问题描述】:
我正在尝试在 AppServiceProvider.php 中创建一个全局变量,我需要在所有刀片文件中贯穿我的整个应用程序含义。此变量是 $profile,它从用户那里获取配置文件数据并将它们显示在刀片中。我这样做了,所以当我在我的个人资料中时,它显示经过身份验证的用户是我,这很好(在 url 中就像这个个人资料/Authuser),Authuser 是来自数据库的用户名。问题是当我转到其他配置文件时,我收到错误未定义的用户名(在 url 配置文件/Someuser 中)。我需要帮助才能在 AppServiceProvider.php 中获取该用户名。问题在于服务提供商中的 $username。我不知道如何在全球范围内传递它。任何帮助表示赞赏。这是我的代码。
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);
}
web.php
Route::get('profile/{profile}', 'UserProfileController@showProfile')->name('profile.show');
【问题讨论】: