在laravel服务提供商中使用auth [英] using auth in laravel service provider
本文介绍了在laravel服务提供商中使用auth的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
每当我尝试使用\Auth::User()
时,我都会获得非对象属性,因为只要在服务提供商中使用它们,我的Auth::guest()
都将返回true
whenever i tried to use \Auth::User()
i am getting non object property because my Auth::guest()
returns true whenever i use them in service provider
use Illuminate\Contracts\View\View;
use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\View\Factory;
use App\relations;
use App\User;
use DB;
use Illuminate\Support\Facades\Auth;
class RelationServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
//
\Auth::User()->id;
$relation_friend_for_logged_user = DB::select( DB::raw("SELECT * FROM users"));
$value = "asd";
// for injecting objct
View()->share('count', $value);
}
但是为什么\Auth::guest()
无论我是否登录都返回true
but why \Auth::guest()
is returning true whether i am logged in
推荐答案
您可能想使用 View Composer .据我所知,您的服务提供商启动方法中尚未提供经过身份验证的用户.
You probably want to use a View Composer for this. As far as I know the authenticated user is not yet available in your service providers boot method.
public function boot(Guard $auth) {
view()->composer('*', function($view) use ($auth) {
// get the current user
$currentUser = $auth->user();
// do stuff with the current user
// ...
// pass the data to the view
$view->with('currentUser', $currentUser);
});
}
这篇关于在laravel服务提供商中使用auth的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文