Coder Social home page Coder Social logo

symfony_ex's Introduction

symfony_ex

A testing ground for Symfony tricks

  1. Building a skeleton of an application and testing it

composer create-project symfony/website-skeleton dbapp cd dbapp/public php -S 0.0.0.0:8000

  1. Connecting to mysql

Install mysql, create a demo database with a user/password. Example:

mysql
create database demo;
create user demo identified by "demo";
grant all on demo.* to demo;

Modify the file .env in the dbapp folder DATABASE_URL="mysql://demo:[email protected]:3306/demo"

php bin/console doctrine:database:create

(fails because the db exists but that's ok)

  1. Creating entities. In this example we have persons who own books (one-to-many relationship)

php bin/console make:entity

[create entity Person with an attribute called name] [create entity Book with an attribute called name]

php bin/console make:migration
php bin/console doctrine:migrations:migrate
  1. Creating relations
php bin/console make:entity

Person
[add a new attribute called books, type relation, OneToMany]
  1. Creating code for persisting persons

Put the following code in dbapp/src/Controller/PersonController.php

<?php
namespace App\Controller;
use App\Entity\Person; 
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Doctrine\Persistence\ManagerRegistry;


class PersonController extends AbstractController
{
    #[Route('/person', name: 'person')]
    public function index(): Response
    {
        return $this->render('person/index.html.twig', [
            'controller_name' => 'PersonController',
        ]);
    }

    /**
     * @Route("/newperson", name="new_person")
     */
    public function newPerson(ManagerRegistry $doctrine): Response
    {
        $entityManager = $doctrine->getManager();
        $per = new Person();
        $per->setName('John');
        $entityManager->persist($per);
        $entityManager->flush();
        return new Response('Saved new person with id '.$per->getId());
    }
}

Start the server in public (php -S 0.0.0.0:8000) and with your browser go to http://localhost:8000/newperson You should get a response "Saved new person with id 1"

  1. Creating a form and getting a name that we'll persist

In the previous example we always stored a person named John. Let's improve the code so that we can provide a name for the person that we'll store in the database.

This task needs several components: a form in which we type the name and something in the controller that can 1 Show the form 2 Get the input from the form.

The form should go to a file called templates/person/index.html.twig and it looks like this:

{% extends 'base.html.twig' %}

{% block title %}Please enter new person data{% endblock %}

{% block body %}
<form action="/newperson">
    <input type="text" name="pname" required>
    <input type="submit" value="Submit">
</form>
{% endblock %}

We should show this form when the user accesses an URL. So let's add this in the beginning of the PersonController class

class PersonController extends AbstractController
{
    /**
     * @Route("/person", name="show_person_form")
     */
    public function index(): Response
    {
        return $this->render('person/index.html.twig');
    }

Test by using "/person" at the end of the URL with your browser. It kind of works but always stores a person named John. So let's change the "newperson" function:

    /**
     * @Route("/newperson", name="new_person")
     */
    public function newPerson(ManagerRegistry $doctrine, Request $request): Response
    {
        $entityManager = $doctrine->getManager();
        //collect the information from the form
        $pname = "";
        if ($request->get('pname') !== null) {
            $pname = $request->get('pname');
        }
        if ($pname == "") { return new Response('Empty name!'); }
        $per = new Person();
        $per->setName($pname);
        $entityManager->persist($per);
        $entityManager->flush();
        return new Response('Saved new person '.$pname.' with id '.$per->getId());
    }

symfony_ex's People

Contributors

manzikki avatar myfriendappro avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.