Join Jon Peck for an in-depth discussion in this video Generating magazine controllers, part of Up and Running with Symfony2 for PHP (2014).
In addition to schemas and updating entities, doctrine can also generate controllers based on the structure of entities, If it doesn't already exist, doctrine will create a controller class in the right place within the bundle. That part is fairly trivial, but the creation of actions that map to the basic CRUD interactions, like "create", "read", "update", and "destroy", is very useful and provides a great starting place. Of course, routes are generated as well which describes how users can access each action. Basic forms are included for the interactive actions that require user input, and we'll explore those in a later chapter.
Finally, templates in a Twig format will be included for rendering the actual output. While the end result isn't exactly something you'd want to consider finished, it's a great starting place. Let's explore the actions in a bit more detail. The generated actions are in two groups, the default, "read-only" actions which are "index", to list all the members of an entity class, and "show", which will display the contents of a particular entity instance. The second group are the "write" actions, which are a bit more comprehensive.
Starting with "new", which shows the creation form, then the "create" action itself, which will create a new entity for the form user input. "Edit", as you can imagine, displays an editing form which is used in conjunction with "update", which actually makes the changes to the entity. Finally, "delete" will delete the entity. Note that these actions are atomic as possible. Forms are handled separately from the actual interaction from the editing. Let's generate controllers for our two entities by switching to the terminal. The "PHP app/console", "generate", "doctrine", "CRUD" will generate the controllers.
Let's take a quick look at the "help" first. So, "space", "dash-dash", "help". That's some actually fairly comprehensive documentation, and even details how the generated files use a template and how to override that template. Let's generate a controller for publications. So, type, "up", and "delete", the "help", and press "enter". The first question is to give the editing shortcut name for the editing question, and an example is given. We're going to specify the "Linda magazine bundle: publication".
By default, only the "read-only" actions are provided, "list" and "show". The "write" actions are optional, but let's include them, so type, "yes". Similar to entities, let's continue to use the annotation configuration format. The routes prefix defines the route path under which the actions can be accessed under. "/publication" is logical, so let's keep that. That's the last question. Let's verify that we're gonna create a full CRUD controller using the annotation format. Yes, "I'm sure". It's all done.
The controller, actions, forms, and templates have been created, even though it's all described as just "CRUD" and "form". Let's create the issue controller while we're at it. So, press, "up", and press "enter" again to repeat the same command. This time, we'll specify the "Linda magazine bundle: issue". Yes, we'd like to generate the "write" actions, annotation, and the route's prefix looks fine. "Confirm generation?" Yes. I like this positive call to action.
"Start using the generated code?" Right, let's do that.
- Installing Symfony
- Creating a bundle from the console
- Customizing and generating database tables
- Generating controllers
- Creating, editing, and debugging entities
- Displaying and debugging a form
- Rendering content with templates
Skill Level Intermediate
Q: When trying to access the application, I receive an error stating "This script is only accessible from localhost." How can I get around this restriction?
A: The development front controller and configuration scripts are protected by default to only allow access from the localhost. Refer to the video titled “Exploring the Symfony layout” to see how to disable this security.
Q: I specified the wrong database credentials and got an "Access denied for user" error from doctrine. How can I fix the database configuration?
<div>A: The most likely cause is a typo or misconfiguration in the Symfony parameters file, which you can find at ./symfony/app/config/parameters.yml. This file is typically generated during installation using an interactive wizard.</div><div> </div><div>Symfony requires read and write access to a MySQL database, and needs to know how to connect to the database. Therefore, you'll need to specify a username, password, port (if nonstandard), host, and database name. The credentials to your individual development environment are likely different than the ones used in the course; please use your best judgment in determining the correct values. After making a change, verify the configuration by using a web browser to navigate to /symfony/web/config.php, then click “Configure your Symfony Application online” at the bottom of the page.</div>
Q: How can I configure PHP's time zone?
<div>A: You'll need to edit PHP's configuration to specify a time zone. The <em></em><a href="http://www.lynda.com/Apache-tutorials/Up-Running-Linux-PHP-Developers/158372-2.html"><em>Up and Running with Linux for PHP</em> <em>Developers</em></a> PHP configuration can be modified by editing the custom configuration:</div><div> </div><div>sudoedit /etc/php5/mods-available/custom.ini</div><div> </div><div> If you have a different development environment, determine the location of your php.ini configuration file. Specify a date.timezone, such as: </div><div> </div><div>date.timezone = America/Los_Angeles</div><div> </div><div>Then restart the server using: </div><div> </div><div>sudo service apache2 restart</div><div> </div><div>A full list of supported timezones can be found at <a target="_blank" href="http://php.net/manual/en/timezones.php">http://php.net/manual/en/timezones.php</a>.</div><div> </div>