git clone https://github.com/symfony/symfony-standard.git symfony
cd symfony
php bin/vendors.php
git submodule add -f git://github.com/FriendsOfSymfony/UserBundle.git vendor/bundles/FOS/UserBundle
$loader->registerNamespaces(array(<br> 'FOS' => __DIR__. '/../vendor/bundles' ,<br> // your other namespaces <br>); <br>
public function registerBundles()<br>{<br> return array(<br> // ... <br> new FOS\UserBundle\FOSUserBundle(),<br> // ... <br> );<br>} <br>
security:<br> providers:<br> fos_userbundle:<br> id: fos_user.user_manager <br>
firewalls:<br> main:<br> pattern: .*<br> form_login:<br> provider: fos_userbundle<br> login_path: /login<br> use_forward: false<br> check_path: /login_check<br> failure_path: null<br> logout: true<br> anonymous: true<br>access_control:<br> - { path: /post/new, role: ROLE_USER } <br> - { path: /.*, roles: IS_AUTHENTICATED_ANONYMOUSLY} <br>
doctrine:<br> orm:<br> mappings:<br> FOSUserBundle: ~<br> AcmeDemoBundle: ~<br> # your other bundles<br>
<br>fos_user:<br> db_driver: orm<br> firewall_name: main<br> class:<br> model:<br> user: Acme\DemoBundle\Entity\User <br>
framework:<br> translator: { fallback: en } <br>
<?php<br><br> namespace Acme\DemoBundle\Entity;<br><br>use Doctrine\ORM\Mapping as ORM;<br><br> /** <br> * <br> * @ORM\Entity <br> */ <br> class Post<br>{<br> /** <br> * @var integer $id <br> * <br> * @ORM\Id <br> * @ORM\Column(name="id", type="integer") <br> * @ORM\GeneratedValue(strategy="AUTO") <br> */ <br> private $id;<br><br> /** <br> * @ORM\ManyToOne(targetEntity="User", inversedBy="posts") <br> * @ORM\JoinColumn(name="user_id", referencedColumnName="id") <br> */ <br> public $user;<br><br> /** <br> * @var string $title <br> * <br> * @ORM\Column(name="title", type="string", length=255) <br> */ <br> public $title;<br><br> /** <br> * @var text $description <br> * <br> * @ORM\Column(name="description", type="text") <br> */ <br> public $description;<br>} <br>
<?php<br> namespace Acme\DemoBundle\Entity;<br>use FOS\UserBundle\Entity\User as BaseUser;<br>use Doctrine\ORM\Mapping as ORM;<br><br> /** <br> * @ORM\Entity <br> * @ORM\Table(name="fos_user") <br> */ <br> class User extends BaseUser<br>{<br> /** <br> * @ORM\Id <br> * @ORM\Column(type="integer") <br> * @ORM\generatedValue(strategy="AUTO") <br> */ <br> protected $id;<br> <br> /** <br> * @ORM\OneToMany(targetEntity="Post", mappedBy="fos_user") <br> */ <br> public $posts;<br><br> public function __construct()<br> {<br> parent::__construct();<br> // your own logic <br> }<br>} <br>
php app/console doctrine:schema:create
<?php<br> namespace Acme\DemoBundle\Controller;<br><br>use Symfony\Bundle\FrameworkBundle\Controller\Controller;<br>use Acme\DemoBundle\Entity\Post;<br>use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;<br><br> class PostController extends Controller<br>{<br> /** <br> * @Template() <br> */ <br> public function indexAction()<br> {<br> $em = $ this -> get ( 'doctrine' )->getEntityManager();<br> $posts = $em->getRepository( 'AcmeDemoBundle:Post' )->findAll();<br> return array( 'posts' => $posts);<br> }<br> <br> /** <br> * @Template() <br> */ <br> public function newAction()<br> {<br> $post = new Post();<br> // <br> $post->user = $ this -> get ( 'security.context' )->getToken()->getUser();<br> // , , <br> $form = $ this -> get ( 'form.factory' )<br> ->createBuilder( 'form' , $post)<br> ->add( 'title' , 'text' )<br> ->add( 'description' , 'textarea' )<br> ->getForm();<br> $request = $ this -> get ( 'request' );<br> if ($request->getMethod() == 'POST' ) {<br> $form->bindRequest($request);<br> if ($form->isValid()) {<br> $em = $ this -> get ( 'doctrine' )->getEntityManager();<br> $em->persist($post);<br> $em->flush();<br> return $ this ->redirect($ this ->generateUrl( 'post_index' ));<br> }<br> }<br> return array( 'form' => $form->createView());<br> }<br>} <br>
return array('posts' => $posts);
return $this->render('AcmeDemoBundle:Post:index.html.twig', array('posts' => $posts));
{% extends 'AcmeDemoBundle::layout.html.twig' %}<br><br>{% block content %}<br><br>{% if is_granted('ROLE_USER') %}<br> < div >< a href ="{{ path(" post_new ") }}" > New Post </ a ></ div > <br>{% endif %}<br><br>{% for post in posts %}<br> < h2 > {{ post.title}} </ h2 > <br> < p > {{ post.description }} </ p > <br> < div > by {{ post.user.username }} </ div > <br>{% else %}<br> .<br>{% endfor %}<br><br>{% endblock %} <br>
{% extends 'AcmeDemoBundle::layout.html.twig' %}<br><br>{% block content %}<br> < h1 > New Post </ h1 > <br> <br> < form action ="{{ path(" post_new ") }}" method ="post" > <br> {{ form_widget(form) }}<br><br> < input type ="submit" class ="symfony-button-grey" value ="Create" /> <br> </ form > <br>{% endblock %} <br>
post_index:<br> pattern: /post<br> defaults: { _controller: AcmeDemoBundle:Post:index }<br><br>post_new:<br> pattern: /post/new<br> defaults: { _controller: AcmeDemoBundle:Post:new }<br><br>fos_user_security:<br> resource: "@FOSUserBundle/Resources/config/routing/security.xml"<br><br>fos_user_user:<br> resource: "@FOSUserBundle/Resources/config/routing/user.xml"<br> prefix: /user <br><br> * This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/120645/
All Articles