Tutorial: Developing with Symfony and PHP Editor/IDE

by PHP-Editors staff, September 2011
Part 1 :: Installing PhpED and Symfony
Part 2 :: Symfony Project in PHP Editor
Part 3 :: Code insight at work
Part 4 :: Making the most out of color syntax highlighting
Part 5 :: Developing Symfony Application in PhpED IDE
Part 6 :: Debugging Symfony Application in PhpED
Part 7 :: Integrating Symfony CLI in PhpED
Part 8 :: In Conclusion

Previously we published an article about the use of PHP editor for building Web Applications with PHP, JSON and JavaScript. In this tutorial we will look at using PhpED - PHP Editor from NuSphere (http://www.nusphere.com/products/phped.htm) to develop PHP applications based on Symfony (ttp://www.symfony-project.org) - one of the most popular PHP frameworks. This tutorial assumes some familiarity with Symfony and will focus on getting the most out of PHP Editor used to develop Symfony application. Specifically we will look into getting Syntax Color highlighting, Code insight and PHP Debugger to work with Symfony.

Software used in this tutorial:
PHP Editor/IDE: PhpED 6.1 from NuSphere - http://www.nusphere.com
Web server: ApacheFriends XAMPP (Basispaket) version 1.7.0
Operating System: MSFT Windows XP, Version 2002, SP 3


Installing PhpED and Symfony

If you are not using PhpED you should probably give it a try. We reviewed this Editor favorably before - read version 5.9.5 review at http://php-editors.com/review/nusphere.php, please note that the latest version is now 6.1. Free fully functional trial is available here http://www.nusphere.com/download.php.ide.htm - it is straightforward and comes with the full stack needed to run PHP applications, including built in web server. Symfony installation was a bit harder, but not terrible - we used PEAR to get it and got it down with a couple of commands:
    pear channel-discover pear.symfony-project.com
    pear install symfony/symfony-beta
If you search around the web you will see people talking about the workarounds used to install phing but we skipped that part as unnecessary for our simple "Hello, World!" example. Once you have Symfony installed, you can start running Symfony's command line magic to initialize the project:
    chdir c:\xampp\htdocs
    mkdir hello_world_symfony
    chdir hello_world_symfony
We will make one application and one module in this project and call both of them hello_world and now go:
    C:\xampp\htdocs>symfony generate:project hello_world_symfony
    >> dir+      C:\xampp\htdocs\apps
    >> dir+      C:\xampp\htdocs\cache
    >> dir+      C:\xampp\htdocs\config
    >> file+     C:\xampp\htdocs\config/ProjectConfiguration.class.php
    >> file+     C:\xampp\htdocs\config/properties.ini
    >> file+     C:\xampp\htdocs\config/rsync_exclude.txt
    >> dir+      C:\xampp\htdocs\data
    >> dir+      C:\xampp\htdocs\data/fixtures
    >> file+     C:\xampp\htdocs\data/fixtures/fixtures.yml
    >> dir+      C:\xampp\htdocs\lib
    >> dir+      C:\xampp\htdocs\lib/form
    >> file+     C:\xampp\htdocs\lib/form/BaseForm.class.php
    >> dir+      C:\xampp\htdocs\log
    >> dir+      C:\xampp\htdocs\plugins
    >> file+     C:\xampp\htdocs\symfony
    >> dir+      C:\xampp\htdocs\test
    >> dir+      C:\xampp\htdocs\test/bootstrap
    >> file+     C:\xampp\htdocs\test/bootstrap/functional.php
    >> file+     C:\xampp\htdocs\test/bootstrap/unit.php
    >> dir+      C:\xampp\htdocs\test/functional
    >> dir+      C:\xampp\htdocs\test/unit
    >> dir+      C:\xampp\htdocs\web
    >> file+     C:\xampp\htdocs\web/.htaccess
    >> dir+      C:\xampp\htdocs\web/css
    >> file+     C:\xampp\htdocs\web/css/main.css
    >> dir+      C:\xampp\htdocs\web/images
    >> dir+      C:\xampp\htdocs\web/js
    >> file+     C:\xampp\htdocs\web/robots.txt
    >> dir+      C:\xampp\htdocs\web/uploads
    >> dir+      C:\xampp\htdocs\web/uploads/assets
    >> tokens    C:/xampp/htdocs/config/ProjectConfiguration.class.php
    >> tokens    C:/xampp/htdocs/config/properties.ini
    >> tokens    C:/xampp/htdocs/config/rsync_exclude.txt
    >> tokens    C:/xampp/htdocs/config/ProjectConfiguration.class.php
    >> tokens    C:/xampp/htdocs/config/properties.ini
    >> tokens    C:/xampp/htdocs/config/rsync_exclude.txt
    >> tokens    C:/xampp/htdocs/lib/form/BaseForm.class.php
    >> file+     C:\xampp\htdocs\config/databases.yml
    >> dir+      C:\xampp\htdocs\config/doctrine
    >> file+     C:\xampp\htdocs\config/doctrine/schema.yml
    >> chmod 777 C:\xampp\htdocs\web\uploads
    >> chmod 777 C:\xampp\htdocs\cache
    >> chmod 777 C:\xampp\htdocs\log
    >> chmod 777 C:\xampp\htdocs/symfony
    >> chmod 777 C:/xampp/htdocs/web/uploads/assets
    >> tokens    C:/xampp/htdocs/config/databases.yml
    >> tokens    C:/xampp/htdocs/config/doctrine/schema.yml
    >> tokens    C:/xampp/htdocs/config/ProjectConfiguration.class.php
    >> tokens    C:/xampp/htdocs/config/properties.ini
    >> tokens    C:/xampp/htdocs/config/rsync_exclude.txt
    >> tokens    C:/xampp/htdocs/lib/form/BaseForm.class.php


    C:\xampp\htdocs\hello_world_symfony>symfony generate:app hello_world
    >> dir+      C:\xampp\htdocs\hello_world_symfony\apps/hello_world\config
    >> file+     C:\xampp\htdocs\hello_world_sym...apps/hello_world\config/app.yml
    >> file+     C:\xampp\htdocs\hello_world_sym...licationConfiguration.class.php
    >> file+     C:\xampp\htdocs\hello_world_sym...ps/hello_world\config/cache.yml
    >> file+     C:\xampp\htdocs\hello_world_sym...ello_world\config/factories.yml
    >> file+     C:\xampp\htdocs\hello_world_sym.../hello_world\config/filters.yml
    >> file+     C:\xampp\htdocs\hello_world_sym.../hello_world\config/routing.yml
    >> file+     C:\xampp\htdocs\hello_world_sym...hello_world\config/security.yml
    >> file+     C:\xampp\htdocs\hello_world_sym...hello_world\config/settings.yml
    >> file+     C:\xampp\htdocs\hello_world_sym...pps/hello_world\config/view.yml
    >> dir+      C:\xampp\htdocs\hello_world_symfony\apps/hello_world\i18n
    >> dir+      C:\xampp\htdocs\hello_world_symfony\apps/hello_world\lib
    >> file+     C:\xampp\htdocs\hello_world_sym...ello_world\lib/myUser.class.php
    >> dir+      C:\xampp\htdocs\hello_world_symfony\apps/hello_world\modules
    >> dir+      C:\xampp\htdocs\hello_world_symfony\apps/hello_world\templates
    >> file+     C:\xampp\htdocs\hello_world_sym...ello_world\templates/layout.php
    >> tokens    C:/xampp/htdocs/hello_world_sym...hello_world/config/settings.yml
    >> file+     C:\xampp\htdocs\hello_world_symfony\web/index.php
    >> file+     C:\xampp\htdocs\hello_world_symfony\web/hello_world_dev.php
    >> tokens    C:\xampp\htdocs\hello_world_symfony\web/index.php
    >> tokens    C:\xampp\htdocs\hello_world_symfony\web/hello_world_dev.php
    >> rename    C:\xampp\htdocs\hello_world_sym...lo_worldConfiguration.class.php
    >> tokens    C:\xampp\htdocs\hello_world_sym...lo_worldConfiguration.class.php
    >> chmod 777 C:\xampp\htdocs\hello_world_symfony\web\uploads
    >> chmod 777 C:\xampp\htdocs\hello_world_symfony\cache
    >> chmod 777 C:\xampp\htdocs\hello_world_symfony\log
    >> chmod 777 C:\xampp\htdocs\hello_world_symfony/symfony
    >> chmod 777 C:/xampp/htdocs/hello_world_symfony/web/uploads/assets
    >> dir+      C:\xampp\htdocs\hello_world_symfony\test/functional/hello_world

    C:\xampp\htdocs\hello_world_symfony>symfony generate:module hello_world hello_world
    >> dir+      C:\xampp\htdocs\hello_world_sym...rld\modules/hello_world\actions
    >> file+     C:\xampp\htdocs\hello_world_sym...world\actions/actions.class.php
    >> dir+      C:\xampp\htdocs\hello_world_sym...d\modules/hello_world\templates
    >> file+     C:\xampp\htdocs\hello_world_sym...orld\templates/indexSuccess.php
    >> file+     C:\xampp\htdocs\hello_world_sym...orld/hello_worldActionsTest.php
    >> tokens    C:\xampp\htdocs\hello_world_sym...orld\hello_worldActionsTest.php
    >> tokens    C:/xampp/htdocs/hello_world_sym...world/actions/actions.class.php
    >> tokens    C:/xampp/htdocs/hello_world_sym...orld/templates/indexSuccess.php
Lots of lines of output and the whole bunch of files generated - now it is time to launch PHP Editor

Symfony Project in PHP Editor

We used PhpED's Project Settings Wizard to setup our Symfony project - wizard starts automatically when you start new PhpED Project and all it takes is clicking the Next button and correctly selecting the root of the Project:
PhpED Project Settings Welcome Screen:


Select 2-nd option - Web server on the same machine:


Point to XAMPP (or any other Web Server installation) DocumentRoot:


Now select your Project root (if you noticed above we put our Hello World application into C:\xampp\htdocs\hello_world_symfony)


Click through the next few screens:




PhpED will also install PHP Debugger (http://www.nusphere.com/products/phped.htm):


And finally you have your project properties set:


Click Ok, Save the project (we called ours "symfony") and see all of your Symfony project files displayed in PhpED's Workspace tab's tree.
Open C:\xampp\htdocs\hello_world_symfony\web\index.php in the editor and click Run button:


Symfony's default output becomes available in PhpED's embedded Firefox browser:


You can also configure any external browser in PhpED or use embedded IE browser - this option is available in Tools->Settings menu of the editor

Code insight at work

The immediate advantage of using a good PHP Editor is Code Intelisense feature. In PhpED go to View->Tabs->Code Explorer and see all of Symfony PHP classes nicely organized in a tree with configurable display:


You can navigate through your application code or in Symfony core files by simply clicking on the classes in Code Explorer Tree

Making the most out of color syntax highlighting

PHP Editor is expected to provide color syntax highlighting for PHP files. PhpED goes further with dynamic syntax highlighting feature working nicely for PHP, JavaScript, HTML ,CSS and many others but syntax highlighting for YML files is not supported and they are displayed as simple text. However, we found an interesting trick to make it better: Go to Tools->Settings-Associations menu and add *.yml to C++. You will see your yml files code highlighted:


You can also play with Associations to try assigning another language handler to yml files. While not ideal , this makes it much easier to edit

Developing Symfony Application in PhpED IDE

Symfony of course is Model View Controller framework. All requests coming from the browser are dispatched to the appropriate actions by the controller , which gets the configuration from .yml files. To make our Hello World module work we will need to change the routing of hello_world application. In PhpED:
  • navigate to symfony/apps/hello_world/config folder and open the file routing.yml
  • Change the 7th line in that file from - param: { module: default, action: index } to param: { module: hello_world, action: index } - this is telling Symfony to dispatch requests to our hello_world module by default
  • Now lets do something in the action of hello_world module. Navigate to Go symfony/apps/hello_world/modules/ hello_world/actions in workspace tab and open actions.class.php. Default source just forwards the request to default module.
  • Remove existing forwarding code $this->forward('default', 'module'); inside executeIndex() method.
  • Type in $this-> - and see PhpED's code completion in action for Symfony:


    You will notice on the picture above the left most tab, which looks similar to Code Explorer that we showed before - this is Code Navigator tab that displays hierarchical tree of the classes and functions from the file open in the editor
  • Select renderText() method from the list - the picture below shows another nice Code Insight feature of the Editor - helpful hint explaining the use of the method and the link to the place where this method is defined:


    You can also always right mouse click on a function or method in the editor and select Find Declaration menu item, which works perfectly for Symfony objects and your own code. Type in the text: "Hello, World!"
  • Go back to your command line and type: 'symfony cache:clear' or symfony cc
  • You can now type http://localhost/hello_world/web in your browser window and press enter - or, w/o ever leaving the editor, open file symfony/web/index.php and hit Run button to see the output in PhpED's embedded FireFox (or any other browser you have configured in Tools->Settings):

Here we go, Hello World using Symfony and PhpED

Debugging Symfony Application in PhpED

Ironically the most difficult task which is debugging of Symfony application is very easy if you have good PHP IDE. If you used Project Settings wizard in PhpED NuSphere's dbg - PHP debugger is already installed on XAMPP's apache. There is more than one way to start debugging session:
  • open file symfony/web/index.php and hit Run in debugger button. Debugging session starts and breaks the execution on the first executable line on index.php:


    From that point on you can step through the code and step into your own and Symfony's methods and check out all variables and their values. For example, here is what you'll see if you step in the sfContext::createInstance and move the cursor over $configuration:

  • You can also start debugging session by opening type http://localhost/hello_world/web in the browser selecting Debug this page on browser's NuSphere's debugger toolbar
  • You can also set a break point in any of your actions:


    Then if you start debugging session and keep hitting Run in Debugger (which works as Conitnue Debugging on subsequent calls) you will hit that break point eventually. Once there, take a look at call stack to get a better understanding of Symfony's MVC structure:


    Note: if for some reason you are not using Project Settings wizard you can install the debugger yourself both locally and on the remote server. NuSphere's site has the instructions to follow: http://www.nusphere.com/kb/technicalfaq/index.htm - see PHP Debugger section

Integrating Symfony CLI in PhpED

Many PhpED users take advantage of PhpED's integration with 3-rd party tools, mostly shell commands. We didn't figure out a way to integrate the commands requiring user input, such as symfony generate:module , but the most frequently used clear cache command "symfony cc" can be easily integrated:
  • Open Tools->Settings->Integration
  • Select Add Menu
  • Type in Symfony CC for the menu name
  • Click on newly added meny in the list and select Edit button on the right
  • Enter "php @ProjRoot@/symfony cc" into Command Line, select "shell" in Execute with.
  • Select check boxes as shown below:

This command will now be available on pop-up menu in the workspace

In Conclusion

The value of a good PHP Editor/IDE is hard to over estimate. NuSphere's PhpED handled Symfony seamlessly. We found it especially useful for learning the frameworks, where stepping through the code with PHP debugger makes things order of magnitude simpler. We haven't tried Symfony with Zend Studio or Komodo yet, if you would like to submit your review to us, please don't hesitate to do so.


© Copyright 2003-2023 www.php-editors.com. The ultimate PHP Editor and PHP IDE site.