symfony

Helpers for symfony 1.1

Here is the list of helpers in symfony 1.1.

But unlike upgrading to rails 1.2.6 to 2.0.2, there is one setting to make everything compatible. This difference talks a lot about the diffirence between symfony and Ruby on Rails.

See the upgrade document of symfony 1.1.

Snippets for symfony i18n for TextMate

Update: Looks like this post is on symfony blog. You can find more snippets on this website.


The symfony framework uses XLIFF for i18n. If you are new to i18n in symfony, this short blog post gives you a very good introduction.

TextMate symfony bundle comes with a snippet called “xliff” that creates a set of XML’ed lines of the source and translated string. The default key equivalent is set to ⌘-I. The snippet for that is:

<trans-unit id="$1">
  <source>${2:$TM_SELECTED_TEXT}</source>
  <target>$0</target>
</trans-unit>

This is a very handy snippet and I use it quite often. But for the XML files to work properly, you need to add the other tags. Here’s the snippet I use:

<?xml version="1.0" ?>
<xliff version="1.0">
  <file original="global" source-langauge="zl_ZL" datatype="plaintext">
    <body>

$0


    </body>
  </file>
</xliff>

Set “Activation” to “Tab Trigger” “xliff” and set “Scope Selector” to “text.xml”.

PHP symfony snippets for TextMate

Helper tags in symfony is much like in other frameworks. Some of them are included by default, but other aren’t. You can indeed set all the helpers you need in settings.yml, but some (myself included) prefer to include the helpers only when necessary. The following snippets require you are in HTML scope.

For JavaScript:

<?php use_helper('Javascript') ?>
$0

For Validation:

<?php use_helper('Validation') ?>
$0

For Object:

<?php use_helper('Object') ?>
$0

For Text:

<?php use_helper('Text') ?>
$0

Set “Activation” to “Tab Trigger” “use”. Set “Scope Selector” to “text.html”.

Here a QuickTime movie (with no sound) that shows how these work. The way above doesn’t support using more than one helper at a time. You might very well have time you need more than one helpers. You can also add the following to make this process easier.

<?php use_helper('${1:helper}'$2) ?>
$0

There should be a better way to add the multiple helpers. I just can’t come up with a better solution now.

PHP Snippets for TextMate: Open PHP Tag

Recent PHP web apps like Drupal and frameworks like symfony recommend you not close the PHP tag in a PHP script. (In templates you need to open and close PHP tags a lot, of course.)

TextMate PHP Bundle comes with the following snippet with “Tab Trigger” as “php”. Inside an HTML scope, it works as <?php $0 ?> and inside in PHP scope, it works as ?>$0<?php.

When I write a PHP script, I open a blank document and set the language to “HTML”. (TextMate recommends that you use “HTML” instead of “PHP”.) Because I do not want to close the PHP tag, I only want to insert <?php.

In order to do so, I made the following snippets. The snippet itself is simple:

<?php

Set “Activation” to “Tab Trigger” “ph” and set “Scope Selector” to “text.html”. By using “ph” rather than “php”, you don’t see any conflict when you use the snippet.

TextMate PHP Snippets: Debug Print

Here are some of the snippets I use with TextMate.

This is an old school way, but I use var_dump() quite often. The following snippets works when you are editing an HTML file.

<?php echo "<pre>"; var_dump(\$${1:variable}); die(); // DEBUGPRINT ?>

Set “Activation” to Tab Trigger “debug”, and set “Scope Selector” to “text.html”

I have the snippet that works inside a PHP script.

echo "<pre>"; var_dump(\$${1:variable}); die(); // DEBUGPRINT

Set “Scope Selector” to “source.php”.

The reason I use // DEBUGPRINT to leave the snippet searchable. I often comment out the entire line just like:

// echo "<pre>"; var_dump($variable); die(); // DEBUGPRINT

You may be able to do the very similar thing in other text editors like Coda.

I didn’t know the <pre> tag makes the output very readable. Thanks to my colleague.

PHP: Attacking and Defending

For those who subscribe to Matz’s website, the PHP vs Ruby was a hot topic.

My background is:

  • I had basic skills in PHP without OOP.
  • I started to learn a MVC framework in PHP (i.e., symfony).

I just spent a day with ruby. I started to feel the programmer’s happiness.

A Shorthand for Clearing Cache in Symfony

In developing with symfony, you delete the cache over and over. The symfony command for this is that:

symfony clear-cache

or

symfony cc

The second one is muct shorter, but we can make it far shorter by making an alias for the command.

  1. Open your bash profile file. That would “~/.bash_login”, “~/.profile” or “~/.bash_profile”.
  2. Add the following code: alias cc="symfony cc"
  3. Close the terminal window and reopen. Now the command cc should work as “symfony cc”.

Symfony Podcast

PHP Abastract Podcast introduces symfony framework, an MVC framework for PHP.

Installing symfony via PEAR on Leopard

This documentatoin is still pre-alpha. If you find any error on this tutorial, please email me. Visit projects.


Installing PEAR and symfony

This article assumes you read my “Install PHP and MySQL via MacPorts on Mac OS X 10.5 Leopard”. This article tries to make a web server for a symfony project locally.

We install symfony via PEAR and make it work with the Ajax tutorial on symfony website.

What is PEAR?

Just read the manual.

Installing PEAR

Leopard, unlike its predecessors, doesn’t have PEAR pre-installed. You need to install it manually. We don’t use MacPorts here. PEAR also has an excellent documentatoin. Visit PEAR manual.

curl http://pear.php.net/go-pear > go-pear.php

This step downloads a file need to install pear. The file will be located under your home folder, and the file name is “go-pear.php”. (Note: We don’t use go-pear.org because go-pear.org is not longer available.)

sudo php -q go-pear.php

Follow the instruction. Default setting doesn’t cause any problem. Just your personal preference. (But, I installed the tool under /usr/local/bin. Concerning why, read the Dan Benjamin’s article.)

Now your have pear command tool installed.

How to See Your Configuration

Everytime you come across a setting problem, it’s always useful to check your currnt configuration. You can check your pear configuration by running

pear config-show

As I would like to install beta packages as well as stable ones, I set preferred package state to be beta.

You can do so by running

sudo pear config-set preferred_state beta

Here’s what I have:

Auto-discover new Channels     auto_discover    <not set>
Default Channel                default_channel  pear.php.net
HTTP Proxy Server Address      http_proxy       <not set>
PEAR server [DEPRECATED]       master_server    pear.php.net
Default Channel Mirror         preferred_mirror pear.php.net
Remote Configuration File      remote_config    <not set>
PEAR executables directory     bin_dir          /usr/local/bin
PEAR documentation directory   doc_dir          /usr/local/share/pear/docs
PHP extension directory        ext_dir          ./
PEAR directory                 php_dir          /usr/local/share/pear
PEAR Installer cache directory cache_dir        /var/folders/qo/qoHKwm05H3eGEek47+j0Hk+++TI/-Tmp-//pear/cache
PEAR data directory            data_dir         /usr/local/share/pear/data
PEAR Installer download        download_dir     /usr/local/temp/download
directory
PHP CLI/CGI binary             php_bin          /usr/bin/php
php.ini location               php_ini          <not set>
PEAR Installer temp directory  temp_dir         /usr/local/temp
PEAR test directory            test_dir         /usr/local/share/pear/tests
Cache TimeToLive               cache_ttl        3600
Preferred Package State        preferred_state  beta
Unix file mask                 umask            22
Debug Log Level                verbose          1
PEAR password (for             password         <not set>
maintainers)
Signature Handling Program     sig_bin          /usr/local/bin/gpg
Signature Key Directory        sig_keydir       /private/etc/pearkeys
Signature Key Id               sig_keyid        <not set>
Package Signature Type         sig_type         gpg
PEAR username (for             username         <not set>
maintainers)
User Configuration File        Filename         /Users/takaaki/.pearrc
System Configuration File      Filename         /private/etc/pear.conf

Modifying php.ini to Recoginize PEAR installation

Now to make pear work with PHP, you need to add the pear path in your PHP include path by editing php.ini file. The path you need to include is php_dir, in my case /usr/local/share/pear.

mate /opt/local/etc/php.ini

First of all, though not mandatory, but it is highly recommended that you not use magic quotes in PHP. To turn it off, look for “magic_quotes_gpc = On”, which is around line 440. And change it to

magic_quotes_gpc = Off

Next, add th PEAR directory path to include path. Find “include_path”, which is around line 470. Add your PEAR directory, which you can find by running pear config-show and remove the semicolumn (;) at the beginning of the line.

My one looks like this:

include_path = ".:/usr/local/share/pear"

Check if PEAR is recongized with your PHP script

Edit test.php file to check if PHP can work with PEAR.

Open test.php.

mate /opt/local/apache2/htdocs/

Write the following and save it.

<?php

if (require 'PEAR.php') {
    echo "You can now use PEAR with PHP!";
} else {
    echo "PEAR is not recoginzed with your PHP.";
}

?>

Visit localhost/test.php. If you get an error, include_path may very well be wrong.

Installing symfony

Follow the symfony official guide. Find the “PEAR installation”

After installation, let’s check if symfony is properly installed or not. Type the following to get the version number of symfony.

symfony -V

(Note: this is capital/large V. symfony -v won’t work.)

If you see the version number of symfony, symfony is installed successfuly.

Set up a Web Server for symfony

There are three ways to install symfony: a) The sandbox, b) Source Download and c) PEAR instllation. We are using PEAR installation approach. We are going to have a web server for a symfony project running. For other instllation, visit symfony’s documentation.

Our aim is to set up a webserver on a Mac for the Easy Ajax Tutorial on symfony documentaion.

Let’s make several settigs clear. Always keep what you try to do in mind:

  • We make a project called “sfdemo” in the folder named “sfdemo” under your user’s Sites folder.
  • Therefore, your path to the project is /Users/[yourusername]/Sites/sfdemo.
  • Symfony projects require a setting where /web under the project directory is set to web root path. (So, browsers only access to /web directory.)
  • That is, Apache needs to access /Users/[yourusername]/Sites/sfdemo/web when your broweser points http://localhost. (Note: Don’t add .com in the URL.)

Before creating a symfony project, we change Apache configuration file (httpd.conf) so that our project works under localhost.

Open Apache configuration file (httpd.conf).

    mate /opt/local/apache2/conf/httpd.conf

Add the following at the beginning. Replace [yourusername] with a real one. Also replace “/usr/local/share/pear/data” with your PEAR data directory path. You can find your by running pear config-show.

    <VirtualHost localhost:80>
    ServerName localhost
    DocumentRoot "/Users/[yourusername]/Sites/sfdemo/web"
    DirectoryIndex index.php
    Alias /sf /usr/local/share/pear/data/symfony/web/sf
    <Directory "/usr/local/share/pear/data/symfony/web/sf">
    AllowOverride All
    Allow from All
    </Directory>
    <Directory "/Users/[yourusername]/Sites/sfdemo/web">
    AllowOverride All
    Allow from All
    </Directory>
    </VirtualHost>

Save the file and restart Apache web server.

    sudo apachectl restart

You will see a warning as the following.

[warn] VirtualHost localhost:80 overlaps with VirtualHost localhost:80, the first has precedence, perhaps you need a NameVirtualHost directive

This means that you already have a setting for localhost:80 in your Apache configuration file. As this is a warning, not an error, let’s ingnore it for a second. You can tweak the configuration anytime when you learned more about Apache.

Let’s make a symfony project.

First, open Terminal and move to the Sites folder.

cd ~/Sites

And create a directory called “sfdemo” and move to the folder.

mkdir sfdemo
cd sfdemo

Create a project called “sfdemo” and make an application called “app” and a module called “cart”. What each command does is not explained here. Visit the tutorial.

symfony init-project sfdemo
symfony init-app app
symfony init-module app cart

Visit localhost/cart. If everything works fine, you will see a symfony page, saying “Module " cart" created”.

References

Reading Chapter 1 and 2 of the Symfony Book

Just finished Chapter 1 and 2. Some notes.

  • Symfony takes advantage of PHP5. Magic methods are used often. I really should push myself to learn PHP5 and OOP. I will suspend reading the book when I encountered PHP language problems. The book says OOP skills are essential before you start to use symfony.
  • I could understand what MVC framework is, but didn’t fully understand symfony workflow. Layout/view logic/template is one of them I couldn’t understand. The layout is application-global. I didn’t get what logic view is. I hope reading more will give me a clear picture.
  • The structure of a project wasn’t very clear. I have no idea how define applications and modules. This might not be well-explaned yet in this very early chapter. But I got a basic understanding.
  • A project tree structure has a lot of config, lib and templates directories specific to each level. I shold be careful not to misplace files in different levels.
  • Parameter holders. I have no understanding whatsoever about them.
  • Class autloading. You don’t need include statements in spite of many lib directories. Really good news.

My tasks.

If you have any advice on me, please don’t hesitate to leave a comment.

Syndicate content