Hosting symfony based website with nginx

December 4th, 2009

The following config file allows you to host symfony based website with Nginx web server.


server {
	listen   80;
	server_name  www.your-url.com;

               #Enable Gzip
	gzip on;
	gzip_min_length 1000;
	gzip_types text/plain application/xml;
	gzip_disable "MSIE [1-6]\.";

              #where to put log files
	access_log  /var/log/nginx/your-url.com.access.log;

              #project's main directory
	root /full/path/web/;

              #change the following to index.php on production server.
	index front_dev.php;

	charset utf-8;

	location /sf {
                  #path to folder where all symfony assets are located
	    alias /full/path/lib/symfony/data/web/sf;
	}

	location / {
	    # If the file exists as a static file serve it directly without
	    # running al	l the other rewite tests on it
	    if (-f $request_filename) {
		expires 1m;
		break;
	      }

	    if ($request_filename !~ "\.(js|htc|ico|gif|jpg|png|css)$") {
                            #change front_dev to index.php on production server
		rewrite ^(.*) /front_dev.php$1 last;
		  }
	}

	location ~ "^(.+\.php)($|/)" {

	    set $script $uri;
	    set $path_info "/";

	    if ($uri ~ "^(.+\.php)($|/)") {
		set $script $1;
	      }

	    if ($uri ~ "^(.+\.php)(/.+)") {
		set $script $1;
		set $path_info $2;
	      }

	  include /etc/nginx/fastcgi_params;
	  fastcgi_pass 127.0.0.1:9000;

	  fastcgi_param SCRIPT_FILENAME /full/path/web$script;
	  fastcgi_param SCRIPT_NAME $script;
	  fastcgi_param PATH_INFO $path_info;
	}
}

Mariusz Hosting , ,

Accessing sfGuardPlugin user id.

September 29th, 2009

If you are using sfGuardPlugin all you need to get id of a user that is currently logged in is this:

In your actions.class.php

$user_id = $this->getUser()->getGuardUser()->getId();

But this will cause fatal error if the user is not authenticated. We can prevent this with isAuthenticated().
// /apps/frontend/lib/myUser.class.php

class myUser extends sfGuardSecurityUser
{
     public function getUserId()
     {
        if($this->isAuthenticated())
           return $this->getGuardUser()->getId();
        else
           return 0;
      }
}

Mariusz Actions, Plugins, View

Using Memcached with Symfony 1.2 and Doctrine

September 15th, 2009

Make sure you have Memcached installed.

Add this to ProjectConfiguration.class.php

//ProjectConfiguration.class.php
    public function configureDoctrine(Doctrine_Manager $manager) {
 
        $servers = array(
            'host' => 'localhost',
            'port' => 11211,
            'persistent' => true
        );
 
 
        $cacheDriver = new Doctrine_Cache_Memcache(array(
            'servers' => $servers,
            'compression' => false
            )
        );
 
        //enable Doctrine cache
        $manager = Doctrine_Manager::getInstance();
 
 
 
        $manager->setAttribute(Doctrine::ATTR_RESULT_CACHE, $cacheDriver);
 
    }

Add this to your query

 ->useResultCache(true);

Mariusz Actions ,

Accessing sfUser from template

February 3rd, 2009

It’s very easy - just use the $sf_user variable

Example.:

<?php if($sf_user->isAuthenticated()):? >
     < ?php echo "User is authenticated"?>
<?php else:?>
     <?php echo "User is not authenticated"?>
<?php endif?>

Mariusz View

Creating thumbnails with sfThumbnailPlugin and doctrine

February 3rd, 2009
 
public function set($name, $value, $load = true)
{
            parent::set($name, $value, $load);
            if($name == 'file')
                $this->generateThumbnail($value);
            }
 
public function generateThumbnail($value)
{
            $uploadDir = sfConfig::get('sf_upload_dir');
            $loadDir = $uploadDir;
            $thumbnail = new sfThumbnail(150, 150);
            $thumbnail->loadFile($loadDir.$value);
            $thumbnail->save($uploadDir.'/thumbs/'.$value, 'image/jpeg');
     }

Mariusz Model ,

Hello World!

February 3rd, 2009
 
<?php echo 'Hello World!'?>

Mariusz View