Thursday, July 14, 2011

ASP.NET C# Paths

Sooner you follow a standard practice for paths, easier it will be during the time of the application deployment and launch. The following are the common methods used in ASP.NET for paths.

Application Path: HttpContext.Current.Request.ApplicationPath;

Physical Path: HttpContext.Current.Request.MapPath(appPath), where appPath = HttpContext.Current.Request.ApplicationPath; or "~"

For physical path, you can also use, Server.Mappath("~") or Server.Mappath(appPath);

MapPath method of the Server or Request maps the specified relative or virtual path to the corresponding physical directory on the server. "~" relates to the application root or application path.

Complete URL Path: HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + HttpContext.Current.Request.ApplicationPath;

The complete url path can be very useful if you are sending links in an email through your application for features like invitations.

Cheers!


'Love is not an emotion. It is our very existence.' - Sri Sri

Saturday, March 12, 2011

Removing dl, dt and dd default decorators in Zend_Form

If you are having problems with Zend_Form default document list tags dl, dt and dd tags just remove them and place the list tags.

Follow the following coding method:

$this->clearDecorators();
        $this->addDecorator('FormElements')
         ->addDecorator('HtmlTag', 
                           array('tag' => '<ul>'))
         ->addDecorator('Form');
        
        $this->setElementDecorators(array(
            array('ViewHelper'),
            array('Errors'),
            array('Description'),
            array('Label', array('separator'=>' ')),
            array('HtmlTag', 
            array('tag' => 'li', 'class'=>'form_elements')),
        ));

        $submit->setDecorators(array(
            array('ViewHelper'),
            array('Description'),
            array('HtmlTag', 
            array('tag' => 'li', 'class'=>'submit_button')),
        ));

Here, first we clear the default decorators. Then using setElementDecorators method we add our custom form decorators.

As we do not require label tag and display of errors for the submit button we separately add the decorators for submit button using the method setDecorators.

Wednesday, January 12, 2011

Installing Zend_Tool or the zf command in the console

The prerequisite of this tutorial is that you will have to install the Zend Framework. If you have not done so please install it. This link "Installing PHP5, MySQL and Zend Framework 1.11.2 in Ubuntu" should help you do it.

After you have installed Zend Framework you can use Zend_Tool from your console to create project and other components of Zend.

In order to do this you will have to make an alias of the zend framework bin folder in the .bash_aliases file.

sudo gedit /home/aabhushan/.bash_aliases

Then, add the following and save it

alias zf='/usr/share/php5/zend/bin/zf.sh'

You will also have to make sure the following code in .bashrc is uncommented so that the alias file is being read.

if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi

You can use the following command to open the .bashrc file.

sudo gedit /home/aabhushan/.bashrc

If the above process worked out fine then your Zend_Tool should be working. You can test it by opening a new window of the console and running the zf command. The following command will give you the version of the Zend Framework that you are using

zf show version

Installing PHP5, MySQL and Zend Framework 1.11.2 in Ubuntu

First of all, install PHP5 using the repository by typing the following command in the terminal.

sudo apt-get install php5

Go to an internet browser and type http://localhost. You should get an "It works" message if the installation is complete.

Otherwise you can edit the index.html file located at /var/www folder and rename the filename to index.php and replace the code with the following code.

<?php phpinfo(); ?>

Install MySQL using the following command

sudo apt-get install mysql-server

Also, install PHP module for MySQL by using the following command.

sudo apt-get install php5-mysql

You might want to install Phpmyadmin to help you access and run SQL commands on your databases. Run the following command if you want to install it. Otherwise, you can use MySQL from the terminal itself.

sudo apt-get install phpmyadmin

Now, download the Zend Framework Zip or Tar file from http://framework.zend.com/download/latest and extract the archive. In my case in the following way.

sudo unzip /home/aabhushan/Downloads/ZendFramework-1.11.2.zip

Now, move the extracted Zend Framework folder to the PHP folder.

sudo mv /home/aabhushan/ZendFramework-1.11.2/ /usr/share/php5/

Now,create a soft link for the Zend Framework folder so that you will not have to restart apache when you replace the old version of Zend with the new one.

cd /usr/share/php5/

sudo ln -s ZendFramework-1.11.2/ zend

Now, if you want to include the path to Zend by yourself in your application then you can do it by adding set_include_path in the following way,

set_include_path(get_include_path().PATH_SEPARATOR.'/usr/share/php5/zend');

Otherwise, if you want to add it by default then you can set it in the php settings by editing /etc/php5/apache2/php.ini. In my case in the following way,

sudo gedit /etc/php5/apache2/php.ini

Now, edit the line,

; include_path = ".;/usr/share/php5:/usr/share/pear"

to,

include_path = ".;/usr/share/php5:/usr/share/pear:/usr/share/php5/zend/library"

Now, you need to restart apache and Zend Framework is successfully installed to your computer.

sudo /etc/init.d/apache2 restart

In order to enable apache2 mode rewrite or mod_rewrite, to be able to call the controller action by using "/public/controller/action" instead of using "/public/index.php/controller/action" execute the following command in the terminal.

sudo a2enmod rewrite

Good luck!