Search code examples
laravellaravel-5laravel-authenticationlaravel-helper

How to Add a Custom Function That Can Be Chained with the Auth() Function?


As stated in the documentation, the auth() function can be used instead of the Auth facade, returns an authenticator instance and can be used like this:

auth()->check() 
auth()->guest()
auth()->id() 
auth()->user()

As you can see - I can chain ->check() or ->guest() or ->id() or ->user().

Is it possible to add a new custom function that can be also chaned? For example, somethingNew() that can be used like this:

auth()->somethingNew() // returns boolean

Solution

  • The auth helper simply returns the instance of the guard being used. You can implement custom guards which would expose your somethingNew method.

    Configure the application to use your new guard in config/app.php and enjoy calling auth()->somethingNew().