Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentes Révision précédente | |||
| formations:symfony_init [14/12/2020 00:31] – mle-goue | formations:symfony_init [26/12/2020 15:53] (Version actuelle) – mle-goue | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| + | ====== Initialiser un projet Symfony 4 ====== | ||
| + | |||
| + | ===== Logiciels requis ===== | ||
| + | De manière obligatoire : | ||
| + | * PHP >= 7.1.3 | ||
| + | * [[https:// | ||
| + | |||
| + | <note important> | ||
| + | |||
| + | De manière facultative : | ||
| + | * [[https:// | ||
| + | |||
| + | <note tip> | ||
| + | |||
| + | De plus il faudra un serveur MySQL sur la machine (Laragon fera l' | ||
| + | |||
| + | |||
| + | |||
| + | ===== Initialisation du projet ===== | ||
| + | ==== Symfony et les dépendances de base ==== | ||
| + | Créer un nouveau dossier qui va contenir le projet, et lancer la commande suivante à l' | ||
| + | composer create-project symfony/ | ||
| + | |||
| + | ==== Installation de Webpack ==== | ||
| + | <note important> | ||
| + | Lancer la commande suivante pour installer **Webpack Encore** : | ||
| + | composer require symfony/ | ||
| + | |||
| + | Pour les dépendances front : | ||
| + | npm install --save-dev @symfony/ | ||
| + | | ||
| + | ==== Installation de FOSUserBundle ==== | ||
| + | <note warning> | ||
| + | <note important> | ||
| + | <note tip>Le module utilisé FOSUserBundle est un module couramment utilisé avec symfony pour cette utilisation.</ | ||
| + | |||
| + | === Modules nécessaires === | ||
| + | composer require swiftmailer-bundle | ||
| + | composer require symfony/ | ||
| + | composer require friendsofsymfony/ | ||
| + | |||
| + | <note important> | ||
| + | |||
| + | === Création de l' | ||
| + | <file php src/ | ||
| + | <?php | ||
| + | // src/ | ||
| + | |||
| + | namespace App\Entity; | ||
| + | |||
| + | use FOS\UserBundle\Model\User as BaseUser; | ||
| + | use Doctrine\ORM\Mapping as ORM; | ||
| + | |||
| + | /** | ||
| + | * @ORM\Entity | ||
| + | * @ORM\Table(name=" | ||
| + | */ | ||
| + | class User extends BaseUser | ||
| + | { | ||
| + | /** | ||
| + | * @ORM\Id | ||
| + | * @ORM\Column(type=" | ||
| + | * @ORM\GeneratedValue(strategy=" | ||
| + | */ | ||
| + | protected $id; | ||
| + | |||
| + | public function __construct() | ||
| + | { | ||
| + | parent:: | ||
| + | // your own logic | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Le code est ici très basique. On pourra rajouter des champs notamment avec la commande '' | ||
| + | |||
| + | === Configuration === | ||
| + | Modifier le fichier security.yaml : | ||
| + | <file yaml config/ | ||
| + | security: | ||
| + | encoders: | ||
| + | FOS\UserBundle\Model\UserInterface: | ||
| + | |||
| + | role_hierarchy: | ||
| + | ROLE_ADMIN: | ||
| + | ROLE_SUPER_ADMIN: | ||
| + | |||
| + | # https:// | ||
| + | providers: | ||
| + | fos_userbundle: | ||
| + | id: fos_user.user_provider.username | ||
| + | |||
| + | firewalls: | ||
| + | dev: | ||
| + | pattern: ^/ | ||
| + | security: false | ||
| + | main: | ||
| + | pattern: ^/ | ||
| + | form_login: | ||
| + | provider: fos_userbundle | ||
| + | csrf_token_generator: | ||
| + | |||
| + | logout: | ||
| + | anonymous: | ||
| + | |||
| + | # Easy way to control access for large sections of your site | ||
| + | # Note: Only the *first* access control that matches will be used | ||
| + | access_control: | ||
| + | - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY } | ||
| + | - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY } | ||
| + | - { path: ^/ | ||
| + | - { path: ^/admin/, role: ROLE_ADMIN } | ||
| + | </ | ||
| + | |||
| + | Ensuite il faut créer le fichier de configuration de FOSUser | ||
| + | <file yaml config/ | ||
| + | fos_user: | ||
| + | db_driver: orm # other valid values are ' | ||
| + | firewall_name: | ||
| + | user_class: App\Entity\User | ||
| + | from_email: | ||
| + | address: " | ||
| + | sender_name: | ||
| + | </ | ||
| + | |||
| + | Et enfin ajouter un paramètre au fichier framework.yaml : | ||
| + | <file yaml config/ | ||
| + | framework: | ||
| + | templating: | ||
| + | engines: [' | ||
| + | </ | ||
| + | |||
| + | === Création du schéma === | ||
| + | Après avoir éventuellement ajouté des champs à l' | ||
| + | php bin/console make: | ||
| + | php bin/console doctrine: | ||
| + | | ||
| + | === Connexion via MyCentraleAssos === | ||
| + | Pour rajouter une connexion via MyCA, il faut avoir un jeu de clés OAUTH_ID et OAUTH_SECRET. | ||
| + | |||
| + | Il faut un module pour gérer la communication via le protocol OAuth2 : | ||
| + | composer require adoy/oauth2 | ||
| + | |||
| + | On va modifier le .env pour rajouter les attributs nécessaires : | ||
| + | <file env .env> | ||
| + | OAUTH_ID= | ||
| + | OAUTH_SECRET= | ||
| + | OAUTH_BASE=https:// | ||
| + | </ | ||
| + | |||
| + | Ensuite le fichier services.yaml pour rajouter ces paramètres d' | ||
| + | <file yaml config/ | ||
| + | parameters: | ||
| + | locale: ' | ||
| + | oauth_id: ' | ||
| + | oauth_secret: | ||
| + | oauth_base: ' | ||
| + | </ | ||
| + | |||
| + | Enfin il faut créer un controller qui va gérer la connexion ! | ||
| + | |||
| + | Si vous utilisez FOSUserBundle : | ||
| + | |||
| + | <file php src/ | ||
| + | <?php | ||
| + | |||
| + | namespace App\Controller; | ||
| + | |||
| + | use App\Entity\User; | ||
| + | use FOS\UserBundle\Model\UserManagerInterface; | ||
| + | use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | ||
| + | use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
| + | use Symfony\Component\HttpFoundation\Request; | ||
| + | use Symfony\Component\HttpFoundation\Session\SessionInterface; | ||
| + | use Symfony\Component\Routing\Annotation\Route; | ||
| + | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||
| + | use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; | ||
| + | use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; | ||
| + | use Symfony\Component\Security\Http\Event\InteractiveLoginEvent; | ||
| + | |||
| + | class UserController extends AbstractController | ||
| + | { | ||
| + | /** | ||
| + | * @Route("/ | ||
| + | */ | ||
| + | public function index(Request $request, UserManagerInterface $userManager, | ||
| + | { | ||
| + | $id = $this-> | ||
| + | $secret = $this-> | ||
| + | $base = $this-> | ||
| + | |||
| + | $client = new \OAuth2\Client($id, | ||
| + | |||
| + | if(!$request-> | ||
| + | $url = $client-> | ||
| + | return $this-> | ||
| + | }else{ | ||
| + | $params = [' | ||
| + | $resp = $client-> | ||
| + | |||
| + | if(isset($resp[' | ||
| + | $info = $resp[' | ||
| + | |||
| + | $client-> | ||
| + | $client-> | ||
| + | $response = $client-> | ||
| + | $data = $response[' | ||
| + | |||
| + | $username = $data[' | ||
| + | |||
| + | $user = $userManager-> | ||
| + | if($user === null){ // Création de l' | ||
| + | $user = $userManager-> | ||
| + | $user-> | ||
| + | $user-> | ||
| + | $user-> | ||
| + | $user-> | ||
| + | $user-> | ||
| + | $user-> | ||
| + | |||
| + | |||
| + | $userManager-> | ||
| + | } | ||
| + | |||
| + | // Connexion effective de l' | ||
| + | $token = new UsernamePasswordToken($user, | ||
| + | $tokenStorage-> | ||
| + | |||
| + | $session-> | ||
| + | |||
| + | $event = new InteractiveLoginEvent($request, | ||
| + | $dispatcher-> | ||
| + | |||
| + | } | ||
| + | |||
| + | // Redirection vers l' | ||
| + | return $this-> | ||
| + | } | ||
| + | |||
| + | } | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | Si vous n' | ||
| + | |||
| + | <file php src/ | ||
| + | <?php | ||
| + | |||
| + | namespace App\Controller; | ||
| + | |||
| + | use App\Entity\User; | ||
| + | use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | ||
| + | use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
| + | use Symfony\Component\HttpFoundation\Request; | ||
| + | use Symfony\Component\HttpFoundation\Session\SessionInterface; | ||
| + | use Symfony\Component\Routing\Annotation\Route; | ||
| + | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||
| + | use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; | ||
| + | use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; | ||
| + | use Symfony\Component\Security\Http\Event\InteractiveLoginEvent; | ||
| + | use Doctrine\Common\Persistence\ObjectManager; | ||
| + | |||
| + | class UserController extends AbstractController | ||
| + | { | ||
| + | /** | ||
| + | * @Route("/ | ||
| + | */ | ||
| + | public function index(Request $request, TokenStorageInterface $tokenStorage, | ||
| + | { | ||
| + | if($this-> | ||
| + | return $this-> | ||
| + | } | ||
| + | |||
| + | |||
| + | $id = $this-> | ||
| + | $secret = $this-> | ||
| + | $base = $this-> | ||
| + | |||
| + | $client = new \OAuth2\Client($id, | ||
| + | |||
| + | if(!$request-> | ||
| + | $url = $client-> | ||
| + | return $this-> | ||
| + | }else{ | ||
| + | $params = [' | ||
| + | $resp = $client-> | ||
| + | |||
| + | if(isset($resp[' | ||
| + | $info = $resp[' | ||
| + | |||
| + | $client-> | ||
| + | $client-> | ||
| + | $response = $client-> | ||
| + | $data = $response[' | ||
| + | |||
| + | $username = $data[' | ||
| + | |||
| + | $user = $this-> | ||
| + | if($user === null){ // Création de l' | ||
| + | $user = new User; | ||
| + | $user-> | ||
| + | $user-> | ||
| + | $user-> | ||
| + | $user-> | ||
| + | $user-> | ||
| + | |||
| + | |||
| + | $manager-> | ||
| + | $manager-> | ||
| + | } | ||
| + | |||
| + | // Connexion effective de l' | ||
| + | $token = new UsernamePasswordToken($user, | ||
| + | $tokenStorage-> | ||
| + | |||
| + | $session-> | ||
| + | |||
| + | $event = new InteractiveLoginEvent($request, | ||
| + | $dispatcher-> | ||
| + | |||
| + | } | ||
| + | |||
| + | // Redirection vers l' | ||
| + | return $this-> | ||
| + | } | ||
| + | |||
| + | } | ||
| + | | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||