Symfony Slugify Slug

A slug is basically a URL-friendly string for identifying a post, page, user, etc..  It provides a better idea of what the content the URL points to and will actually be about.

There are 4 basic steps in Symfony slugging.  

Create a route for the slugged model in the routing.yml config file using the sfDoctrineRoute class and pass to your model’s show action

 

account_show_user:
  url: /member/:slug
  class: sfDoctrineRoute
  param: { module: account, action: show }
  options: { model: sfGuardUser, type: object }
  requirements:
    id: \d+
    sf_method: [get]


Add the Sluggable behavior to your model in schema.yml and define the field we want slugged.

sfGuardUser:
  actAs:
    Postable: ~
    Timestampable: ~
    Sluggable:
      unique: true
      fields: [username]
      canUpdate: true
  #...

Create a generic project class with the slugify method or behavior (replacing spaces with hyphens etc.)

class Slugz
{
  static public function slugify($text)
  {
    // replace all non letters or digits by -
    $text = preg_replace('/\W+/', '-', $text);

    // trim and lowercase
    $text = strtolower(trim($text, '-'));
 
    return $text;
  }
}

Create the show action within the model’s action file. Make sure to reference the route object that the routing.yml config file has internally created.

public function executeShow(sfWebRequest $request)
  {
    $this->account = $this->getRoute()->getObject();
    $this->forward404Unless($this->account);
  }

For more

Unable to write cache file” problem in Symfony 2

One common issue in Symfony 2 projects is that the “app/cache” and “app/logs” directories must be writable both by the web server and the command line user. On a UNIX system, if your web server user is different from your command line user, you can run the following commands just once in your project to ensure that permissions will be setup properly. 

Simply type the following command:

 

sudo setfacl -R -m u:www-data:rwX -m u:`whoami`:rwX app/cache app/logs
sudo setfacl -dR -m u:www-data:rwx -m u:`whoami`:rwx app/cache app/logs