Starter Symfony4 app
This is a starter app from Symfony4 for easy forking for new starters.
vagrant plugin install vagrant-hostsupdater
vagrant up
You now have a web server running on http://symfony.local.
You can edit the code directly in this directory to make changes.
You can ssh into the vagrant box with vagrant ssh
.
Get to the code with cd /srv/www/app/current
.
Once inside the vagrant box you can run Symfony commands, here are some useful ones:
# Clear the Symfony cache
$ bin/console cache:clear
routes.yaml
for the index action at the /
pathtemplates/
base.html.twig
{name}
Request $request
and string $name
as parameters to the actionlastName
parameter from the $request
with a suitable default value if it doesn’t exist
$ ./bin/console doctrine
create
Post
entity in Entity/
Doctrine\ORM\Mapping as ORM
, these are the Doctrine annotations you’ll use to define the schema@ORM\Entity
annotation to the class@ORM\Column(type=<your type>)
annotation to each propertyid
field to make MySQL automatically generate it:
$ ./bin/console doctrine
update --dump-sql
$ ./bin/console doctrine
update --force
.env
file
DATABASE_URL=mysql://<username>:<password>@127.0.0.1:3306/<db name>
$ mysql -u <username> -p <db name>
show tables;
describe post;
select * from post;
PostType
class in Form/
that extends AbstractType
buildForm(FormBuilderInterface $builder, array $options)
function$bulider->add()
that exist in the Post
entity, eg.
$builder->add('title')
add()
function allows you to change the type of the form field that’s rendered
$builder->add('text', TextareaType::class);
$builder->add('sumbit', SubmitType::class)
createPost
(remember to inject the Request
)Post
, bind it to the form and handle the requsett
$post = new Post();
$form = $this->createForm(PostType::class, $post);
$form->handleRequest($request)
{{ form(form) }}
if (false === $form->isSubmitted() || false === $form->isValid()) {
return $this->>render('create-post.html.twig', ['form' => $form->createView()]);
}
PostType
class to see what you can changeIn order to save your Post
to the database when the form is submitted, you need to use the EntityManager
, so
you inject it into the constructor of your Controller
/** @var EntityManagerInterface **/
private $entityManager;
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManagerInterface;
}
Post
to the database, but only if the form has been submitted and is valid
$this->entityManager->persist($post);
$this->entityManager->flush();
return $this->redirect('create');
<ul>
{% for post in posts %}
<li>{{ post.title }}</li>
{% endfor %}
</ul>
$posts = $this->entityManager->findAll(Post::class);
Request
into your action and get the 'title'
parameter from it (with a sensible default value of ''
)QueryBuilder
in order to search the database for specific Posts
$posts = $this->entityManager->createQueryBuilder()
->select('post')
->from(Post:class, 'post')
->where('post.title = :title')
->setParameter('title', $title)
->getQuery()
->getResult()
;