Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.3] [Bug] with accessing the user. #14983

Closed
Radostin opened this issue Aug 24, 2016 · 9 comments
Closed

[5.3] [Bug] with accessing the user. #14983

Radostin opened this issue Aug 24, 2016 · 9 comments

Comments

@Radostin
Copy link

Radostin commented Aug 24, 2016

Hello,

I've just started experimenting with L5.3. Basically, it seems that the authentication system is not booted, when resolving a Controller. I know that there have been changes in the way Controllers are being resolved in 5.3, and I hope that is not the issue. So, here's the code.

class Controller extends BaseController
{

    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    /**
     * Controller constructor.
     */
    public function __construct()
    {
        view()->share('currentUser', auth()->guard('admin')->user());
        // currentUser = null
    }
}

It seems that when resolving the controller, the authentication system doesn't recognize the user. Hence, the auth()->guard('admin')->user() returns null.

But, if I use the same code inside my blade, it works. Consequently, that means that the authentication system gets registered/booted once we start resolving the views.

// that is way too verbose
auth()->guard('admin')->user()->profilePicture()

// if I could get away with:
$currentUser->profilePicture();

If you wonder, whether I have something weird going on with my service providers, here's an excerpt of my app.php file:

 Illuminate\Auth\AuthServiceProvider::class,
        Illuminate\Broadcasting\BroadcastServiceProvider::class,
        Illuminate\Bus\BusServiceProvider::class,
        Illuminate\Cache\CacheServiceProvider::class,
        Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class,
        Illuminate\Cookie\CookieServiceProvider::class,
        Illuminate\Database\DatabaseServiceProvider::class,
        Illuminate\Encryption\EncryptionServiceProvider::class,
        Illuminate\Filesystem\FilesystemServiceProvider::class,
        Illuminate\Foundation\Providers\FoundationServiceProvider::class,
        Illuminate\Hashing\HashServiceProvider::class,
        Illuminate\Mail\MailServiceProvider::class,
        Illuminate\Notifications\NotificationServiceProvider::class,
        Illuminate\Pagination\PaginationServiceProvider::class,
        Illuminate\Pipeline\PipelineServiceProvider::class,
        Illuminate\Queue\QueueServiceProvider::class,
        Illuminate\Redis\RedisServiceProvider::class,
        Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
        Illuminate\Session\SessionServiceProvider::class,
        Illuminate\Translation\TranslationServiceProvider::class,
        Illuminate\Validation\ValidationServiceProvider::class,
        Illuminate\View\ViewServiceProvider::class,

        /*
         * Package Service Providers...
         */
        HtmlServiceProvider::class,
        Barryvdh\Debugbar\ServiceProvider::class,
        Laracasts\Flash\FlashServiceProvider::class,
        //        JavaScriptServiceProvider::class,
        Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class,

        /*
         * Application Service Providers...
         */
        App\Providers\AppServiceProvider::class,
        // App\Providers\BroadcastServiceProvider::class,
        App\Providers\AuthServiceProvider::class,
        App\Providers\EventServiceProvider::class,
        App\Providers\RouteServiceProvider::class,

I tried moving things around, but nothing seemed to work.

Thanks in advance!

@Radostin Radostin changed the title [5.3] Bug with accessing the user. [5.3] [Bug] with accessing the user. Aug 24, 2016
@borutojisan
Copy link

thank you for this ... having the same problem too..hope there is a solution very very soon.. T.T

@tzurbaev
Copy link
Contributor

You have to change this since with Laravel 5.3 you can not access user in Controller constructors.

As an alternative, you can use View::share or View Composers to share variables with views.

@Radostin
Copy link
Author

That's fine, but I want to share them in bulk. Hence, I would have to declare the share and/or the composer in a service provider. However, that doesn't work either. Plus, I just noticed that I can't do auth()->user() in a middleware as well.

I hope that those changes were not intentional.

@vniko
Copy link

vniko commented Aug 24, 2016

You have to change this since with Laravel 5.3 you can not access user in Controller constructors.

Could you give a link to the doc where such behavior is mentioned?

@tzurbaev
Copy link
Contributor

@vniko not documented yet, see #14968

@borutojisan
Copy link

as OP stated.. i need to share stuffs in bulk too..... any alternative?? my code relies largely on some data of the auth shared in bulk..its used frigging everywhere..lol

@vniko
Copy link

vniko commented Aug 24, 2016

@tzurbaev while it's not documented it's a bug, not a feature : ) And it's a really bad change

@crynobone
Copy link
Member

#14834 (comment)

@doctorawesome
Copy link

doctorawesome commented Aug 24, 2016

I had similar problem. Fix it with middleware class. Put Sentinel::check() and View::share. If I need object in controller, just get it by $request.

I don't now is it good way or not, but it's working for me :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants