Skip to content

jonocode – a developer's blog

I moved! –> http://pressreload.com

Category Archives: Symfony 1.0

I recently received a request from an SEO / online marketing company to remove the extra title from one of our sites. Symfony was displaying the title information for every page twice; once in the title tag, and again in as a meta tag with name=”title”.

Google was not displaying the title they had given the index page, and there was a concern that Google considered the title duplication to be spam.

Both the title tag and the meta tags are generated from the helper functions include_title and include_metas, respectively. I could find no easy, configuration-based way to tell include_metas to not include the title meta, so I decided replace the helper with the following:

<?php

foreach ( sfContext::getInstance()->getResponse()->getMetas() as $name => $content ):
  if ( $name != 'title' ):
    echo tag('meta', array('name' => $name, 'content' => $content))."\n";
  endif;
endforeach;

?>

About a month later there is no change in how Google displays the site’s index page. Perhaps it will take longer to find out if this will change it. Or if it won’t change anything.

Have you had any trouble with Google not displaying your page titles in their results? Do you know if displaying the same title twice, both in the title tag and in the meta title tag, is affecting things?

Please do comment.

I love pretty URLs. I don’t like file name extensions unless they end with .html.

All versions of symfony save me from address bar ugliness, but only for the first application I create. Everything else is ass.php/backwards.

I’m going to give you two ways of beautifying your second, third, and forth applications so you can http://www.get.your/application/looking/sexy. Both methods involve creating a subdirectory.

Put it all in a subdirectory, and symlink your way out

So if you have an app named “admin,” you can do the following:

  1. Create a directory in your web folder called admin
  2. Copy your admin.php and admin_dev.php into this folder
  3. Copy the .htaccess file into this folder
  4. Rename admin.php to index.php
  5. Edit the file now named index.php, and change the require_once statement at the top so it’s an extra directory back
  6. Symbolically link the js, css, images, and uploads directories from within the admin directory to the directory above; so from your app directory: ln -s ../js

This is what I usually do in a Linux environment. However, I’m developing on a Windows box, and symbolic links don’t translate easily between Windows and Linux. So here’s the second option, without using symbolic links:

Put just the .htaccess in a subdirectory

  1. As before, create a subdirectory in your web folder for the admin.
  2. Copy the .htaccess file in, and at the very bottom of the file, change index.php to ../admin.php. It should read RewriteRule ^(.*)$ ../admin.php [QSA,L]
  3. Edit your routing.yml file for the admin app, and append /admin/ to all of your routes.

That should about cover it. Also remember to set no_script_name to on in each of your applications.

Enjoy.

Alternative methods? Please leave comments.

Tags: ,

This post assumes you have some experience with Symfony 1.0.

At my work I needed to create several different versions of the same site. This mainly entailed changing background images to get a slightly different look and feel, and changing the content category, depending on a request parameter.

For this tutorial, I will use the company I work for as an example. Momentum IT Group has two distinct divisions: ELearning and Web. So let’s say I wanted to make slight design changes depending on which division I’m looking at.

We’ll start where all things Symfony begin: with the routes. Open up apps/frontend/config/routing.yml and enter the following:

division_page:
  url:               /:division/:slug
  param:          { module: cms, action: display }
  requirements: { division: elearning|web }

I leave the cms module for you to implement. The display action would take a unique slug as a key to look up the page content from the database for display.

In this route, the division is required to be either elearning or web, and these two options will be used to display a different background design (and possibly select from a different content category in your cms module).

The next step is to implement the switch which determines which background to apply, if any. You can always write this code in your cms module, but you may have other modules other than the cms module which need to change design elements depending on which division the user is viewing. To handle this situation, you can create a filter.

A filter is applied once for each request. Start by opening apps/frontend/config/filters.yml and adding a new filter class:

rendering:    ~
web_debug: ~
security:     ~

# generally, you will want to insert your own filters here
cssFilter:
  class:       cssFilter

cache:       ~
common:    ~
flash:         ~
execution:  ~

Create the filter class in apps/frontend/lib/cssFilter.class.php:

<?php

class cssFilter extends sfFilter
{
  public function execute($filterChain)
  {
    if ( $this->isFirstCall())
    {
      $response = $this->getContext()->getResponse();
      switch ( sfContext::getInstance()->getRequest()->getParameter('division') ) 
      {
        case 'elearning':
          $response->addStylesheet('elearning', 'last');
        break;
        case 'web':
          $response->addStylesheet('web', 'last');
        break;
      }
    }
  }
}

Make sure you place two new CSS files in your web/css directory.

For this example, they are elearning.css and web.css. They will be the last css files in your HTML header and will override your main.css where needed. So, you might override a background image like so:

web/css/main.css

body {
  background: url(../images/background.jpg) no-repeat top left;
}

web/css/elearning.css

body {
  background-image: url(../images/elearning.jpg);
}

Now if you direct your browser to [your_site]/elearning/[page_slug], you will have taken advantage of CSS cascading and Symfony filters to present a slightly different design depending on the context.

Tags: , , ,