Jump to content

calling constructor method


osherdo
 Share

Recommended Posts

I am using Laravel framework.

 

I want to authenticate a user on all functions available in my code.

 

I did not understood the constructor method quite right, and the docs were hard to follow as well.

 

Assuming I have this code:

public function __construct()
 { // Constructor for checking user's auth after a while.

$this->middleware('auth');
 if (!Auth::check()) { return redirect('auth/login');
 }


function something()
{
// How do I call it to work inside this function?
}

Thanks for helping.

 

Link to comment
Share on other sites

You don't call the constructor in other methods. The sole purpose of the constructor is to initialize the object right after it has been created (hence the name), and doing this multiple times can go horribly wrong.

 

Either simply copy and paste the auth check. Or create an extra method which is called in both the constructor and other methods. Actually, an auth check in the constructor doesn't make a lot of sense to begin with.

Edited by Jacques1
Link to comment
Share on other sites

The usernames are on the blue bars. Your name is osherdo, my name is Jacques1.

 

 

 

I still don't get why I will need to call the method in the constructor as well.

 

Nobody ever said that except you. According to the documentation, you either attach the authentication check to the route or the entire controller (by calling the middleware method once in the constructor).

 

So it seems your assumption that you have to do the check per method is simply false.

Link to comment
Share on other sites

@Jacques1 thanks for that. So I understood the quoted part from your reply, and I think I may have misunderstood you.

 

Or create an extra method which is called in both the constructor and other methods. Actually, an auth check in the constructor doesn't make a lot of sense to begin with.

 
Link to comment
Share on other sites

You asked how to call a constructor in every method to perform an authentication check. I've told you this is wrong and offered a functionally equivalent but sane alternative. I have not addressed the question whether your idea is the right approach for Laravel.

 

If you want Laravel-specific help, you're in the wrong forum. I can move your thread to the frameworks section.

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.