我需要能够在整个应用程序中(在所有刀片文件中)访问某些变量。目前我只能在一个页面上访问它们,并且该页面是在控制器 (profile.show) 中设置的。我成功地通过 AppServiceProvider.php 中的 view composer 访问了其中一个变量,但我无法访问其他变量。当我将它们转储到其他刀片上时,它显示未定义的错误,但 profile_details 工作正常。我需要有关如何将这些变量($profile、$data、$options、$photos)放入 AppServiceProvider.php composer 以便能够访问它们的帮助。任何帮助表示赞赏。这是我的代码。AppServiceProvider.php<?phpnamespace App\Providers;use App\User;use App\UserProfile;use Illuminate\Support\Facades\Auth;use Illuminate\Support\ServiceProvider;use Illuminate\Database\Schema\Builder; // Import Builder where defaultStringLength method is definedclass AppServiceProvider extends ServiceProvider{ /** * Register any application services. * * @return void */ public function register() { } /** * Bootstrap any application services. * * @return void */ public function boot() { view()->composer('*', function ($view) { $view->with('profile_details', Auth::id() ? UserProfile::profileDetails(Auth::id(), Auth::id()) : []); }); Builder::defaultStringLength(191); // Update defaultStringLength }}UserProfileController.phpclass UserProfileController extends Controller{ public function showProfile($username, Request $request) { $user = $request->user(); $viewer = User::find(Auth::id()); $owner = User::where('username', $username)->first(); if($viewer->id !== $owner->id && !$owner->active){ abort(404); } if(!$profileId = User::getIdFromUsername($username)){ abort(404); } $profile = UserProfile::profileDetails($profileId, $viewer->id); $data = UserProfile::getProfileLookingFor(Auth::user()->id); $options = UserProfile::getProfileLookingForDisplayOptions(); $details = $this->returnDropdowns(); $photo = new Photo(); $photos = $photo->managePhotos(); return view('profile.show', compact('user', 'profile', 'data', 'options', 'details', 'photos')); }}
1 回答
Cats萌萌
TA贡献1805条经验 获得超9个赞
您可以使用View::share访问所有视图中的变量:
在 的boot方法中AppServiceProvider执行以下操作:
public function boot() {
$profile_details = Auth::id() ? UserProfile::profileDetails(Auth::id(), Auth::id()) : [];
View::share('profile_details', $profile_details);
Builder::defaultStringLength(191); // Update defaultStringLength
}
- 1 回答
- 0 关注
- 84 浏览
添加回答
举报
0/150
提交
取消