Posted on 17th February 2010No Responses
Przydatne funkcje Symfony

Czasami mam problem z zapamiętaniem niektórych funkcji Symfony z których korzystam po prostu częściej niż z innych, dlatego postanowiłem systematycznie wrzucać tutaj takie funkcje żeby mieć je zawsze pod ręką…

Request

Pobieranie adresu IP użytkownika

// w akcji
$this->getRequest()->getHttpHeader( 'addr' , 'remote' );
// w widoku
$sf_request->getHttpHeader( 'addr' , 'remote' );
// gdziekolwiek indziej
sfContext::getInstance()->getRequest()->getHttpHeader( 'addr' , 'remote' );

Routing

Generowanie adresu URL

sfContext::getInstance()->getController()->genUrl( $route , $absolute = false );
//jesli konieczne jest przekazanie obiektu
sfContext::getInstance()->getController()->genUrl( array(
  'sf_route' => 'route_name',
  'sf_subject' => $PropelObject
) );
//lub parametrów
sfContext::getInstance()->getController()->genUrl( array(
  'sf_route' => 'route_name',
  'article_id' => $article_id,
  'article_slug' => $article_slug
) );
//lub tradycyjnie
url_for( $route , $absolute = false );

Pobranie nazwy routingu

//w akcji
$this->getContext()->getRouting()->getCurrentRouteName()
//gdziekolwiek indziej
sfContext::getInstance()->getRouting()->getCurrentRouteName()

ORM

Pobranie domyślnej wersji językowej w modelu (Propel)

$culture = sfPropel::getDefaultCulture();

Response

Sekcja META

$this->getResponse()->setTitle(  $title );
$this->getResponse()->addMeta( 'description', $description );
$this->getResponse()->addMeta( 'keywords', $keywords );

Nagłówki

  //Content Type
  $this->getResponse()->setContentType( 'application/json' ); //odpowiedz JSON
  $this->getResponse()->setContentType( 'text/xml' ); //odpowiedz XML

Inne posty:

  1. Jak wygenerować bardzo dużą mapę strony (sitemap.xml)
  2. Przydatne wyrażenia regularne
Comments
Leave a Response
XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Comment moderation is enabled. Your comment may take some time to appear.