2012-04-26 10 views
0

私はパーマリンクに関する段落を読んだ(主キーを隠し、重要な文字列に置き換えます)が、私はそれは、このコードをどのように機能するかを理解することはできません。のsymfony:パーマリンク(URLで非表示ID)

public function executePermalink($request) 
    { 
    $article = ArticlePeer::retrieveBySlug($request->getParameter('slug'); 
    $this->forward404Unless($article); // Display 404 if no article matches slug 
    $this->article = $article;   // Pass the object to the template 
    } 

このコードは典型的なpropelですが、それは正しいのでしょうか?教義のようなものがありますか?私はretrieveBySlug()関数を書いていますか?私はそれを書く方法を理解できる例がありますか?教義で

どうもありがとう

答えて

1

あなたが使用することができ、「コードはSluggable」と呼ばれる拡張機能を持っています。

それはあなたがあなたのschema.ymlを変更し、 "コードはSluggable" 拡張子を追加する必要があります動作させるために:

あなたにその後、あなたのrouting.ymlでDoctrineRoute

# apps/frontend/config/routing.yml 
category: 
    url:  /article/:slug 
    class: sfDoctrineRoute 
    param: { module: article, action: show } 
    options: { model: Article, type: object } 

を設定

# config/doctrine/schema.yml 
Article: 
    actAs: 
    Timestampable: ~ 
    Sluggable: 
     fields: [name] 
    columns: 
    name: 
     type: string(255) 
     notnull: true 

あなたはこのような何かを行うことができ、アクションのためのコード:

public function executeShow(sfWebRequest $request) 
{ 
    $this->article = $this->getRoute()->getObject(); 
    $this->forward404Unless($article); // Display 404 if no article matches slug 
    $this->article = $article;   // Pass the object to the template 
} 

教義を実行することを忘れないでください:再構築しますスキーマを変更した後でデータベースを作成します。

+0

完璧を使用して、エラーがあったので、感謝します。私はそれを試してみます。 – satboy78

+0

@ilanco '$ this-> getRoute() - > getObject()'が既にチェックを実行しているので、 'forward404Unless'でチェックを取り除くことができます。 – j0k

+0

こんにちは、私はそれを試しました...それは私にいくつかの問題を与えましたが、私はそれを解決しました(私はそれをやった方法について説明して答え...) – satboy78

0

は現在正常に動作しています。

# apps/frontend/config/routing.yml 
opera_slug: 
    url: /:sf_culture/opere/:operaslug.html 
    class: sfDoctrineRoute 
    param: { module: opera, action: permalink } 
    options: { model: Opera, type: object } 
    requirements: 
    sf_culture: (?:it|en|es|fr) 



    public function executePermalink(sfWebRequest $request) 
    { 
    $this->opera = $this->getRoute()->getObject(); 
    $this->forward404Unless($this->opera); // Display 404 if no article matches slug 
    //$this->opera = $opera;   // Pass the object to the template 
    } 

あなたが見ることができるように、私はexecutePermalink(の最後の2行を変更した)、私はあなたの関数

+0

いいですが、 sf_formatとrequiremetに追加するsf_format:html –