2

I have an integration.php file which is in public/arrowchat/includes/integration.php

I was wondering how do I access the Auth class within there.

I have tried this but it doesn't seem to work.

<?php
    use Auth;

    function get_user_id() 
    {
        if (Auth::guest()) 
        {
            return NULL;
        } else {
            return Auth::user()->id;
        }
    }
?>
2
  • I'm using laravel framework Commented Oct 12, 2016 at 17:49
  • What you should do is to specify why "it doesn't seem to work". Is it throwing an error? What error? Without it, we'll just be guessing. Commented Oct 12, 2016 at 21:06

1 Answer 1

1

If you want to use Auth facade in a plain (read - invoked/executed directly, not within the the framework) PHP file, you still need to require composer autoloader so that classes such as Auth are loaded. In your example above, you are most likely to get an unknown class error.

But even then, Auth most probably relies on classes like Request to get the headers and cookies and Request is initialised in Laravel bootstrap.

If you really, really need to do this - you will have to copy/paste some of the bootstrap stuff from Laravel's bootstrapper in order to make this work.

Otherwise, I would recommend you to make a Laravel console command instead and then everything will just work out of the box.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.