Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Symfony 4 change
- composer create-project symfony/skeleton nameOfProject
- php -S localhost:8000 -t public
- php bin/console dobijamo sve komande
- symfony.sh - bundles - koristimo alies
- npr
- composer require maker
- composer req annotations
- php bin/console make:controller
- name:PostCotroller
- annotation for controller
- /**
- *Class PostCntroller
- *@package App\Controller
- *@Route("/post", name="post")
- */
- annotation for methode
- /**
- * @Route("/edit/{id}", name="edit")
- */
- public function editAction(){
- retrun new Response('Welcome to your edit action!');
- }
- to use render you must install
- composer req twig
- /**
- * @Route("/", name="post")
- */
- public function indexAction()
- {
- return $this->render('post/idex.html.twig');
- }
- *********************
- index.html.twig
- {% entends 'base.html.twig' %}
- {% block body %}
- {% endblock %}
- include bootstrap in base.index.twig
- in {% block stylesheets %} {% endblock %}
- --------------------------------------------
- for database you must installl
- composer req doctrine
- nakon toga u config folderu imamo doctrine.yaml
- u doctrine.yaml nista ne moramo da mjenjamo
- ---------------------------
- u env file imamo DATABASE_URL KOJI TREBA DA PODESIMO za nasu bazu podataka
- ------------
- php bin/console make:entity
- name:Post
- ------------------
- entity Post
- add fild
- composer req validator
- use Doctrine\ORM\Mapping as ORM;
- use Symfony\Component\Validation\Constraints as Assert;
- /**
- * @ORM\Column("type="string", name="title")
- * @var string
- */
- private $tile;
- /**
- * @ORM\Colum(type="string", name="image")
- * @Assert\NotBlank(message="Please upload an image")
- * @Assert\File(mimeType={"image/jpeg"})
- */
- private $image;
- php bin/console doctrine:schema:create
- ----------------------------
- composer req form
- php bin/console make:form
- name:PostType
- $builder
- ->add('title')
- ->add('description')
- ->add('image', FileType::class)
- ->add(
- 'save',
- SubmitType::class
- )
- ;
- PostController
- public function createAction(Request $request){
- $em = $this->getDoctrine()->getManager()
- $post = new Post();
- /**
- * @var Form
- */
- $form = $form = $this->createForm(PostType::class, $post);
- $form->handleRequest($request);
- if($form->isSubmitted() && $form->isValid()){
- /**
- * @var UploadedFile $file
- */
- $file = $post->getImage();
- $fileName = md5(uniqid()).'.'.$file->guessExtension();
- $file->move(
- $this->getParameter('images_directory'),
- $fileName
- );
- $post->setImage($fileName);
- $em->persist($post);
- $em->flush();
- return $this->redirect($this->generateUrl('postindex'));
- }
- return $this->render('post/new.html.twig',[
- 'form' => $form->createView()
- ]);
- }
- moramo definisati images_directory unutar config/services.yaml
- parameters:
- ....
- images_directory: '%kernel.project_dir%/public/uploads'
- -------------------
- new.html.twig
- {$block body %}
- {{ form(form) }}
- {% endblock %}
- ---------------
- twig.yaml
- twig:
- .....
- form_themes:
- -'bootstrap_4_layout.html.twig'
- -----------------------
- composer req asset
- public function indexAction()
- {
- $em = $this->getDctrine()->getManager();
- $repository = $em->getRepository(Post::class);
- $posts = $repository->findAll();
- return $this->render('post/index.html.twig', compact('posts')):
- }
Add Comment
Please, Sign In to add comment