Laravel - 如何在应用服务提供商中获取用户ID

时间:2018-05-05 03:45:08

标签: laravel laravel-5

我尝试在我的应用服务提供商中获取用户ID,但是我收到错误显示错误异常:尝试获取非对象的属性,任何想法如何获取用户ID?

AppServiceProvider:

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use App\Project;

use Log;
use Auth;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    { 
        $userid =  Auth::user()->id;
        Log::info('userid :', [$userid]);


        $project = Project::where('user_id',Auth::user()->id)->count();

    }
}

错误:

local.ERROR: ErrorException: Trying to get property of non-object in C:\wamp64\www\test\app\Providers\AppServiceProvider.php:20 Stack trace: #0 C:\wamp64\www\test\app\Providers\AppServiceProvider.php(20): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(8, 'Trying to get p...', 'C:\\wamp64\\www\\f...', 20, Array) #1 [internal function]: App\Providers\AppServiceProvider->boot() #2 C:\wamp64\www\test\vendor\laravel\framework\src\Illuminate\Container\Container.php(507): call_user_func_array(Array, Array) #3 C:\wamp64\www\test\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(734): Illuminate\Container\Container->call(Array) #4 C:\wamp64\www\test\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(717): Illuminate\Foundation\Application->bootProvider(Object(App\Providers\AppServiceProvider)) #5 [internal function]: Illuminate\Foundation\Application->Illuminate\Foundation\{closure}(Object(App\Providers\AppServiceProvider), 19) #6 C:\wamp64\www\test\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(718): array_walk(Array, Object(Closure)) #7 C:\wamp64\www\test\vendor\laravel\framework\src\Illuminate\Foundation\Bootstrap\BootProviders.php(17): Illuminate\Foundation\Application->boot() #8 C:\wamp64\www\test\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(203): Illuminate\Foundation\Bootstrap\BootProviders->bootstrap(Object(Illuminate\Foundation\Application)) #9 C:\wamp64\www\test\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(222): Illuminate\Foundation\Application->bootstrapWith(Array) #10 C:\wamp64\www\test\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(117): Illuminate\Foundation\Http\Kernel->bootstrap() #11 C:\wamp64\www\test\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(87): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request)) #12 C:\wamp64

3 个答案:

答案 0 :(得分:2)

使用View::composer('*',)在所有视图中使用Auth。

 use Illuminate\Support\Facades\View;
.........
public function boot()
{ 
     View::composer('*', function($view)
     {
         if (Auth::check()){
         $project = Project::where('user_id',Auth::id() )->count();
    }
 });

您可以将其理解为在视图渲染之前进行身份验证之后,您需要在这些之前进行检查。

答案 1 :(得分:0)

在服务提供商的boot()方法中,您应该只引导您的应用程序,而不是执行有关查找或输出数据的任何操作。如果确实需要在服务提供商中执行此操作,则可以在提供商的register()方法中执行此操作。不过,更好的地方可能是控制器或命令。

直接使用Auth::user()也很危险,因为您的应用中可能没有用户通过身份验证。您可以在使用if(Auth::check()) { ... }访问用户之前执行Auth::user(),或者在您的情况下就足够了,您可以使用Auth::id()访问用户ID。如果没有可用的用户,它也会返回null,即Auth::check() === false

答案 2 :(得分:-1)

Auth将始终尝试获取用户登录或不登录的ID。如果用户未登录,则会出现非对象错误,因为没有任何内容可以通过用户模型对象获取。

 public function boot()
    { 
        if (Auth::check()) {
           $userid =  Auth::user()->id;
           Log::info('userid :', [$userid]);


           $project = Project::where('user_id',Auth::user()->id)->count();
        }

    }
祝你好运。