All posts by jorzekowsky

Symfony2 Tutorial Part 2: A Simple Blog

About this tutorial

Now we can start to create a simple blog application with our newly set up symfony2 project.

If you still have your AcmeDemoBundle you can remove it now. (http://blog.quadspot.de/wordpress/symfony2/remove-acmedemobundle-in-a-symfony2-project)

  1. First we create a New Bundle which will contain our blog. The easiest way to do this, is to use the interactive generators.
    If you simply type:

    php app/console generate:bundle

    the generator will ask for all the needed parameters.
    A quicker way is to pass all the required options directly to the generator:

    php app/console generate:bundle --namespace=Quadspot/BlogBundle --dir=src --structure --format=yml --no-interaction
    

    You can use any format you like. I personaly prefer yml.

    This will generate our BlogBundle in src/Quadspot/BlogBundle.

    Hint: If you want to know more about the options you can use the –help option

    php app/console generate:bundle --help

    The generator also registered new Bundle in app/AppKernel.php and created the required routes in app/config/routing.yml.

    Hint: The new generated Bundle comes with a simple default application which is already fully functional. You can access it under http://symfony.yourhostname/app_dev.php/hello/symfony Look into app/config/routing.yml, src/Quadspot/BlogBundle/Controller/DefaultControler.php, src/Quadspot/BlogBundle/Resources/views/Default/index.html.twig and if you used a format other than annotations src/Quadspot/BlogBundle/Resources/config/routing.xml, src/Quadspot/BlogBundle/Resources/config/routing.php or src/Quadspot/BlogBundle/Resources/config/routing.yml if you want to learn more about it.

  2. Now we use the generator to create our entity:
    php app/console doctrine:generate:entity --entity=QuadspotBlogBundle:Post --fields="title:string(255) created_at:datetime updated_at:datetime body:text" --format=yml --no-interaction
    

    This command will have created two files: src/Quadspot/BlogBundle/Entity/Post.php

    <?php //src/Quadspot/BlogBundle/Entity/Post.php
    namespace Quadspot\BlogBundle\Entity;
    
    use Doctrine\ORM\Mapping as ORM;
    
    /**
     * Post
     */
    class Post
    {
        /**
         * @var integer
         */
        private $id;
    
        /**
         * @var string
         */
        private $title;
    
        /**
         * @var \DateTime
         */
        private $created_at;
    
        /**
         * @var \DateTime
         */
        private $updated_at;
    
        /**
         * @var string
         */
        private $body;
    
    
        /**
         * Get id
         *
         * @return integer 
         */
        public function getId()
        {
            return $this->id;
        }
    
        /**
         * Set title
         *
         * @param string $title
         * @return Post
         */
        public function setTitle($title)
        {
            $this->title = $title;
    
            return $this;
        }
    
        /**
         * Get title
         *
         * @return string 
         */
        public function getTitle()
        {
            return $this->title;
        }
    
        /**
         * Set created_at
         *
         * @param \DateTime $createdAt
         * @return Post
         */
        public function setCreatedAt($createdAt)
        {
            $this->created_at = $createdAt;
    
            return $this;
        }
    
        /**
         * Get created_at
         *
         * @return \DateTime 
         */
        public function getCreatedAt()
        {
            return $this->created_at;
        }
    
        /**
         * Set updated_at
         *
         * @param \DateTime $updatedAt
         * @return Post
         */
        public function setUpdatedAt($updatedAt)
        {
            $this->updated_at = $updatedAt;
    
            return $this;
        }
    
        /**
         * Get updated_at
         *
         * @return \DateTime 
         */
        public function getUpdatedAt()
        {
            return $this->updated_at;
        }
    
        /**
         * Set body
         *
         * @param string $body
         * @return Post
         */
        public function setBody($body)
        {
            $this->body = $body;
    
            return $this;
        }
    
        /**
         * Get body
         *
         * @return string 
         */
        public function getBody()
        {
            return $this->body;
        }
    }
    

    and src/Quadspot/BlogBundle/Resources/config/doctrine/Post.orm.yml

    #src/Quadspot/BlogBundle/Resources/config/doctrine/Post.orm.yml
    Quadspot\BlogBundle\Entity\Post:
        type: entity
        table: null
        id:
            id:
                type: integer
                id: true
                generator:
                    strategy: AUTO
        fields:
            title:
                type: string
                length: '255'
            created_at:
                type: datetime
                length: null
            updated_at:
                type: datetime
                length: null
            body:
                type: text
                length: null
        lifecycleCallbacks: {  }
    

    Before we create our database schema we will change the name of the Post table in the database from Post to quadspot_blogbundle_post.
    Simply edit the src/Quadspot/BlogBundle/Resources/config/doctrine/Post.orm.yml file so that it looks like this:

    #src/Quadspot/BlogBundle/Resources/config/doctrine/Post.orm.yml
    Quadspot\BlogBundle\Entity\Post:
        type: entity
        table: quadspot_blogbundle_post
        id:
            id:
                type: integer
                id: true
                generator:
                    strategy: AUTO
        fields:
            title:
                type: string
                length: '255'
            created_at:
                type: datetime
                length: null
            updated_at:
                type: datetime
                length: null
            body:
                type: text
                length: null
        lifecycleCallbacks: {  }
    

    Now we can create our database schema:

    php app/console doctrine:schema:create

    Hint: If your haven’t yet created your database you can do this with:

    php app/console doctrine:database:create
  3. Next we wil create a new controler and templates do retrieve the dada from the database. We use the generator functionality of symfony2 again.
    php app/console generate:doctrine:crud --entity=QuadspotBlogBundle:Post --format=yml --no-interaction

    We just have to manually add the new routing configuration to our routing file src/Quadspot/BlogBundle/Resources/config/routing.yml.

    #src/Quadspot/BlogBundle/Resources/config/routing.yml
    QuadspotBlogBundle_post:
        resource: "@QuadspotBlogBundle/Resources/config/routing/post.yml"
        prefix:   /
    

    Hint: We changed the prefix to /.

  4. We can now remove the DefaultController and its Templates.
    Delete src/Quadspot/BlogBundle/Controller/DefaultController.php and src/Quadspot/BlogBundle/Resources/views/Default/index.html.twig
  5. Now we just need some entries in the database to be displayed in our blog. Open a mysql console:
    mysql -usymfony -ppa$$word symfony
    INSERT INTO `symfony`.`quadspot_blogbundle_post` (`id` ,`title` ,`created_at` ,`updated_at` ,`body`) VALUES ('1' , 'Lorem Ipsum', '2014-09-22 10:30:00', '2014-09-22 10:30:00', 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam');
    INSERT INTO `symfony`.`quadspot_blogbundle_post` (`id` ,`title` ,`created_at` ,`updated_at` ,`body`) VALUES ('2' , 'Lorem Ipsum 2', '2014-09-22 10:35:00', '2014-09-22 10:35:00', 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut');

And that’s it for now. In the next part we will create our backend so that we can add new posts and edit and delete existing ones.

Linux Mint 17

Was zu tun ist nach der Installation:

  • Home dir verschieben
  • .gitconfig, .openvpn, .thunderbird, .ssh ins neue home dir kopieren.

Pakete:

apt update && apt upgrade -y && apt install ssh nano mc vim enigmail ntp virt-manager chromium-browser openvpn diodon -y

LaTeX:

apt install rubber texlive texlive-lang-german texlive-latex-extra latex-beamer -y

Apache + PHP:

apt install acl apache2 apache2-mpm-prefork curl mysql-server php-apc php5 php5-cli php5-intl php5-sqlite php5-xdebug php5-xsl phpmyadmin -y

Oracle Java:

sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java7-installer

Kram für Oracle instant client und Android SDK

apt install build-essential
apt install php5-dev php-pear libaio1
apt install ia32-libs

Bash config:

echo 'export HISTCONTROL=ignoreboth:erasedups' >> ~/.bashrc

Symfony2 Tutorial Part 1: Project Setup

About this tutorial:

In this tutorial I will show you how to setup a new symfony2 project on a local webserver. I use Linux Mint 17 with the following packages installed:

apache2 apache2-mpm-prefork curl mysql-server php-apc php5 php5-cli php5-intl php5-sqlite php5-xdebug php5-xsl phpmyadmin

Lets get started.

Install symfony2

  1. Install composer:
    http://getcomposer.org/download/
    I use the following command as root:

    curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin
  2. Create a MySQL database:
    On your MySQL console:

    CREATE DATABASE symfony;
    GRANT ALL ON symfony.* TO symfony@'localhost' IDENTIFIED BY 'pa$$word';
    
  3. Get symfony2:
    Open a terminal and type

    composer.phar create-project symfony/framework-standard-edition /your/symfony/dir '2.5.*'

    HINT: You can replace '2.5.*' with your preferred symfony2 version. For example use '2.3.*' if you want to use the LTS version.

  4. Configure ACL:
    How to install and configure acl: http://wiki.ubuntuusers.de/ACL
    Open a terminal and type in the following commands.

    cd /your/symfony/dir
    HTTPDUSER=`ps aux | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | head -1 | cut -d\  -f1`
    sudo setfacl -R -m u:"$HTTPDUSER":rwX -m u:`whoami`:rwX app/cache app/logs
    sudo setfacl -dR -m u:"$HTTPDUSER":rwX -m u:`whoami`:rwX app/cache app/logs
    

Setup your webserver

  1. Create /etc/apache2/sites-available/symfony.conf as root with the following content.
    <VirtualHost *:80>
    	ServerName symfony
            ServerAlias symfony.yourhostname
    
    	ServerAdmin webmaster@localhost
    	DocumentRoot /your/symfony/dir/web
    
    	<Directory />
    		DirectoryIndex app.php
    		Options FollowSymLinks
    		AllowOverride All
    		Require all granted
    	</Directory>
    	<Directory "/your/symfony/dir/web">
    		Options Indexes FollowSymLinks MultiViews
    		AllowOverride All
    		Require all granted
    	</Directory>
    
    	ErrorLog ${APACHE_LOG_DIR}/symfony_error.log
    	CustomLog ${APACHE_LOG_DIR}/symfony_access.log combined
    
    </VirtualHost>
    
    
  2. run
    sudo a2ensite symfony

    and

    sudo service apache2 reload
  3. edit /etc/hosts and add:
    127.0.0.1 symfony.yourhostname symfony

HINT: The apache mod rewrite is not enabled by default. You enable it with:

sudo a2enmod rewrite
sudo service apache2 restart

HINT: you can check if you have configured your server correctly with:

cd /your/symfony/dir
php app/check.php

In most cases the date.timezone parameter in both /etc/php5/apache2/php.ini and /etc/php5/cli/php.ini is not set. In my case it’s: date.timezone = "Europe/Berlin".
You can find a list of different timezones here: http://php.net/manual/en/timezones.php

Access your symfony2 application

Thats it. You can now access your symfony2 application (development) via
http://symfony.yourhostname/app_dev.php
And the production environment via
http://symfony.yourhostname

At this point you will get an 404 error because you don’t have any content to be shown yet. Don’t worry we will add something in the next part.