osherdo Posted September 2, 2016 Share Posted September 2, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/302068-calling-constructor-method/ Share on other sites More sharing options...
Jacques1 Posted September 2, 2016 Share Posted September 2, 2016 (edited) 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 September 2, 2016 by Jacques1 Quote Link to comment https://forums.phpfreaks.com/topic/302068-calling-constructor-method/#findComment-1537006 Share on other sites More sharing options...
requinix Posted September 2, 2016 Share Posted September 2, 2016 Why can't you just authenticate once for the entire duration of the script? Why does each function have to perform the same check every time? Quote Link to comment https://forums.phpfreaks.com/topic/302068-calling-constructor-method/#findComment-1537030 Share on other sites More sharing options...
osherdo Posted September 3, 2016 Author Share Posted September 3, 2016 (edited) @OrpheanBeholderScryDoubt I think the second solution will be better. I still don't get why I will need to call the method in the constructor as well. @Dark Administrator this is what I am trying to figure Edited September 3, 2016 by osherdo Quote Link to comment https://forums.phpfreaks.com/topic/302068-calling-constructor-method/#findComment-1537048 Share on other sites More sharing options...
Jacques1 Posted September 3, 2016 Share Posted September 3, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/302068-calling-constructor-method/#findComment-1537050 Share on other sites More sharing options...
osherdo Posted September 3, 2016 Author Share Posted September 3, 2016 @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. Quote Link to comment https://forums.phpfreaks.com/topic/302068-calling-constructor-method/#findComment-1537062 Share on other sites More sharing options...
Jacques1 Posted September 3, 2016 Share Posted September 3, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/302068-calling-constructor-method/#findComment-1537070 Share on other sites More sharing options...
osherdo Posted September 4, 2016 Author Share Posted September 4, 2016 @Jackues1 ok I get you. Well I think the accurate question I wanted to ask for this specific thread,regardless of Laravel framework - How can I reuse a php method (suppose it contains the logic for authenticating a user) in a function? Quote Link to comment https://forums.phpfreaks.com/topic/302068-calling-constructor-method/#findComment-1537083 Share on other sites More sharing options...
Jacques1 Posted September 4, 2016 Share Posted September 4, 2016 And that's exactly the question I have answered in my very first reply: To reuse code, you create an extra method and then call it in the methods that need the feature. You might want to reread this thread, because I get the impression you've missed pretty much all of it. Quote Link to comment https://forums.phpfreaks.com/topic/302068-calling-constructor-method/#findComment-1537086 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.