You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Service - A tool or set of tools (like a Mailer or an API Library)
Route - Links a URL to code
Controller - The above code resides herein; routes that belong to eachother like lists for entities and CRUD for those entities are bundled in their own controller.
View - How is the data presented to the user. This can be ANYTHING! HTML, XML, JSON, a File Download etc.
console
The console is found in the root of the project (root)/bin/console. Older versions had (root)/symfony to do the same.
On Windows use php bin/console, while on Mac or Linux use ./bin/console.
command
purpose
make:entity
Create a new entity, or add fields to an existing entity
make:entity --regenerate
Add missing getters & setters
make:entity --regenerate --overwrite
REWRITE ALL getters & setters - your changes to the existing getters/setters is lost
make:form
Generate form code. When created, use as demonstrated in example 3
make:controller
Create a new controller
make:command
Create a new CLI-controller
make:migration
Process changes to entities and put them in a migration file
doctrine:migrations:migrate
Actually make the changes in the database
doctrine:schema:update --force
Re-do the database structure based on the current state of all entities
Mark for saving, often this is already done by find* methods
flush()
clear cache???? effect is actually saving the data to the database
Example 2: Saving an entity
# Example entity: \App\Entity\Book .useApp\Entity\Book;
classBooksControllerextendsAbstractController {
/** * @route("/book/{book_id}/mark-read", name="mark_book_read") */publicfunctionmarkBookRead($book_id) {
$entityManager = $this->getDoctrine()->getManager();
$bookRepository = $entityManager->getRepository(Book::class); // do not type '\App\Entity\Book'. You should be able to guess why.$book = $bookRepository->find($book_id);
$book->setIsRead(true);
// $book->save(); // symfony 1 method, no longer exists$entityManager->persist($book);
$entityManager->flush();
}
}
Example 3: using forms, created by ./bin/console make:form
$form = $this->createForm( TheFormType::class, $anEntity ); // load the form$form->handleRequest($request); // process a POSTif ( $form->isSubmitted() && $form->isValid() ) { // is actual POST, and validates OK$entityManager->persist( $anEntity );
$entityManager->flush();
}
$html = $form->createView();
Example 4: using forms, created by code
$form = $this->createFormBuilder( $anEntity )
->add( 'title', TextType::class )
->add( 'ISBN', TextType::class )
->add( 'isRead', CheckboxType::class )
->add( 'save', SubmitType::class, [ 'label' => 'Save' ] )
->getForm(); ); // load the form$form->handleRequest($request); // process a POSTif ( $form->isSubmitted() && $form->isValid() ) { // is actual POST, and validates OK$entityManager->persist( $anEntity );
$entityManager->flush();
}
$formObject = $form->createView();