djuro95

Symfony4Changes

Mar 4th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. Symfony 4 change
  2.  
  3. composer create-project symfony/skeleton nameOfProject
  4.  
  5. php -S localhost:8000 -t public
  6.  
  7.  
  8. php bin/console dobijamo sve komande
  9.  
  10.  
  11. symfony.sh - bundles - koristimo alies
  12. npr
  13. composer require maker
  14.  
  15. composer req annotations
  16.  
  17. php bin/console make:controller
  18. name:PostCotroller
  19.  
  20.  
  21. annotation for controller
  22. /**
  23. *Class PostCntroller
  24. *@package App\Controller
  25. *@Route("/post", name="post")
  26. */
  27.  
  28.  
  29. annotation for methode
  30. /**
  31. * @Route("/edit/{id}", name="edit")
  32. */
  33. public function editAction(){
  34. retrun new Response('Welcome to your edit action!');
  35. }
  36.  
  37.  
  38. to use render you must install
  39. composer req twig
  40.  
  41.  
  42.  
  43.  
  44. /**
  45. * @Route("/", name="post")
  46. */
  47. public function indexAction()
  48. {
  49. return $this->render('post/idex.html.twig');
  50. }
  51.  
  52.  
  53.  
  54. *********************
  55. index.html.twig
  56.  
  57. {% entends 'base.html.twig' %}
  58.  
  59. {% block body %}
  60.  
  61. {% endblock %}
  62.  
  63. include bootstrap in base.index.twig
  64. in {% block stylesheets %} {% endblock %}
  65.  
  66. --------------------------------------------
  67.  
  68. for database you must installl
  69.  
  70. composer req doctrine
  71.  
  72. nakon toga u config folderu imamo doctrine.yaml
  73.  
  74. u doctrine.yaml nista ne moramo da mjenjamo
  75.  
  76. ---------------------------
  77.  
  78. u env file imamo DATABASE_URL KOJI TREBA DA PODESIMO za nasu bazu podataka
  79.  
  80. ------------
  81.  
  82. php bin/console make:entity
  83. name:Post
  84.  
  85. ------------------
  86.  
  87. entity Post
  88.  
  89. add fild
  90.  
  91. composer req validator
  92.  
  93. use Doctrine\ORM\Mapping as ORM;
  94. use Symfony\Component\Validation\Constraints as Assert;
  95.  
  96.  
  97. /**
  98. * @ORM\Column("type="string", name="title")
  99. * @var string
  100. */
  101. private $tile;
  102.  
  103.  
  104. /**
  105. * @ORM\Colum(type="string", name="image")
  106. * @Assert\NotBlank(message="Please upload an image")
  107. * @Assert\File(mimeType={"image/jpeg"})
  108. */
  109. private $image;
  110.  
  111. php bin/console doctrine:schema:create
  112.  
  113. ----------------------------
  114.  
  115. composer req form
  116.  
  117. php bin/console make:form
  118. name:PostType
  119.  
  120.  
  121. $builder
  122. ->add('title')
  123. ->add('description')
  124. ->add('image', FileType::class)
  125. ->add(
  126. 'save',
  127. SubmitType::class
  128. )
  129. ;
  130.  
  131.  
  132. PostController
  133.  
  134. public function createAction(Request $request){
  135. $em = $this->getDoctrine()->getManager()
  136. $post = new Post();
  137.  
  138. /**
  139. * @var Form
  140. */
  141.  
  142. $form = $form = $this->createForm(PostType::class, $post);
  143. $form->handleRequest($request);
  144.  
  145. if($form->isSubmitted() && $form->isValid()){
  146. /**
  147. * @var UploadedFile $file
  148. */
  149.  
  150. $file = $post->getImage();
  151.  
  152. $fileName = md5(uniqid()).'.'.$file->guessExtension();
  153.  
  154. $file->move(
  155. $this->getParameter('images_directory'),
  156. $fileName
  157. );
  158.  
  159. $post->setImage($fileName);
  160. $em->persist($post);
  161. $em->flush();
  162.  
  163. return $this->redirect($this->generateUrl('postindex'));
  164.  
  165. }
  166.  
  167. return $this->render('post/new.html.twig',[
  168. 'form' => $form->createView()
  169. ]);
  170. }
  171.  
  172.  
  173.  
  174. moramo definisati images_directory unutar config/services.yaml
  175. parameters:
  176. ....
  177. images_directory: '%kernel.project_dir%/public/uploads'
  178.  
  179.  
  180. -------------------
  181.  
  182. new.html.twig
  183.  
  184. {$block body %}
  185. {{ form(form) }}
  186. {% endblock %}
  187.  
  188.  
  189. ---------------
  190.  
  191. twig.yaml
  192.  
  193. twig:
  194. .....
  195. form_themes:
  196. -'bootstrap_4_layout.html.twig'
  197.  
  198. -----------------------
  199. composer req asset
  200.  
  201. public function indexAction()
  202. {
  203. $em = $this->getDctrine()->getManager();
  204. $repository = $em->getRepository(Post::class);
  205. $posts = $repository->findAll();
  206.  
  207. return $this->render('post/index.html.twig', compact('posts')):
  208.  
  209. }
Add Comment
Please, Sign In to add comment