Developly

A technical blog about AWS, Symfony, Zend Framework, PHP, Dojo & SaaS.

    • 0
      16 Jun 2011

      Upgrading Symfony2

      • Edit
      • Delete
      • Tags
      • Autopost

      The first time we upgraded, we have to admit, it was a shit-show. Massive copy paste-over shit-show. Since then we’ve gotten a little more pragmatic. Here’s a quick recipe that should help you stay up to date with releases of Symfony2 or it’s bundles, like the Standard Edition for example.

      From the root of your project, run the following commands:

      git remote add symfony https://github.com/symfony/symfony-standard.git
      git fetch symfony
      git checkout v2.0.0BETA5
      git checkout -b symfony-upgrade-beta5
      git checkout master
      rm -rf vendors/*
      git submodules update
      php bin/vendors.php
      git merge symfony-upgrade-beta5

      You might have to deal with conflicts as a regular part of the merge but it’s that’s easy enough if you follow this guide.

      If you clear your console and lose the list of files you need to manually merge, you can run this command to re-summon them:

      git diff --name-only --diff-filter=M -S"<<<<<<< HEAD"
      • views
      • Tweet
      • Tweet
    • 0
      14 Jun 2011

      Simple Symfony 2 Virtual Hosts

      • Edit
      • Delete
      • Tags
      • Autopost

      Like many I’ve looked for a lightning quick virtual host pre-tested for Symfony2 only to find snarky comments saying “Just use a virtual host”. Yeah, thanks! I’ve prepared two virtual hosts locally that allow you to run both development and production environments on Windows – this should work for Linux too, just adapt your paths.

      Add virtual hosts to httpd.conf

      NameVirtualHost *:80
      <VirtualHost *:80>
          ServerName  myapp.lprod
          DocumentRoot "C:\Zend\Apache2\htdocs\myapp\web"
          ErrorLog "C:\Zend\Apache2\htdocs\myapp\app\logs\perror.log"
          CustomLog "C:\Zend\Apache2\htdocs\myapp\app\logs\paccess.log" common
          <Directory "C:\Zend\Apache2\htdocs\myapp\web">
              AllowOverride   All
          </Directory>
      </VirtualHost>
      <VirtualHost *:80>
          ServerName  myapp.ldev
          DocumentRoot "C:\Zend\Apache2\htdocs\myapp\web"
          ErrorLog "C:\Zend\Apache2\htdocs\myapp\app\logs\derror.log"
          CustomLog "C:\Zend\Apache2\htdocs\myapp\app\logs\daccess.log" common
          <Location />
              RewriteEngine On
              RewriteCond %{REQUEST_FILENAME} !-f
              RewriteRule ^(.*)$ /app_dev.php [QSA,L]
          </Location>
      </VirtualHost>

      Add hosts entries

      127.0.0.1 myapp.lprod 
      127.0.0.1 myapp.ldev

      After restarting Apache HTTP and possibly your browser, you should be able to travel to both http://myapp.ldev/ and http://myapp.lprod to get to your application.

      Clear production cache by hand

      If you get an error when going to prod you most likely need to clean cache – remember that this is not automatic for the prod environment. That means this command needs to be ran every-time you make changes that are otherwise cached. Run this command from your project root.

      php app/console cache:clear --env=prod

      I hoped this helps out beginner Symfony2 developers or those who want a quick recipe without messing around.

      • views
      • Tweet
      • Tweet
    • 1
      13 Jun 2011

      Zend_Session_SaveHandler_DbTable in application.ini

      • Edit
      • Delete
      • Tags
      • Autopost

      A common task when thinking about scaling applications horizontally is storing session pointers in shared memory. This is because session data is typically stored on a box’s local disk – so if you have multiple servers under a load balancer, you could be rotated to a box that doesn’t know about your session. This is a problem.

      You might use sticky sessions at the load balancer level to keep users on the box where their sessions were written, but this could have a negative impact on your overall throughput. You could store them in an in-memory layer, like memcached, but you should consider that layers like this should be safely “wipe able” without breaking your application entirely. If you need to ensure that session data is available to many box’s and isn’t going to become corrupt or disappear, then perhaps storing them in database is your best option.

      For some reason setting up session saving to DB wasn’t readily achievable online via application.ini. The docs weren’t too helpful either. I hope this helps developers setup session saving with Zend_Session_SaveHandler_DbTable in a jiffy.

      Create the session table

      CREATE TABLE `session` (
          `session_id` char(32) NOT NULL,
          `save_path` varchar(100) NOT NULL,
          `name` varchar(32) NOT NULL DEFAULT '',
          `modified` int,
          `lifetime` int,
          `session_data` text,
          PRIMARY KEY (`Session_ID`, `save_path`, `name`)
      );

      Configure the session handler

      ;place after resources.db section
      resources.session.save_path = APPLICATION_PATH "/../data/sessions"
      resources.session.use_only_cookies = true
      resources.session.remember_me_seconds = 864000
      resources.session.saveHandler.class = "Zend_Session_SaveHandler_DbTable"
      resources.session.saveHandler.options.name = "session"
      resources.session.saveHandler.options.primary[] = "session_id"
      resources.session.saveHandler.options.primary[] = "save_path"
      resources.session.saveHandler.options.primary[] = "name"
      resources.session.saveHandler.options.primaryAssignment[] = "sessionId"
      resources.session.saveHandler.options.primaryAssignment[] = "sessionSavePath"
      resources.session.saveHandler.options.primaryAssignment[] = "sessionName"
      resources.session.saveHandler.options.modifiedColumn = "modified"
      resources.session.saveHandler.options.dataColumn = "session_data"
      resources.session.saveHandler.options.lifetimeColumn = "lifetime"

      Start the session

      This is optional depending on your application, read more about it in the Zend_Session section of the docs.

      class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
      {
          public _initSessionStart()
          {
              $this->bootstrap('db');
              $this->bootstrap('session');
              Zend_Session::start();
          }
      }
      • views
      • Tweet
      • Tweet
    • 0
      9 Jun 2011

      Purging Zend Server's Job Queue.

      • Edit
      • Delete
      • Tags
      • Autopost

      Zend Server is great. It kicks WAMP & MAMP’s butt. My favorite feature is the job queue – it offers an object oriented, cross-platform, cron-like paradigm. It’s really handy.

      When developing with it it’s not uncommon to have many jobs fail (from expirementing), jobs that spawn jobs, or jobs that are dependent on other jobs. When this happens, your jobqueue logs get ginormous. This isn’t typically a problem but the Zend Server admin interface does not allow you to select more than one page of jobs at a time. When you have 20,000 jobs, this means you’ll be clicking select all then delete about 1000 times. This isn’t reasonable. Here is the best and easiest way to delete Zend Server job queue records.

      1. Stop the Zend Job Queue service.
      2. Delete the JobQueue database @ ZendServer/data/db/jobqueue.db
      3. Start the Zend Job Queue service.

      Hopefully this helps other developers in the same position. And Zend, make a real select all button in the ZendServer admin panel.

      • views
      • Tweet
      • Tweet
    • 1
      3 Jun 2011

      Using Symfony2 on PhpFog now.

      • Edit
      • Delete
      • Tags
      • Autopost

      PhpFog & Symfony

      PhpFog is a new PHP PaaS service. It’s dazzling. It takes the weight of deploying applications to the cloud off your shoulders and allows you to focus on writing code. You can deploy your app’s with a simple git push phpfog master. This is great, but PhpFog is new and there is no built-in considerations for Symfony2. That makes sense – Symfony2 is still (at the time of writing) in beta. But I want to deploy Symfony2 apps now!

      In this article I’ll show you how you can use PhpFog to host a Symfony2 application. For the purpose of ease we are going to use Github but you could just as easily host your code with Beanstalk, or use PhpFog’s Git repository alone.

      Step 1, setup a Symfony2 app in Git.

      If you already have your application in Git and have at least one production route, then you can skip to the next step.

      1. Download the Symfony2 Standard Edition
      2. Add a new bundle called CompanyExampleBundle where company is the name of your company, group, or project. Don’t forget to add Company to the auto-loader in app/autoload.php, and register the bundle in app/AppKernel.php.
      3. Create a new controller at src/Company/Controller/HelloWorldController.php which renders a view CompanyExample:HelloWorld:index.html.twig. In the view, put in a nice little Hello world!.
      4. In app/routing_dev.yml change the _welcome route’s pattern to /welcome. Now go to app/routing.yml and add a route like so:

        _hellowworld:
              pattern: /
              defaults: { _controller: CompanyExampleBundle:HelloWorld:index }
      5. At this point, verify that you can get to the new controller by traveling to http://localhost/yourproject/web/app_dev.php/ in your browser. If you see a Hello world!, then you’re gravy. Now substitute app_dev.php for just app.php to access the production front controller and make sure you see the same thing. If you get a 400 or things appear to be as they were before, you might need to blow production cache. This can be done from the command line like so:

        cd /to/yourproject
         php app/console --env=prod cache:clear
      6. You’ve setup a new Symfony2 app and are ready to get going! Commit and push your application to Github then continue on to the next step.

      Step 2, Create your app on PhpFog.

      1. Login to PhpFog and select Create a New App.
      2. On Step 1 of the application wizard choose “Custom App” from the bottom left.
      3. On Step 2 it’s important to note that if you want to use Assetic, then you need a paying box – this is because the shared tier does not support open_basedir. If you aren’t going to use Assetic, go ahead and choose the free shared tier.
      4. On the final step choose a URL. If you decide to use a custom URL you will have to alter DNS settings, so for a quicker finish we’d suggest a phpfog url. Once you’ve done this hit the “Create Application” button and you’re done! Once the application has been provisioned you will receive an email and can continue onto the next step.

      Step 3, Pushing your Symfony2 application to PhpFog

      Pushing your app to PhpFog is really easy. It’s perhaps my favorite thing to do.

      1. Add the public key from the machine you are working on ~/.ssh/id_rsa.pub to the accepted SSH keys in your Account Settings.
      2. Now ho to the Source Code tab from your apps PhpFog admin panel and copy the git repository location.
      3. Finally, from the console, add phpfog as a remote and push. Like so:

        git remote add phpfog git@git01.phpfog.com:myproject.phpfogapp.com
         git push phpfog master --force
         # should say something like 'deploying to the cloud' on success
      4. Now test that your code made it up by accessing the robots.txt file that comes with Symfony2 by default. Should be at a url like so: http://company.phpfogapp.com/web/robots.txt. If this resolves, you’ve successfully deployed your code to the cloud!

      You’re not done yet, however, you still need to tweak you PhpFog app to Symfony2’s liking.

      Step 4, Configure your PhpFog app for Symfony2

      You’re almost there. There’s just a couple things left…

      1. Symfony2 caches all your configuration files to the app directory, and therefor needs write access. Go to the “Permissions” tab of your app’s admin panel and add the pathname app and save changes.
      2. Go to the “Settings” tab and change the Base Directory to web. This will tidy up your url, you won’t have to type in /web before accessing resources.
      3. Finally, you need a way to clear production cache, while in production. PhpFog doesn’t give console access so the simplest way to accomplish this is by creating a php script called web/clean_prod_cache.php with the code like so:

        echo `php ../app/console  env=prod cache:clear`;
         echo `php ../app/console  env=prod cache:warm`;

        Now when you go to http://company.phpfogapp.com/clean_prod_cache.php the cache will be wiped and warmed. All ready to go.

      I hope that this guide helps both developers looking to use Symfony2 on PhpFog early as well as the PhpFog team in implementing Symfony2 support.

      • views
      • Tweet
      • Tweet
    • 0
      1 Jun 2011

      PhpStorm equivilant of Eclipse's Open Resource Dialog.

      • Edit
      • Delete
      • Tags
      • Autopost

      My favorite by far feature of Eclipse is the ability to press CTRL + SHIFT + R to enable the "Open Resource" dialog, which allowed you to auto-search your projects file tree for a file. On PhpStorm the equivilant is CTRL + SHIFT + N. Good luck with your PhpStorming.

      • views
      • Tweet
      • Tweet
    • 0
      29 Mar 2011

      Fixing Packages Excluded Yum Error with Zend Server Repo

      • Edit
      • Delete
      • Tags
      • Autopost

      If you’ve ever installed Zend Server on a server with yum-plugin-priorities installed, then you know that it’s difficult to yum list available *php*. You end up getting an error like 100 packages excluded due to repository priority protections. The reason for this is that zend.conf, which Zend instructs you to use, doesn’t come with priorities specified by default. Simply update the conf file to include a priority to fix the issue. We added priority=1 and failovermethod=priority to ours:

      [Zend]
      name=Zend Server
      baseurl=http://repos.zend.com/zend-server/rpm/$basearch
      enabled=1
      gpgcheck=0
      priority=1
      failovermethod=priority
      
      [Zend_noarch]
      name=Zend Server - noarch
      baseurl=http://repos.zend.com/zend-server/rpm/noarch
      enabled=1
      gpgcheck=0
      priority=1
      failovermethod=priority

      Now when we ran a yum list available *php* we got the expected response:

      [root@ip-1 httpd] yum list available *php*
      
      Loaded plugins: fastestmirror, priorities, security
      Loading mirror speeds from cached hostfile
      Available Packages
      cups-php.x86_64                                 1:1.4.2-35.7.amzn1   amzn-main
      graphviz-php.x86_64                             2.26.0-4.12.amzn1    amzn-main
      libinformix-php-5.2-zend-server.x86_64          2-90                 Zend
      libinformix-php-5.3-zend-server.x86_64          2-90                 Zend
      mod-php-5.2-apache2-zend-server.x86_64          5.2.17-27            Zend
      php.x86_64                                      5.3.5-1.12.amzn1     amzn-main
      php-5.2-apc-zend-server.x86_64                  5.2.17-14            Zend
      php-5.2-bcmath-zend-server.x86_64               5.2.17-8             Zend
      php-5.2-bz2-zend-server.x86_64                  5.2.17-8             Zend
      php-5.2-calendar-zend-server.x86_64             5.2.17-8             Zend
      php-5.2-cm-utils-pe-zend-server.x86_64          1.0.14-18            Zend
      php-5.2-cm-utils-zend-server.x86_64             1.0.14-18            Zend
      php-5.2-code-tracing-zend-server.x86_64         4.1.55-31            Zend
      php-5.2-common-extensions-zend-server.noarch    5.0.4-25             Zend_noarch
      php-5.2-common-extensions-zend-server-ce.noarch 5.0.4-25             Zend_noarch
      php-5.2-ctype-zend-server.x86_64                5.2.17-8             Zend
      php-5.2-curl-zend-server.x86_64                 5.2.17-15            Zend
      php-5.2-data-cache-zend-server.x86_64           4.0.59-11            Zend
      php-5.2-debugger-zend-server.x86_64             5.3.23-12            Zend
      php-5.2-dev-zend-server.x86_64                  5.2.17-20            Zend
      php-5.2-download-server-zend-server.x86_64      1.5.48-17            Zend
      php-5.2-exif-zend-server.x86_64                 5.2.17-9             Zend
      #truncated list for this example

      This helped us when installing Zend Server on Amazon Linux AMI, now that they have moved to priorities.

      • views
      • Tweet
      • Tweet
    • 4
      22 Mar 2011

      3 Step Layouts in Zend Framework

      • Edit
      • Delete
      • Tags
      • Autopost

      Zend Framework provides the components Zend_View and Zend_Layout to facilitate developing the view layer of your application. These components are light, powerful, and nifty – making it easy to alter behavior slightly to achieve different rendering strategies. The primary strategy outlined by the framework is the Two Step View pattern.

      While the Two Step View pattern creates a reasonable paradigm for seperating layout presentation from page presentation, enabling Three-level Inheritance allows for even better separation. In other words, using 3 Step Layouts is a possible and often necessary investment to make when implementing front-ends in Zend Framework projects; they allow fine grain re-usability that provides for a DRYer presentation layer.

      3 Step View pattern

      Setting up 3 Step Layouts

      Nothing extra is needed to implement this pattern, just Zend Framework.

      Step 1 – The Wrapper

      The first step is to create a wrapper. A wrapper is just a view, there’s nothing special about it. Register your wrapper with Zend Framework by popping into application.ini and telling the Layout Resource that the wrapper is the default layout, also specifying where to find it:

      ; application/configs/application.ini
      
      resources.layout.layoutPath = APPLICATION_PATH "/views/layouts" 
      resources.layout.layout = wrapper

      As a general rule of thumb all view behavior that needs to happen on every request, regardless of which controller or module is being accessed, belongs in the wrapper. Another way of saying it is that the wrapper is the bootstrap for the presentation layer. Here’s an example wrapper that can be used as a starting point. It’s a pretty good assumption that every application you create will at least start with a structure like so:

      <?php /* application/views/layouts/wrapper.phtml */ ?> 
      
      <?php /* @var $this Zend_View */ ?> 
      <?php echo $this->doctype()."\n"; ?> 
      <html> 
          <head> 
              <?php echo $this->headTitle(); ?> 
              <?php echo $this->headMeta(); ?> 
              <?php echo $this->headLink(); ?> 
              <?php echo $this->headStyle(); ?> 
          </head> 
          <body> 
              <?php /* NESTED LAYOUT SUPPORT */ ?> 
              <?php if($this->layout()->nestedLayout): ?>  
                  <?php $this->layout()->setLayout($this->layout()->nestedLayout); ?> 
              <?php else: ?> 
                  <?php $this->layout()->setLayout('default'); ?> 
              <?php endif;?> 
              <?php echo $this->layout()->render();  ?> 
      
              <?php /* OUTPUT JAVASCRIPT */ ?> 
              <?php echo $this->headScript(); ?> 
              <?php echo $this->inlineScript(); ?> 
          </body> 
      </html>

      From here you should extend your wrapper to add global layout configuration for your app. By extending, I simply mean adding code directly to the wrapper in order to provide more functionality. Here are a few things that you might be done in your wrapper:

      • Setting the doctype and charset
      • Setting a base site title that will be appended to downstream
      • Linking in CSS libraries like reset, grid system, font-stacks, etc…
      • Attaching JavaScript libraries like JQuery, Dojo, YUI, etc…
      • Inserting site tracking urchins

        Note that you should only ever need 1 wrapper in your application, needing more could be a symptom of poor design. Comment below if you find yourself in a jam.

      Step 2 – The Nested Layout

      If the wrapper is the bootstrap to our presentation layer, then a nested layout is the layout to our presentation layer. A nested layout is also just a regular Zend_View script that will be consumed by the wrapper we just created. It contains markup that is specific to your application. These blocks usually include the header and footer, and possibly different columns with placeholders for capture. Our wrapper looks for a layout called default. For the sake of the example we’re keeping it simple:

      <?php /* application/views/layouts/default.phtml */ ?> 
      
      <div class="container">    
          <?php /* @var $this Zend_View */ ?> 
          <div class="header"> 
              <h1>My Application</h1> 
          </div> 
          <div class="body"> 
              <?php echo $this->layout()->content; ?> 
          </div> 
          <div class="footer"> 
              <p>Copyright 2011</p> 
          </div> 
      </div>

      Step 3 – The View

      At this stage you simple allow Zend Framework to serve the view intended. You’re done. It’s a surprisingly simple solution that should prove to be worth the time taken to put in place.

      Interchanging Nested Layouts

      Further along your development you may find a need to switch out the layout for different reasons. Here are a couple ways to modify which nested layout will be used.

      Changing the Nested Layout from a View

      The default nested layout will more than likely be the layout you use for most pages, but in the case that you need a different layout – specifying one is easy. A common use case for this is switching to a modal layout, a centered window for less content. A good example is a login page:

      <?php /* @var $this Zend_View */ ?>
      <?php $this->layout()->nestedLayout = 'modal'; ?>
      <?php $this->headTitle()->append = 'Login'; ?>
      
      <h1>Please Login</h1>
      <?php echo $this->form; ?>

      This will tell the wrapper that you want to use the layout application/views/layouts/modal.phtml instead of the default.

      A Nested Layout Per Module

      Another common scenario in Zend Framework is the use case of having a layout defined for each module – admin, www, editor, what have you. This is a slightly more advanced problem that can be solved using a Front Controller Plugin. Start by registering our soon-to-be plugin:

      ; application/configs/application.ini
      
      resources.frontController.plugins.nl = Application_Plugin_NestedLayoutPerModule

      Now define a plugin that sets the nested layout to the module’s name

      /* application/plugins/NestedLayoutPerModule.php */
      
      Application_Plugin_NestedLayoutPerModule
          extends Zend_Controller_Plugin_Abstract
      {
          public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
          {
              $module = $request->getModuleName();
      
              $front = Zend_Controller_Front::getInstance();
              $layout = $front->getParam('bootstrap')->getResource('layout');
      
              $layoutDir = $front->getModuleDirectory() . '/views/layouts';
              $layout->setScriptPath($layoutDir);     
          }
      }

      If you were accessing an admin module via the uri /admin, then the plugin would look for a file called application/modules/admin/views/layouts/default.phtml.

      Variations – Nesting Nested Layouts

      Once your nested layouts become fairly advanced, you might find it useful to create specialization yet again. Say, for example, up until now your main nested layout has a big fat 1 column layout. It’s served you well up until this point. Now you have a need to add a 2 and 3 column layout. You can create a new nested layout of course, but then you would have to recreate your extensive header and footer markup you crafted so carefully. You could move the header and footer to partials and pull them in in both nested layouts – but that requires an uneccessary contract. Instead, lets create another layout layer, a variation. To use variations, follow the same pattern as nested layouts. Replace echo $this->layout()->content with the following to allow your nested layout to look for the presense of a variation layout:

      <?php /* application/views/layouts/default.phtml */ ?<
      
      <?php /* @var $this Zend_View */ ?> 
      <div class="container"> 
          <div class="header"> 
              <h1>My Application</h1> 
          </div> 
          <div class="body"> 
      
              <?php /* VARIATIONS SUPPORT */ ?> 
              <?php if($this->layout()->variation): ?>  
                  <?php $this->layout()->setLayout($this->layout()->variation); ?> 
                  <?php echo $this->layout()->render();  ?> 
              <?php else: ?> 
                  <?php echo $this->layout()->content; ?> 
              <?php endif;?> 
          </div> 
      
          <div class="footer"> 
              <p>Copyright 2011</p> 
          </div> 
      </div>

      Now lets create a 2 column variation for our default nested layout:

      <?php /* application/views/layouts/variations/default-2col.phtml */ ?> 
      
      <?php /* @var $this Zend_View */ ?> 
      <div class="main"> 
          <?php echo $this->layout()->content; ?> 
      </div> 
      <div class="sidebar"> 
          <?php echo $this->placeholder('sidebar'); ?>
      </div> 
      <footer> 
          <p>Copyright 2011</p> 
      </footer>

      Finally, from our login page, let’s take advantage of our new variation:

      <?php /* @var $this Zend_View */ ?>
      <?php $this->layout()->variation = 'default-2col'; ?>
      <?php $this->headTitle()->append = 'Login'; ?>
      
      <h1>Please Login</h1>
      <?php echo $this->form; ?>
      
      <?php $this->placeholder('sidebar')->captureStart(); ?>
          <p>Forgot your password? That's unfortunate.</p>
      <?php $this->placeholder('sidebar')->captureEnd(); ?>

      Zend_View is very flexible

      Zend Framework makes it easy to create more advanced view layers like this. It’s sometimes useful to have a peep at how other framework’s addressing this problem, too. Symfony2, for example, uses a templating language called Twig which makes it easy to implement this pattern. They’ve detailed their approach in their section on Three-level Inheritance. It’s impressive that Zend Framework, who supplies no templating language by default (other than PHP itself), allows for such crisp plain ‘ol PHP view implementations.

      • views
      • Tweet
      • Tweet
    • 0
      9 Mar 2011

      Installing Memcached as a Windows Service.

      • Edit
      • Delete
      • Tags
      • Autopost

      Here's a quick cmd recipe for installing memcached as a service on windows. This script uses memcached 1.2.4, but you can check for newer versions from splinedancer.

      dir C:/this/is/the/install/dir
      curl -o memcached.zip http://www.splinedancer.com/memcached-win32/memcached-1.2.4-Win32-Preview-20080309_bin.zip
      curl -o unzip.exe http://stahlforce.com/dev/unzip.exe
      unzip.exe memcached.zip
      memcached.exe -d install
      memcached.exe -d start
      del unzip.exe

      If everything worked, you should be able to run services.msc and check that the memcached service exists, set to automatic, and is running.

      Memservices

      • views
      • Tweet
      • Tweet
    • 0
      4 Mar 2011

      Checking if a Placeholder Exists in Zend Framework's Zend_View

      • Edit
      • Delete
      • Tags
      • Autopost

      A common scenario we’ve run into while implementing front ends with Zend Framework is the need to be able to check for the existence of a specific placeholder. If data has been captured in placeholder header for example, then output the placeholder inside this kind of visual structure.

      There are two ways to accomplish this.

      Checking the Registry

      This is the quickest way, by questioning the placeholder helper’s registry:

      <?php /* @var $this Zend_View */ ?>
      <?php /* @var $pH Zend_View_Helper_Placeholder */ ?>
      <?php $pH = $this->getHelper('placeholder'); ?>
      
      <?php if ($pH->getRegistry()->containerExists('myStuff')): ?>
          <div class="myStuff">
              <?php echo $pH->placeholder('myStuff'); ?>
          </div>
      <?php endif; ?>

      Extending Zend_View_Helper_Placeholder

      If you want to make things a little less verbose, you could override the the placeholder helper entirely to enable checking. Add the following to application.ini:

      ;application/configs/application.ini
      resources.view.helperPath.MyApp_View_Helper = APPLICATION_PATH "/../library/MyApp/View/Helper"

      Then create a helper that extends Zend_View_Helper_Placeholder:

      /**
       * MyApp/View/Helper/Placeholder.php
       * allow quick checking of container existence
       */
      class MyApp_View_Helper_Placeholder
          extends Zend_View_Helper_Placeholder
      {
          public function placeholder($name, $checkOnly=false)
          {
              if ($checkOnly) {
                return $this->__isset($name);
              }
          }
          public function __isset($container)
          {
              return $this->getRegistry()->containerExists($container);
          }
      }

      Now checking if a placeholder exists will look like so:

      <?php /* @var $this Zend_View */ ?>
      <?php if ($this->placeholder('myStuff', true)): ?>
          <div class="myStuff">
              <?php echo $this->placeholder('myStuff'); ?>
          </div>
      <?php endif; ?>
      • views
      • Tweet
      • Tweet
    « Previous 1 2 3 Next »
    • Search

    • Tags

      • zend framework
      • php
      • ec2
      • amazon linux ami
      • symfony2
      • zend server
      • Zend_View
      • memcached
      • mysql
      • puppet
      • recommendations
      • windows
      • zend_db
      • zend_rest_controller
      • zend_rest_route
      • SaaS
      • Zend_Layout
      • Zend_View_Helper_Placeholder
      • ajax
      • aws
      • cmd
      • continuous integration
      • dns
      • dojo
      • eclipse
      • gmail
      • html5
      • humor
      • linux
      • oracle
      • paas
      • phpfog
      • phpstorm
      • postgresql
      • sql server
      • startups
      • svn
      • version control
      • virtualhosts
      • webcomics
      • yum
      • zend_application_resource_mail
      • zend_config_ini
      • zend_form
      • zend_mail
      • zend_session
    • Archive

      • 2011 (11)
        • June (6)
        • March (5)
      • 2010 (15)
        • December (1)
        • November (5)
        • October (9)
    • Obox Design
  • Developly

    Learn about Insured Rating, a cloud based insurance rating engine at www.insuredapp.com

    111151 Views
  • Get Updates

    Follow this site »
    You're following this site (Edit)
    You're a contributor here (Edit)
    This is your site (Edit)
    Subscribe by email »
    Get the latest updates in your email box automatically.
    Loading...
    Subscribe via RSS
    Twitter