CSCI 4140: Tutorial 13
Symfony: PHP framework for web projects
(Incomplete) Guide of developing a guest book application with Symfony

Installing and Configuring Symfony

  1. Install the Symfony Installer: http://symfony.com/doc/current/book/installation.html#installing-the-symfony-installer
  2. Start a new Symfony application using the Symfony installer (similar to the Express application generator). Type the following command in your shell (for Linux / Mac) or command prompt (for Windows):

    symfony new guestbook

    where guestbook is the name of the application.

  3. The application will be stored on the guestbook directory. Now change the current directory to guestbook.

  4. Do you notice that there is a file called composer.json? Similar to any Node.js application which uses package.json, Composer (the package manager used by Symfony) also use a JSON file for specifying required packages.

  5. Symfony provides a convenient command-line tool. Type this command to see its usage:

    php app/console

    This command will be used a lot during development.

  6. The application is ready to run! PHP has a built-in web server for development and Symfony provides a shortcut for that. Now type the following command:

    php app/console server:run

    and open your browser and access the http://localhost:8000. You should see a welcome page of Symfony.

  7. Before doing development, you need to configure the application first. You can configure the application by modifying the configuration file app/config/parameters.yml, or by using the graphical user interface on the welcome page.

    Please configure the database for later usage.

    After this, you can terminate the server with Ctrl+C.

  8. It is time to start coding! In Symfony, all your application code is inside bundles. So let's use the command-line tool to create a new bundle called CuhkCseGuestbookBundle.

    php app/console generate:bundle

    or

    php app/console generate:bundle --namespace=CuhkCse/GuestbookBundle --format=yml

    Note that it is optional to specify arguments. If arguments are missing, it will enter interactive mode for inputting the values you want.

    Let's use CuhkCse/GuestbookBundle for the bundle namespace. Each bundle is hosted under a namespace, which begins with a vendor name (e.g., company name, project name, client name, etc.), followed by one or more optional category sub-namespaces, and it should end with the bundle name itself.

    For most of the options, you just need to keep the default values. For the configuration format, I suggest using yml which is the most simplest format (as you will see later).

    After this operation, the directory for the bundle is created at src/CuhkCse/GuestbookBundle.

Creating Pages in Symfony

Creating pages involve two steps: creating the route and implement the corresponding controller method.

Creating routes

  1. By default, the routing configuration file in a Symfony application is located at app/config/routing.yml.

    This is the first YAML file you see in Symfony! YAML stands for "YAML Ain't Markup Language". It is designed to be a human-readable data serialization format.

    Note that there is an entry called cuhk_cse_guestbook. This is automatically added when the CuhkCseGuestbookBundle is generated. It imports the routing.yml file under src/CuhkCse/GuestbookBundle/Resources/config/.

  2. In our guest book application, we are going to define two routes. One for displaying the messages, while one for submitting new messages. Replace the contents in src/CuhkCse/GuestbookBundle/Resources/config/routing.yml to add two entries:

    cuhk_cse_guestbook_homepage:
        path:     /
        defaults: { _controller: CuhkCseGuestbookBundle:Default:index }
    
    cuhk_cse_guestbook_form:
        path:     /form
        defaults: { _controller: CuhkCseGuestbookBundle:Default:form }

    Note that you can only use 4 spaces for indentation. No tabs are allowed!

Implementing controller methods

  1. As you can see in routing.yml, the value of _controller is CuhkCseGuestbookBundle:Default:index for the route at /. This refers to indexAction method in the DefaultController.php under the controller directory.
  2. Implement the indexAction and formAction in src/CuhkCse/GuestbookBundle/Controller/DefaultController.

    <?php
    
    namespace CuhkCse\GuestbookBundle\Controller;
    
    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use Symfony\Component\HttpFoundation\Response;
    
    class DefaultController extends Controller {
        public function indexAction() {
            return new Response( 'index' );
        }
    
        public function formAction() {
            return new Response( 'form' );
        }
    }

    The controller methods are very simple. It just use the Symfony object Response to construct the server response.

  3. Now you can start the server again and visit http://localhost:8000 and http://localhost:8000/form again to test whether the two new routes work.

Completing the guest book application

Sorry that I don't have time to write the remaining part...Please come to the tutorial and I will explain more. I plan to demonstrate the following functions (if time permits):

Useful commands during development