我需要能够在整个应用程序中(在所有刀片文件中)访问某些变量。目前我只能在一个页面上访问它们,并且该页面是在控制器 (profile.show) 中设置的。我成功地通过 AppServiceProvider.php 中的 view composer 访问了其中一个变量,但我无法访问其他变量。当我将它们转储到其他刀片上时,它显示未定义的错误,但 profile_details 工作正常。我需要有关如何将这些变量($profile、$data、$options、$photos)放入 AppServiceProvider.php composer 以便能够访问它们的帮助。任何帮助表示赞赏。这是我的代码。
AppServiceProvider.php
<?php
namespace 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 defined
class 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.php
class 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'));
}
}
Cats萌萌