PHP code example of dyvelop / current-user-bundle

1. Go to this page and download the library: Download dyvelop/current-user-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

dyvelop / current-user-bundle example snippets


// File: app/AppKernel.php
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            new Dyvelop\CurrentUserBundle\DyvelopCurrentUserBundle(),
        );
        
        return $bundles;
    }
}

// fetch current user (the result is NULL when no one is logged in)
$user = $this->container->get('dyvelop.current_user.provider')->getUser();



namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Dyvelop\CurrentUserBundle\Annotation as Dyvelop;

class Article
{
    /**
     * @ORM\ManyToOne(targetEntity="User")
     * @ORM\JoinColumn(name="author_id", referencedColumnName="id", nullable=true)
     * @Dyvelop\CurrentUser(prePersist=true)
     */
    protected $author;
}