Create a Web application with Symfony2 Framework - Part 1

In this article, I'm going to explain about how to create a Web application with Symfony2 Framework. In my previous article, I have explained about how to "Installing and Configuring Symfony2 framework on Linux Ubuntu OS". In this post we are going to learn the basics of create a web application with Symfony2 Framework.

Create a "Hello World" Web application with Symfony2 Framework:

The implementation of "Hello World" project proved to be relatively simple.
  1. A task consists of a database row and has a name or title.
  2. A task can be assigned a priority.
  3. A task has a creator date.
  4. A task can be marked as completed.
The application is extented to the following actions.
  • Index : All tasks are displayed and sorted by priority.
  • add : In this action the task is created. After successful validation, the user is returned to the [index] Action.
  • edit : In this action, an existing task is edited. After successful validation, the user is returned to the [index] Action.
  • delete : Allows an existing task will be deleted.
  • markAsDone : Allows an existing task is marked as completed.
  • marksAsUndone : Allows an existing task is marked as completed.
Working in XAMPP environment:

To create a Web application with Symfony2 framework, we need to install XAMPP server that is under http://www.apachefriends.org/de/xampp.html. and to download the Symfony2 Framework Click Here.

XAMPP is a development environment it needs to be set-up a new named-vhost: www.testsmfony.local (this is valid for project name). 

My new named-vhost is : 

<VirtualHost *:80> DocumentRoot "C :/ path / to / project / web" ServerName testsmfony.local ServerAlias ​​www.testsmfony.local <Directory "C :/ path / to / project / web" > Order Allow, Deny Allow from all AllowOverride all </ Directory> </ VirtualHost>

To note here is that the /web directory in Symfony2 folder is specified as the destination. Is a 403 error appears, the xampp apache errors.log file to crawl.

Thus the named vhost also can be used, even in the hosts file of the Windows domain name are marked as local address:

Www.testsmfony.local 127.0.0.1 
127.0.0.1 testsmfony.local

After restarting the browser and the server XAMPP, now the changes should have been taken.

If you call now URL: 

http://www.testsmfony.local/app_dev.php/demo/hello/User% 20and% 20Scandio% 20Tutorial 

on should receive a Proket the example, which is automatically created by Symfony2.

"Hello User Scandio Tutorial!"

You get this message, you have done it, and has a running Symfony2 installed.

Console Configuration

To enjoy the full functionality of Symfony2, you must have the PHP CLI environment in the Windows environment variables are entered. (If you have not registered before then). This is not complicated, the path when calling the PHP CLI can be specified.

To edit the PATH environment variable on Windows 7, you will navigate to: Start -> Control Panel -> System and Security -> System -> Advanced System Settings -> Advanced -> Environment Variables -> System Variables -> Path . then "Edit ..." and click on the end of the string to specify the path to the current PHP CLI. In my example, this is: "C:\xampp\php". There is the php.exe.

Configuration of the Symfony2 project:

First, the connection to the MySQL database is configured. 

For this purpose we consider the  /app/config/parameters.ini File: 

database_driver = pdo_mysql 
DATABASE_HOST = localhost 
database_name = scandio_project_testsmfony 
database_user = root 
database_password =

The configurations should here be adapted to the current system. The database is ONLY for testing purposes as root reachable.

Unfortunately, there is an unprecedented bugs fixed bug in Doctrine 2 to thank for that one in the Doctrine Configs not specify MySQL COLLATE can. This is the default COLLATE use the current database. That means in the default configuration of XAMPP LATIN_1 ... Basically here utf8_general_ci should ALWAYS be used.

To avoid the error, use the following SQL:

CREATE  DATABASE  scandio_project_testsmfony ` DEFAULT  CHARACTER  SET  utf8 COLLATE  utf8_general_ci;



Steps to create Virtual Hosts for Drupal on Linux Ubuntu

In this article, I'm going to explain about, How to create virtual Hosts for Drupal On Linux Ubuntu. By Using virtual host settings, we can access site as a domain like test.sitename.com instead of access like localhost/sitename. Here are the steps to create a virtual host for Drupal on Linux Ubuntu.

Step 1:

Assume your Drupal codes placed at /var/www/testDrupal

Step 2:

Access your Drupal site by using http://localhost/testDrupal

Step 3:

If you want to access the same site using test.testDrupal.com to solve path issues and wysiywig editor images, and files link issues.

Step 4:

Open the below file in your editor.

/etc/apache2/sites-enabled/vhosts.conf

Step 5:

Edit the file and add the below code in your vhosts.conf file.

<VirtualHost *:80>
ServerName test.testDrupal.com
ServerAlias www.test.testDrupal.com
DocumentRoot /var/www/testDrupal
</VirtualHost>

Step 6:

Open the file /etc/hosts in your editor and add the below code.

127.0.0.1 test.testDrupal.com

Step 7:

Restart your apache server by running below command using console.

sudo /etc/init.d/apache2 restart

Step 8:

Now you can access the site using http://test.testDrupal.com to view drupal instance in direct domain.

Magento - Steps to add Custom Tabs to the Product Admin

In this article, we are going to discuss about How to add custom table to the magento product admin. In Magento, it is possible to add new attributes to each product models and edit the values for these attributes on the Product Edit Page. This article provide a facility for processing that data when the user hits the Save button.

Step 1:
Create the extension's setup file, which loads the extension into Magento. Add the below code in app/etc/modules/Phpcmsframework_Customtabs.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Phpcmsframework_Customtabs>
            <active>true</active>
            <codePool>local</codePool>
        </Phpcmsframework_Customtabs>
    </modules>
</config>

This file will register the extension in Magento, telling it to look in the app/code/local/Phpcmsframework/Customtabs/ directory for the extension's code.

Step 2:

The next file we make is the extension's config file. This file is a more detailed setup file, containing information about the extension's classes, layout files and everything else we need to make it work.

Notice the events section of this file (below)? To save the product data, we listen for an event that is triggered when ever a product is saved in the Magento Admin. This allows us to access the custom data we have created in the tab and process/save it.

Add the below code in app/code/local/Phpcmsframework/Customtabs/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Phpcmsframework_CustomTabs>
            <version>0.1.0</version>
        </Phpcmsframework_CustomTabs>
    </modules>
    <global>
        <blocks>
            <customtabs>
                <class>Phpcmsframework_Customtabs_Block</class>
            </customtabs>
        </blocks>
        <models>
            <customtabs>
                <class>Phpcmsframework_Customtabs_Model</class>
            </customtabs>
        </models>
    </global>
    <adminhtml>
        <layout>
            <updates>
                <customtabs>
                    <file>customtabs.xml</file>
                </customtabs>
            </updates>
        </layout>
        <events>
            <catalog_product_save_after>
                <observers>
                    <Phpcmsframework_save_product_data>
                        <type>singleton</type>
                        <class>customtabs/observer</class>
                        <method>saveProductTabData</method>
                    </Phpcmsframework_save_product_data>
                </observers>
            </catalog_product_save_after>
        </events>
    </adminhtml>
</config>

Step 3:
Add the below code in app/code/local/Phpcmsframework/Customtabs/Block/Adminhtml/Catalog/Product/Tab.php


<?php

class Phpcmsframework_Customtabs_Block_Adminhtml_Catalog_Product_Tab 
extends Mage_Adminhtml_Block_Template
implements Mage_Adminhtml_Block_Widget_Tab_Interface {

    /**
     * Set the template for the block
     *
     */
    public function _construct()
    {
        parent::_construct();
         
        $this->setTemplate('customtabs/catalog/product/tab.phtml');
    }
     
    /**
     * Retrieve the label used for the tab relating to this block
     *
     * @return string
     */
    public function getTabLabel()
    {
        return $this->__('My Custom Tab');
    }
     
    /**
     * Retrieve the title used by this tab
     *
     * @return string
     */
    public function getTabTitle()
    {
        return $this->__('Click here to view your custom tab content');
    }
     
    /**
     * Determines whether to display the tab
     * Add logic here to decide whether you want the tab to display
     *
     * @return bool
     */
    public function canShowTab()
    {
        return true;
    }
     
    /**
     * Stops the tab being hidden
     *
     * @return bool
     */
    public function isHidden()
    {
        return false;
    }

    /**
     * AJAX TAB's
     * If you want to use an AJAX tab, uncomment the following functions
     * Please note that you will need to setup a controller to recieve
     * the tab content request
     *
     */
    /**
     * Retrieve the class name of the tab
     * Return 'ajax' here if you want the tab to be loaded via Ajax
     *
     * return string
     */
#   public function getTabClass()
#   {
#       return 'my-custom-tab';
#   }

    /**
     * Determine whether to generate content on load or via AJAX
     * If true, the tab's content won't be loaded until the tab is clicked
     * You will need to setup a controller to handle the tab request
     *
     * @return bool
     */
#   public function getSkipGenerateContent()
#   {
#       return false;
#   }

    /**
     * Retrieve the URL used to load the tab content
     * Return the URL here used to load the content by Ajax 
     * see self::getSkipGenerateContent & self::getTabClass
     *
     * @return string
     */
#   public function getTabUrl()
#   {
#       return null;
#   }
 }

Step 4:
This is the layout file for the Adminhtml section of the extension. In here we will include the tab on the Magento Product edit page.


Add the below xml code in, app/design/adminhtml/default/default/layout/customtabs.xml

<?xml version="1.0"?>
<layout>
    <adminhtml_catalog_product_edit>
        <reference name="product_tabs">
            <action method="addTab">
                <name>my_custom_tab</name>
                <block>customtabs/adminhtml_catalog_product_tab</block>
            </action>
        </reference>
    </adminhtml_catalog_product_edit>
</layout>

Step 5:
This file is quite simple but without it, nothing will show on the product edit page. The last thing to do before we can see our tab in action is to create our new template file.

Add the below code in, app/design/adminhtml/default/default/template/customtabs/catalog/product/tab.phtml


<?php
/**
 * Custom tab template
 */
?>
<div class="input-field">
 <label for="custom_field">Custom Field</label>
 <input type="text" class="input-text" name="custom_field" id="custom_field" />
</div>

Step 6:
Testing Our Custom Magento Admin Tab

Now that we have the code in place, let's refresh our cache, go to a product edit page and see the tab in action!

Did it work? If it didn't work, check through your XML again as a slight error in any of that will stop everything working.

Now that we have our tab, let's take a look at how to process the data once the user hits the 'Save' button.

Step 7:
Saving our Custom Tab Data

To access the data during the saving process, we have hooked into an event called catalog_product_save_after (see config.xml above). This event is triggered each time a product model is saved. As we have declared our observer for this event inside the adminhtml block, we will only trigger our code when a product model is saved from the Magento Admin.

app/code/local/Phpcmsframework/Customtabs/Model/Observer.php

<?php
  
class Phpcmsframework_Customtabs_Model_Observer
{
    /**
     * Flag to stop observer executing more than once
     *
     * @var static bool
     */
    static protected $_singletonFlag = false;

    /**
     * This method will run when the product is saved from the Magento Admin
     * Use this function to update the product model, process the 
     * data or anything you like
     *
     * @param Varien_Event_Observer $observer
     */
    public function saveProductTabData(Varien_Event_Observer $observer)
    {
        if (!self::$_singletonFlag) {
            self::$_singletonFlag = true;
             
            $product = $observer->getEvent()->getProduct();
         
            try {
                /**
                 * Perform any actions you want here
                 *
                 */
                $customFieldValue =  $this->_getRequest()->getPost('custom_field');

                /**
                 * Uncomment the line below to save the product
                 *
                 */
                //$product->save();
            }
            catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
            }
        }
    }
      
    /**
     * Retrieve the product model
     *
     * @return Mage_Catalog_Model_Product $product
     */
    public function getProduct()
    {
        return Mage::registry('product');
    }
     
    /**
     * Shortcut to getRequest
     *
     */
    protected function _getRequest()
    {
        return Mage::app()->getRequest();
    }
}