欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

Controller __construct broken in laravel 5.3?

Hello I've noticed while migrating that calls made in the controller's construct can't use any alias, following this pattern gives me error:

<?php

namespace App\Http\Controllers\Laralum;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Role_User;
use Auth;

class UsersController extends Controller
{

    public function __construct()
    {
        echo Auth::user()->email;
        die();
    }
}

The error is:

Trying to get property of non-object
Best Answer(As Selected By ConsoleTVs)
nate.a.johnson

@ConsoleTVs have you read up on Gates and Policies? Sounds like they might work well for you.

https://laravel.com/docs/5.3/authorization

There are also Laracast videos on the topic.

simondavies

try switching out

echo Auth::user()->email;

with

dd(auth()->user()->email);
ConsoleTVs

@simondavies Same error, I've been reading you can't access the auth user in the controller in this new version, any solutions?

simondavies

@ConsoleTVs

mmm! try changing your use Auth to :

use Illuminate\Support\Facades\Auth;

As well as what @nate.a.johnson links says, about it

nate.a.johnson

@simondavies it was never meant to work in the constructor. Now it is restricted because of how the middleware work.

simondavies

@nate.a.johnson i know understand, dont think i ever used them in a controller anyways :-). BUt @ConsoleTVsmight be best to work on what you want to do and pass it to another class that your controller then calls, as Nate mentions, not really usable within a controller.

ConsoleTVs

@simondavies @nate.a.johnson

How would you work in my situation in another case?

Here's my example:

Imagine I have 10 controllers, each controller have a lot of functions, every controller's access depends on if the logged in user have the permissions to enter or not, (imagine a controller that controls the users, another that controls the blog, another that controls the roles, etc) I used to make use of the conscruct to check if the user had the permission to access that controller, now i can't no loger use it and I need to do it manually on each funcion of the controller

simondavies

could you not look into middleware and custom your on needs. So before hitting a controller it check the suers authentication situation

nate.a.johnson

@ConsoleTVs have you read up on Gates and Policies? Sounds like they might work well for you.

https://laravel.com/docs/5.3/authorization

There are also Laracast videos on the topic.

ConsoleTVs

Wow "Gates" are new from what I see, and seems to be perfect for my job! Thanks a lot, gonna take a look!

gocanto
 gocanto
1 year ago(106,120 XP)

Now that the auth() does not work in the construct, how can you initialize vars when the class is called?, something similar to what the construct is mean to be

jekinney
jekinney
1 year ago(227,875 XP)

@gocanto if you read the link above its because the middleware hasn't run when the controller is instaciated.

So any thing else, like a model or repo class is fine.

gocanto
 gocanto
1 year ago(106,120 XP)

if you have a auth() within a repo method, and then, you call it from the controller construct, you will see it does not work. anyhow, I read the info, my question is, how can We get a similar behavior?

luddinus

@gocanto This would not work.

// PostController

protected $userPosts;

public function __construct(PostRepository $posts)
{
   $this->userPosts = $posts->fromUser(Auth::user());   
}

// PostRepository
public functon fromUser(User $user)
{
   // $user will be null because you call from te constructor controller so it will fail
   return $user->posts;
}

// but this would work
// UserController
public function __construct(PostRepository $posts)
{
   $this->posts = $posts;
}

public function index()
{
   $posts = $this->posts->fromUser(Auth::user());

   return view('user.posts', compact('posts'));
}

In addition, I recommend to pass the "User object" to the repository instead of use the authenticated user in a repository to avoid these type of errors.

ghulamali2612@gmail.com
<?php

namespace App\Http\Controllers\Laralum;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Role_User;
use Auth;

class UsersController extends Controller
{

protected $user_id;
    
   
    public function __construct()
    {
       $this->middleware(function ($request, $next) {
            $this->user_id = Auth::user()->id;
            return $next($request);
        });
    }
}

This works for me. Hope this helps if any one need.

Please sign in or create an account to participate in this conversation.


来自  https://laracasts.com/discuss/channels/laravel/controller-constructor-broken-in-laravel-53?page=1


普通分类: