PHPackages                             knpuniversity/guard - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Security](/categories/security)
4. /
5. knpuniversity/guard

AbandonedArchivedLibrary[Security](/categories/security)

knpuniversity/guard
===================

Provides Guard-style authentication in Symfony's security component

0.5(10y ago)9931.9k↓50%7[8 issues](https://github.com/knpuniversity/KnpUGuard/issues)1MITPHPPHP &gt;=5.3.9

Since Jun 28Pushed 10y ago9 watchersCompare

[ Source](https://github.com/knpuniversity/KnpUGuard)[ Packagist](https://packagist.org/packages/knpuniversity/guard)[ Docs](http://knpuniversity.com)[ RSS](/packages/knpuniversity-guard/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (5)Dependencies (3)Versions (6)Used By (1)

KnpGuard
========

[](#knpguard)

Add simple and beautiful authentication to Symfony's security component in Silex and anywhere else.

[![Build Status](https://camo.githubusercontent.com/ad75c87afa817348d0e9f4a9709e3c0e5d9cbf800202ee9cff25f7625fdafb20/68747470733a2f2f7472617669732d63692e6f72672f6b6e70756e69766572736974792f4b6e705547756172642e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/knpuniversity/KnpUGuard)

**This library is *deprecated* since Symfony 2.8 and won't work with Symfony 3.**

The original purpose was to get feedback and use-cases from people so that we can merge this feature into Symfony itself (see [symfony/symfony#14673](https://github.com/symfony/symfony/pull/14673)).

Now it's good (see [news from Symfony](http://symfony.com/blog/new-in-symfony-2-8-guard-authentication-component)).

Upgrade to Symfony 3
--------------------

[](#upgrade-to-symfony-3)

On Symfony 2.8, use the official [Guard component](https://symfony.com/doc/master/cookbook/security/guard-authentication.html).

### Step 1 - Remove the library from your composer.json

[](#step-1---remove-the-library-from-your-composerjson)

Be sure to be on Symfony 2.8, open `composer.json` file and remove the library:

Before:

```
{
    "require": {
        "php": ">=5.5",
        "symfony/symfony": "~2.8",
        "...": "...",
        "knpuniversity/guard-bundle": "~0.1@dev"
    },
}
```

Now:

```
{
    "require": {
        "php": ">=5.5",
        "symfony/symfony": "~2.8",
        "...": "..."
    },
}
```

### Step 2 - Remove it from your AppKernel

[](#step-2---remove-it-from-your-appkernel)

If you're using the Symfony framework, remove the KnpUGuardBundle from `AppKernel.php`.

### Step 3 - Modify firewall(s)

[](#step-3---modify-firewalls)

Open and modify `security.yml` file, replace in your firewall(s) key(s) `knpu_guard` by `guard`:

Before:

```
# app/config/security.yml
security:
    # ...

    firewalls:
        # ...

        main:
            anonymous: ~
            logout: ~

            knpu_guard:
                authenticators:
                    - app.form_login_authenticator

            # maybe other things, like form_login, remember_me, etc
            # ...
```

Now:

```
# app/config/security.yml
security:
    # ...

    firewalls:
        # ...

        main:
            anonymous: ~
            logout: ~

            guard:
                authenticators:
                    - app.form_login_authenticator

            # maybe other things, like form_login, remember_me, etc
            # ...
```

### Step 4 - Update Authenticator(s)

[](#step-4---update-authenticators)

Update uses in Authenticator(s) class(es).

**Warning:** checkCredentials() NOW must return true in order for authentication to be successful. In KnpUGuard, if you did NOT throw an AuthenticationException, it would pass.

Before:

```
use KnpU\Guard\Authenticator\AbstractFormLoginAuthenticator;
use KnpU\Guard\...;
// ...

class FormLoginAuthenticator extends AbstractFormLoginAuthenticator
{
    // ...

    public function checkCredentials($credentials, UserInterface $user)
    {
        // ...

        if ($password !== 'correctPassword') {
            throw new AuthenticationException();
        }

        // do nothing, allow authentication to pass
    }

    // ...
}
```

Now:

```
use Symfony\Component\Security\Guard\AbstractFormLoginAuthenticator;
use Symfony\Component\Security\Guard\...;
// ...

class FormLoginAuthenticator extends AbstractFormLoginAuthenticator
{
    // ...

    public function checkCredentials($credentials, UserInterface $user)
    {
        // ...

        if ($password !== 'correctPassword') {
            // returning anything NOT true will cause an authentication failure
            return;
            // or, you can still throw an AuthenticationException if you want to
            // throw new AuthenticationException();
        }

        // return true to make authentication successful
        return true;
    }

    // ...
}
```

### Step 5 - Yes we can test it

[](#step-5---yes-we-can-test-it)

That's it! Try it out, and then upgrade to Symfony 3 :).

- [http://symfony.com/doc/current/cookbook/upgrade/major\_version.html](http://symfony.com/doc/current/cookbook/upgrade/major_version.html)

Documentation
-------------

[](#documentation)

Find a full tutorial here:

Basic Usage
-----------

[](#basic-usage)

Check out the [Tutorial](https://knpuniversity.com/screencast/guard) for real documentation. But here's the basic idea.

Guard works by creating a single class - an **authenticator** - that handles *everything*about how you want to authenticate your users. And authenticator implements [KnpU\\Guard\\GuardAuthenticatorInterface](https://github.com/knpuniversity/KnpUGuard/blob/master/src/GuardAuthenticatorInterface.php))

Here are some real-world examples from the tutorial:

GoalCodeTutorialAuthenticate by reading an `X-TOKEN` header[ApiTokenAuthenticator.php](https://github.com/knpuniversity/guard-tutorial/blob/finished/src/AppBundle/Security/ApiTokenAuthenticator.php)[How to Authenticate via an API Token](https://knpuniversity.com/screencast/guard/api-token)Form login authentication[FormLoginAuthenticator.php](https://github.com/knpuniversity/guard-tutorial/blob/finished/src/AppBundle/Security/FormLoginAuthenticator.php)[How to Create a Login Form](https://knpuniversity.com/screencast/guard/login-form)Social Login (Facebook)[FacebookAuthenticator.php](https://github.com/knpuniversity/guard-tutorial/blob/finished/src/AppBundle/Security/FacebookAuthenticator.php)[Social Login with Facebook](https://knpuniversity.com/screencast/guard/social-login)Contributing
------------

[](#contributing)

Find a bug or a use-case that this doesn't support? [Open an Issue](https://github.com/knpuniversity/KnpUGuard/issues)so we can make things better.

License
-------

[](#license)

This library is under the MIT license. See the complete license in the LICENSE file.

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity42

Moderate usage in the ecosystem

Community20

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 87.3% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~17 days

Total

5

Last Release

3906d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/8aa57faf031af6d7f14a231d38adf1aed19844770fb5de2f82dc61aa65b45111?d=identicon)[weaverryan](/maintainers/weaverryan)

---

Top Contributors

[![weaverryan](https://avatars.githubusercontent.com/u/121003?v=4)](https://github.com/weaverryan "weaverryan (48 commits)")[![Irvyne](https://avatars.githubusercontent.com/u/1851751?v=4)](https://github.com/Irvyne "Irvyne (3 commits)")[![bocharsky-bw](https://avatars.githubusercontent.com/u/3317635?v=4)](https://github.com/bocharsky-bw "bocharsky-bw (2 commits)")[![stof](https://avatars.githubusercontent.com/u/439401?v=4)](https://github.com/stof "stof (2 commits)")

---

Tags

security

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/knpuniversity-guard/health.svg)

```
[![Health](https://phpackages.com/badges/knpuniversity-guard/health.svg)](https://phpackages.com/packages/knpuniversity-guard)
```

###  Alternatives

[phpseclib/phpseclib

PHP Secure Communications Library - Pure-PHP implementations of RSA, AES, SSH2, SFTP, X.509 etc.

5.6k434.8M1.3k](/packages/phpseclib-phpseclib)[defuse/php-encryption

Secure PHP Encryption Library

3.9k162.4M214](/packages/defuse-php-encryption)[nelmio/security-bundle

Extra security-related features for Symfony: signed/encrypted cookies, HTTPS/SSL/HSTS handling, cookie session storage, ...

68112.8M27](/packages/nelmio-security-bundle)[mews/purifier

Laravel 5/6/7/8/9/10 HtmlPurifier Package

2.0k16.7M113](/packages/mews-purifier)[robrichards/xmlseclibs

A PHP library for XML Security

41478.1M118](/packages/robrichards-xmlseclibs)[spatie/laravel-csp

Add CSP headers to the responses of a Laravel app

8569.6M19](/packages/spatie-laravel-csp)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
